在ASP.. NET如何从DataList中的特定控件检索数据

本文关键字:控件 数据 检索 DataList NET ASP | 更新日期: 2023-09-27 18:11:00

在DataList中,我有从数据库检索的评论。每个评论集都有自己的回复文本框以及用于提交回复的提交按钮。在按钮上,我有一个click事件和一个commentID作为命令参数,这样我就可以在事件中检索评论id。我如何引用特定的评论框检索文本虽然。我的注释容器看起来像这样:

<div class="replyContainer">
    <asp:TextBox ID="replyBox" runat="server"></asp:TextBox>
    <asp:ImageButton ID="ImageButton1" runat="server"
        CommandArgument='<%# Eval("cID") %>'
        onclick="replyPostClick" />
</div> 

我的c#方法后面看起来像:

protected void replyPostClick(object sender, EventArgs e)
{
    ImageButton btn = sender as ImageButton;
    CommentQueries.addComment(objectID, userID, btn.CommandArgument.ToString(), ?); 
}

问号将是我传递评论的地方。是否有某种方式检索相关文本框的文本?

在ASP.. NET如何从DataList中的特定控件检索数据

您可以从已点击的ImageButton中获取DataListItem

代码类似于;

ImageButton img = (ImageButton)sender;
DataListItem item = (DataListItem)img.NamingContainer;
// check that the item is not null
if (item != null)
{        
    //get the index of the Current ImageButton selected
    int itemIndex = item.ItemIndex;
    // find the control and value within the datalist using the itemIndex
    // I don't know the ID of your DataList, so I have just called it DataList1
    var replyText = ((TextBox)this.DataList1.Items[item.ItemIndex].FindControl("replyBox")).Text;
}

一旦你有了item你就可以然后