繁体   English   中英

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

[英]In ASP.NET how can I retrieve data from a specific control that is within a DataList

在数据列表中,我有从数据库检索的注释。 每个评论集都有其自己的回复文本框以及一个提交按钮以提交回复。 在按钮上,我有一个click事件以及commentID作为commandArgument,因此我可以在事件中检索注释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(), ?); 
}

问号将是我在评论中传递的位置。 有某种方法可以某种方式检索相关文本框的文本吗?

您可以从已单击的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后就可以

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM