【问题标题】:Repeater - textbox content to database C#中继器 - 文本框内容到数据库 C#
【发布时间】:2017-07-03 21:48:44
【问题描述】:

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

我终于尝试将帖子 id 传递给 itemfinder 并且它有点工作,但是从文本框插入的“comm_contenido”仍然是空的到数据库。

我的问题是从中继器中处理此类插入的正确和更直接的方法是什么?

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: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>

【问题讨论】:

  • 显示加载到中继器代码中。每个帖子都加载它吗?顺便说一句,一旦你开始工作,你应该更进一步,避免代码中出现 SQL 注入。
  • 每个帖子都加载文本框,是的。谢谢,我会尽快完成这项工作。
  • 您不应该在每个帖子上都加载它。如果事件Button1_Command 在您的数据加载事件之后触发,我假设Page_Load,那么用户插入的数据将丢失。只加载一次数据,检查Page.IsPostback 属性。编辑:只需阅读引用 IsPostBack 的答案...检查 Omar 的答案 :-)

标签: c# sql asp.net repeater


【解决方案1】:

您可以通过将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;

希望,我可以帮忙..

【讨论】:

  • 您的答案适合另一种情况,它对我不起作用,因为我使用按钮命令来获取帖子 ID。
【解决方案2】:

这些更改有助于我找到特定文本框并防止发布不需要的数据。

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)

加上所有这些,每个帖子上的 cmets 都被正确插入了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多