简体   繁体   English

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

[英]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.

相关问题 如何通过数据库中的 DataList/RepeaterConrol 或 Gridview 在 asp.net C# 中以水平格式显示我的数据? - How can I display my data in horizontal format in asp.net C# through DataList/RepeaterConrol or Gridview from database? 如何在ASP.NET datalist中放置一个列表 - How can I place a list inside ASP.NET datalist 在ASP.Net C#中,在按钮单击事件上,我在DataList中找到FileUpload控件,并且显示错误? - In ASP.Net C#, on button click event I find the FileUpload Control within DataList and It gives error? 如何在DataList控件内的ASP.NET中设置控件的可见性? - How to set the visibility of a control in ASP.NET inside the DataList control? 如何将数据列表c#asp.net中的每个记录导出到单独的excelsheet - How can I export each record from datalist c# asp.net to separate excelsheet 从Datalist在Asp.Net图像控件中显示图片 - Showing Picture in Asp.Net Image Control From Datalist 如何在asp.net中的数据列表控件中更改列宽 - how to change column width inside datalist control in asp.net 数据列表中的ASP.NET CustomValidator OnServerValidate? - ASP.NET CustomValidator OnServerValidate within datalist? 如何在ASP.NET中使用DataList分别显示组数据? - How to display group data separately using DataList in ASP.NET? 如何从 ASP.NET 用户控件中的父页面访问控件? - How do I access controls from the parent page from within an ASP.NET User Control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM