В моем DataList я хочу, чтобы кнопка отображалась со значком FA, поэтому я использовал кнопку html и сделал ее runat = "server", теперь, когда я нажимаю кнопку, я хочу знать, какой элемент Datalist "ограничен" этой кнопкой .
Я пытался использовать кнопку asp.net, но тогда я не могу использовать значки FA.
Вот как выглядит мой код html и c#:
<asp:DataList Width = "100%" ID = "dtlFAQSections" runat = "server" DataSourceID = "dtsFAQSections" DataKeyField = "FAQSectionID">
<ItemTemplate>
<h2>
<button id = "btnFAQSection" runat = "server" onserverclick = "btnFAQSection_Click" style = "background-color:#f24646; border:none; color:white; margin-left:25px; font-size:16px; cursor:pointer; height: 26px; width: 26px; margin-right: 5px; border-radius:30%;"><i class = "fas fa-plus"></i></button>
<asp:Label Font-Size = "18px" ID = "FAQSectionNameLabel" runat = "server" Text='<%# Eval("FAQSectionName") %>' />
</h2>
<hr style = "border: 1px dotted #000000; border-style: none none dotted; color: #fff; background-color: #fff;"/>
</ItemTemplate>
</asp:DataList>
protected void btnFAQSection_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
DataListItem item = (DataListItem)btn.NamingContainer;
}





Во-первых, вам нужно использовать HtmlButton, так как вы не используете элемент управления ASP в качестве кнопки. Затем просто найдите Родителя.
protected void btnFAQSection_Click(object sender, EventArgs e)
{
HtmlButton btn = (HtmlButton)sender;
DataListItem item = (DataListItem)btn.NamingContainer;
//now you can access the DataListItem
Label label = item.FindControl("FAQSectionNameLabel") as Label;
label.Text = "DataListItem Found";
//or if you want to get the parent DataList
DataList dl = btn.Parent.Parent as DataList;
Label1.Text = dl.ID;
}