[英]In ASP.NET how can I retrieve data from a specific control that is within a DataList
Within a DataList I have comments retrieved from a database. 在数据列表中,我有从数据库检索的注释。 Each comment set has its own reply textbox along with a submit button to submit the reply.
每个评论集都有其自己的回复文本框以及一个提交按钮以提交回复。 On the button I have an click event along with the commentID as a commandArgument so I can retrieve the comment id in my event.
在按钮上,我有一个click事件以及commentID作为commandArgument,因此我可以在事件中检索注释ID。 How do i reference the specific comment box to retrieve the text though.
我如何引用特定的注释框来检索文本。 My comment container looks something like:
我的评论容器如下所示:
<div class="replyContainer">
<asp:TextBox ID="replyBox" runat="server"></asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server"
CommandArgument='<%# Eval("cID") %>'
onclick="replyPostClick" />
</div>
My C# method behind looks something like: 我背后的C#方法如下所示:
protected void replyPostClick(object sender, EventArgs e)
{
ImageButton btn = sender as ImageButton;
CommentQueries.addComment(objectID, userID, btn.CommandArgument.ToString(), ?);
}
The question mark would be where I pass in the comment. 问号将是我在评论中传递的位置。 Is there some way to retrieve the relevant textbox's text somehow?
有某种方法可以某种方式检索相关文本框的文本吗?
You can get the DataListItem
from the ImageButton which has been clicked. 您可以从已单击的ImageButton中获取
DataListItem
。
The code would be something like; 该代码将类似于:
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;
}
Once you have the item
you can can then 拿到
item
后就可以
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.