Repeater - 文本框内容到数据库 C#

Repeater - textbox content to database C#

我正在尝试以 post - 注释方式将内容从转发器元素内的文本框插入我的本地数据库。到目前为止,我已经尝试在所有生成的行上循环以找到特定的文本框,但我没有运气,要么插入为空,要么我为每个预先存在的行插入 1 个,或者我一遍又一遍地插入相同的值通过不同的 posts.

我终于尝试将 post 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>

您可以通过将 OnTextChanged 分配给它来访问 TextBox 控件,如果您想立即访问数据,您还可以将其 AutoPostBack 分配给 true .

但您应该在将数据绑定到转发器之前使用 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;

希望,我能帮上忙..

这些更改对我来说很有用,可以找到特定的文本框并防止 post 接收不需要的数据。

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="true" 添加到文本框和转发器。

最后,最重要的是,我在 onload 事件中添加了 if (!Page.IsPostBack)

加上所有这些,每个 post 的评论都被正确插入。