【问题标题】:Access TextBox in DataList by ID from Button-Click handler in codebehind从代码隐藏中的 Button-Click 处理程序按 ID 访问 DataList 中的 TextBox
【发布时间】:2014-07-21 10:41:43
【问题描述】:

我有一个保存在 Datalist 中的文本框。我需要通过 ID 找到它,以便我可以将写入该文本框的文本插入到数据库中。这是我的包含文本框的 aspx 页面。

<asp:Content ID="Content1" ContentPlaceHolderID="ccont" Runat="Server">
  <div id="ccont">
      <asp:DataList ID="mydatalist" ItemStyle-CssClass="lft_c_down"  runat="server">
        <ItemTemplate>
          <div id="wholeC">
            <div id="ctop">
             <div id="lft_l">
                <div id="lft_c_top">
                   <asp:Image runat="server" ImageUrl='<%#DataBinder.Eval(Container.DataItem,"ipath")%>' Height="250px" Width="300px" />
                    <br/>
                </div>
                <div id="lft_c_down">
                   <b>Product Name:</b>
                   <asp:Label ID="lbl2" Text='<%#DataBinder.Eval(Container.DataItem,"products") %>' runat="server" />
                   <br/>
                   <b>brand:</b>
                   <asp:Label ID="lbl1" Text='<%#DataBinder.Eval(Container.DataItem,"brand") %>' runat="server" />
                   <br/>
                   <b>Price:</b>
                   <asp:Label ID="Label1" Text='<%#DataBinder.Eval(Container.DataItem,"price") %>' runat="server" />
                </div>
              </div>
              <div id="lft_r">
                    <b>Details:</b>
                   <asp:Label ID="Label2" Text='<%#DataBinder.Eval(Container.DataItem,"description") %>' runat="server" />
              </div>
           </div>
          <div id="cmt">
               <asp:TextBox ID="tb_cmt" runat="server" Height="35px" Width="620" placeholder="comment.."  />
               <asp:Button ID="Button1" runat="server" Text="Comment" backcolor="black" BorderStyle="None" Font-Names="Consolas" Font-Overline="False" 
                ForeColor="White" Height="34px" Width="108px" OnClick="cmt_Click" />
           </div>
         </div>
        </ItemTemplate>
      </asp:DataList>

       </div>

ID="tb_cmt" 的文本框是我想在后面的代码中访问的文本框:

protected void cmt_Click(object sender, EventArgs e)
{
    // how to get the TextBox?
    sq.connection();
    SqlCommand cmd = new SqlCommand("insert into comment(ecomment,sid) values(@myecomment,@mysid)", sq.con);
    cmd.Parameters.AddWithValue("@myecomment",tb_cmt.text)//but here tb_cmt is not recognized.
}

【问题讨论】:

    标签: c# asp.net textbox


    【解决方案1】:

    您可以使用被点击按钮的NamingContainer property 来获取DataListItem。然后你只需要使用FindControl 来获取你的TextBox 的引用:

    protected void cmt_Click(object sender, EventArgs e)
    {
        Button btn = (Button) sender;
        DataListItem item = (DataListItem) btn.NamingContainer;
        TextBox txt = (TextBox) item.FindControl("tb_cmt");
        //... save
    }
    

    【讨论】:

    • 感谢您的回答和编辑。顺便说一句,您能帮我解决另一个问题。正如您在上面的代码中看到的那样,注释文本框位于 Datalist 内,现在我想在评论框下方显示 cmets。但我必须从另一个表中获取数据(评论)。这可能吗?
    • 为什么不能从表中获取数据?你在哪里卡住了?对不起,我不明白这个问题。你应该考虑问另一个问题,因为 cmets 不是用来回答问题的;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    相关资源
    最近更新 更多