gpt4 book ai didi

c# - 如何检测单击了数据列表项的哪个元素?

转载 作者:行者123 更新时间:2023-11-30 15:28:46 25 4
gpt4 key购买 nike

我的网络表单应用程序在更新面板中有一个数据列表,数据列表包含:{imagebutton, label,label}。数据列表连接到银行。在 imagebutton 中将显示图像的缩略图。我希望当用户单击数据列表中的每个图像按钮时,将显示原始图像。所有数据都成功显示在数据列表中,但问题出在单击图像按钮之后。我不能继续。我如何检测点击了哪个图像按钮?它的数据源是什么?同一单元格中标签的文本或数据源是什么?

请帮助我,我的代码如下:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>

<div class="content">
<div class="datashow">
<asp:DataList ID="DataList1" runat="server" BorderColor="#666666" BorderStyle="None" BorderWidth="2px" CellPadding="2" CellSpacing="2" Font-Size="Small" RepeatDirection="Horizontal" CssClass="dl" HorizontalAlign="Center" RepeatColumns="5">
<HeaderStyle BackColor="#333333" Font-Bold="True" Font-Size="Large" ForeColor="White" HorizontalAlign="Center" Wrap="True" />
<ItemTemplate>
<asp:ImageButton ID="imgbtn" runat="server" ImageUrl='<%# Bind("imgtp","~/images/{0}") %>' Width="100px" OnClick="imgbtn_Click" />
<br />
<b>Employee Name:</b>
<asp:Label ID="lblCName" runat="server" Text='<%# Bind("name") %>'></asp:Label>
<br />
<b>Designation:</b>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("company") %>'></asp:Label>

</ItemTemplate>
</asp:DataList>

</div>
</div>

</ContentTemplate>
</asp:UpdatePanel>

protected void imgbtn_Click(object sender, ImageClickEventArgs e)
{
//how to detect which imagebtn clicked and what is its datasource?
}

最佳答案

sender 是按钮,NamingContainerDataListItem:

protected void imgbtn_Click(object sender, ImageClickEventArgs e)
{
var imgbtn = (ImageButton) sender;
var item = (DataListItem) imgbtn.NamingContainer;
// the datasource is not available on postback, but you have all other controls
var lblCName = (Label) item.FindControl("lblCName");
string company = lblCName.Text;
}

您还可以使用 ImageButtonCommandArgument存储 ID 或使用隐藏控件(Visible=falseHiddenField)来存储它。

关于c# - 如何检测单击了数据列表项的哪个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25058491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com