0

我正在尝试使用转发器控件插入评论

我的 html 代码

 <asp:Repeater ID="repConcerns" runat="server" OnItemCommand="post"  >
           <ItemTemplate >           
             <div class="row col-md-12">


                         <div class="col-sm-2" style="background-color:#808080">
                         <asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("Pathh") %>' width="90px" Height="90px" CssClass="img-circle" title='<%#Eval("Name") %>'/>&nbsp;<br/><asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("Name") %>'  CssClass="btn btn-info btn-sm" CommandName="btnMessage" CommandArgument='<%#Eval("Username") %>'></asp:LinkButton><br/>                    

                              </div> 
                       <div class="col-sm-10" style="background-color:#808080" >
                         <asp:Label ID="Label1" width="100%" style="padding:7px;" runat="server" Enabled="false" Text='<%#Eval("Title") %>'> </asp:Label>
                         <asp:TextBox ID="txtMessage" placeholder="Empty Post" width="100%" style="padding:7px;" runat="server" Text='<%#Eval("Detail") %>' TextMode="MultiLine" Enabled="false" Height="100px"> </asp:TextBox>
                         <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("Sno") %>' />
                        <div class="bar"></div> 

                           <asp:TextBox ID="txtcomment" runat="server" CssClass="form-control form-control-sm" placeholder="Comment / Write Openion Suggestion" TextMode="MultiLine" Height="40px"></asp:TextBox>
                           <asp:LinkButton ID="btn" runat="server"  CssClass="btn btn-primary" CommandName="comment" CommandArgument='<%#Eval("Sno") %>' >Post</asp:LinkButton>
                       </div>



              </div> 
               <div  style="padding-bottom:10px;"></div>

                 <%--<br /> --%>                     
           </ItemTemplate>
        </asp:Repeater>

和 C# 代码

 protected void post(object source, RepeaterCommandEventArgs e)
{

    if(e.CommandName== "comment")
    {
        a = e.CommandArgument.ToString();

        SqlConnection con = new SqlConnection(strconn);
        SqlCommand com = new SqlCommand();
        com.CommandText = "insert into Comments(Sno,Comment,Username)values('" +a+ "','" + txtcomment.Text  + "','" + username + "')";
        con.Open();


    }
}

我不知道如何插入表“评论”。我制作了一个“页面”,其中它们是墙贴。我已经包含了一个评论选项。评论按钮在每篇文章中都会出现。但我不知道如何在“评论”表中插入评论。我正在尝试使用保存在 HiddenField 中的帖子的相应“Sno”插入评论。但是当我尝试在“txtcomment.text”中写入文本框 id 时,它给了我错误。我如何插入。

4

1 回答 1

1

我对您的原始代码进行了一些修改 - 请注意注释:

protected void post(object source, RepeaterCommandEventArgs e)
{

    if(e.CommandName== "comment")
    {
        //Find TextBox Control
        TextBox txt = (TextBox)e.Item.FindControl("txtcomment");
        var sno = e.CommandArgument.ToString();

        SqlConnection con = new SqlConnection(strconn);
        SqlCommand com = new SqlCommand();
        com.CommandText = "INSERT INTO Comments(Sno,Comment,Username) VALUES (@SNO, @COMMENT, @USERNAME)";
        com.Connection = con;

        //Add Command Parameters
        com.Parameters.AddWithValue("@SNO", sno);
        com.Parameters.AddWithValue("@COMMENT", txt.Text);
        com.Parameters.AddWithValue("@USERNAME", username);

        con.Open();

        //Execute Command
        com.ExecuteNonQuery();
    }
}
于 2018-01-14T14:14:30.980 回答