2017-07-03 43 views
0

我試圖插入內容到我的本地數據庫從一個文本框內的中繼器元素,以後評論的方式。到目前爲止,我已經嘗試循環所有生成的行來查找特定的文本框,但我沒有運氣,要麼插入變爲空,要麼每個預先存在的行獲得1個插入,或者我重複插入相同的值通過不同的職位。中繼器 - 文本框內容到數據庫C#

我終於嘗試將帖子id傳遞給itemfinder,它有點工作,但是從文本框中插入的「comm_contenido」仍然是空的數據庫。

我的問題是從Repeater中處理這些插入的正確和更直接的方法?

C#:

protected void Button1_Command(object sender, CommandEventArgs e) 
{ 

    string postid = e.CommandArgument.ToString(); 
    string emailcc = Session["EMAIL"].ToString(); 
    string user_id = Session["ID"].ToString(); 
    string usrnom = Session["NOMBRE"].ToString(); 
    string usrfoto = Session["FOTO_URL"].ToString(); 
    //string COMM_CONTENIDO = lblcomm.Text.ToString(); 

    var COMM_fecha = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); 
    TextBox txt2; 

     using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConexionBD"].ConnectionString)) 
     { 
      using (SqlCommand cmd = new SqlCommand()) 
      { 

       int m = Int32.Parse(postid); 
       txt2 = (TextBox)Repeater_UsrPosts.Items[m].FindControl("txtcomentar"); 
       string txt1 = txt2.Text; 

       cmd.CommandType = CommandType.Text; 
       cmd.CommandText = (@"INSERT INTO MIEMBROS_Comments (COMM_USER_ID, COMM_CONTENIDO, COMM_FECHA, COMM_USER_NOMBRE, COMM_USER_FOTO, COMM_POST_ID) VALUES ('" 
       + user_id + "','" + txt1 + "','" + COMM_fecha + "','" + usrnom + "','" + usrfoto + "','" + postid + "');"); 
       cmd.Connection = conn; 
       conn.Open(); 
       int rowsAffected = cmd.ExecuteNonQuery(); 
      } 
     } 

     //txtpublica.Text = ""; 
     traerposts(); 

} 

ASP:

<asp:Repeater ID="Repeater_UsrPosts" runat="server" > 
    <ItemTemplate> 

     <!-- Post --> 
     <div class="post clearfix"> 
      <div class="user-block"> 



       <img alt="" src="<%#Eval("post_user_foto")%>" class="img-circle img-bordered-sm" /> 

       <span class="username"> 
        <a href="#"><%#Eval("post_user_nombre") %></a> 
        <a href="#" class="pull-right btn-box-tool"><i class="fa fa-times"></i></a> 
       </span> 
       <span class="description"><%#Eval("post_fecha") %></span> 
      </div> 
      <!-- /.user-block --> 

      <p> 

       <%#Eval("post_contenido") %> 
      </p> 

      <ul class="list-inline"> 
       <li><a href="#" class="link-black text-sm"><i class="fa fa-share margin-r-5"></i>Share</a></li> 
       <li><a href="#" class="link-black text-sm"><i class="fa fa-thumbs-o-up margin-r-5"></i>Like</a> 
       </li> 
       <li class="pull-right"> 
        <asp:LinkButton ID="bttnabrircomentarios" runat="server" class="link-black text-sm"> 
         <i class="fa fa-comments-o margin-r-5"></i>Comments</asp:LinkButton> 
       </li> 
      </ul> 

      <asp:TextBox ID="txtcomentar" runat="server" class="form-control input-sm" placeholder="Escribe un comentario" EnableViewState="False"></asp:TextBox> 

      <%# Eval("post_id") %> - 

      <asp:Button ID="Button1" runat="server" Text="Button" 
       OnCommand="Button1_Command" CommandName="myCommand" 
       CommandArgument='<%# Eval("post_ID") %>' /> 
      <br /> 
     </div> 
     <!-- /.post --> 
    </ItemTemplate> 
</asp:Repeater> 
+1

顯示加載到中繼器代碼中。它是否被加載到每一篇文章中?順便說一句,一旦你得到它的工作,你應該更進一步,避免在你的代碼中存在SQL注入。 – bradbury9

+0

該文本框正在每個帖子加載,是的。謝謝,我會盡快完成這項工作。 – Ghaamae

+0

你不應該在每一篇文章中加載它。如果事件'Button1_Command'在你的數據加載事件後觸發,我假設'Page_Load',那麼用戶插入的數據將會丟失。只加載一次數據,檢查'Page.IsPostback'屬性。編輯:只是閱讀回答引用IsPostBack ...檢查Omar的回答:-) – bradbury9

回答

0

這是我找到特定文本框並防止發佈不需要的數據的變化。

C#:

protected void Button1_Command(object sender, CommandEventArgs e) 
{ 

    string postid = e.CommandArgument.ToString(); 
    string emailcc = Session["EMAIL"].ToString(); 
    string user_id = Session["ID"].ToString(); 
    string usrnom = Session["NOMBRE"].ToString(); 
    string usrfoto = Session["FOTO_URL"].ToString(); 

    var COMM_fecha = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); 
Control s = (Control)sender; 
    TextBox tb = (TextBox)s.NamingContainer.FindControl("txtcomentar"); 
    tb.ReadOnly = true; 
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConexionBD"].ConnectionString)) 
    { 
     using (SqlCommand cmd = new SqlCommand()) 
     { 

      string txt1 = tb.Text; 

      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = (@"INSERT INTO MIEMBROS_Comments (COMM_USER_ID, COMM_CONTENIDO, COMM_FECHA, COMM_USER_NOMBRE, COMM_USER_FOTO, COMM_POST_ID) VALUES ('" 
      + user_id + "','" + txt1 + "','" + COMM_fecha + "','" + usrnom + "','" + usrfoto + "','" + postid + "');"); 
      cmd.Connection = conn; 
      conn.Open(); 
      int rowsAffected = cmd.ExecuteNonQuery(); 
     } 
    } 
    traerposts(); 
} 

而且對ASP我加屬性的EnableViewState = 「真」到文本框,併到中繼器。

最後,最重要的是,如果(!Page.IsPostBack)添加了到onload事件。

隨着這一切,每篇文章的意見都被正確插入。

1

可以通過分配OnTextChanged它達到TextBox控制,並且你還可以指定其AutoPostBacktrue如果你想達到的數據立即。

但是在將數據綁定到中繼器之前,您應該使用if(!IsPostBack),因此在您到達數據之前它不會重置您的Controls

OnTextChanged需要兩個參數,其中之一是sender對象,它是調用它,那是你TextBox,像..

ASP

<asp:Repeater ID="RepeaterExample" runat="server"><ItemTemplate> 
    <asp:TextBox runat="server" ID="TextBoxExample" AutoPostBack="True" OnTextChanged="TextBoxExample_OnTextChanged"/> 
</ItemTemplate></asp:Repeater> 

代碼隱藏

protected void TextBoxExample_OnTextChanged(object sender, EventArgs e) 
{ 
    TextBox txt = (TextBox) sender; 
    //Response.Write(txt.Text); 
    //or whatever you want to do with it. 
} 

如果你想和Button_OnClick一起使用,你應該像以後可以調用的全局字符串一樣使用,你可以做這樣的事情..

ASP

<asp:Button runat="server" ID="ButtonExample" OnClick="ButtonExample_OnClick"/> 

代碼

private string text = ""; 

protected void TextBoxTest_OnTextChanged(object sender, EventArgs e) 
{ 
    TextBox txt = (TextBox)sender; 
    text = txt.Text; 
} 

protected void ButtonExample_OnClick(object sender, EventArgs e) 
{ 
    //Response.Write(text); 
} 

但最後一種方法將最後一個TextBox其文本的背後的價值已經改變,除非你將其添加在一起,就像..

text += txt.Text; 

希望,我可以幫助..

+0

您的答案符合另一種情況,它不適合我,因爲我使用按鈕命令帶來職位ID。 – Ghaamae

相關問題