简体   繁体   English

如何检测单击哪个datalist项元素?

[英]How to detect which element of datalist items is clicked?

my webform app have a datalist in the updatepanel, and datalist contain:{imagebutton, label,label}. 我的webform应用程序在updatepanel中有一个datalist,datalist包含:{imagebutton,label,label}。 datalist is connected to bank. datalist连接到银行。 in imagebutton thumbs of images will be show. 在图像按钮拇指的图像将显示。 I want when a user clicked each imagebutton in datalist, original image will be shown. 我想当用户点击datalist中的每个图像按钮时,将显示原始图像。 all data successfully shown in datalist but problem is after click on imagebutton. 所有数据都成功显示在datalist中,但问题是在点击图像按钮后。 i cant continue. 我不能继续 how can I detect which imagebutton clicked? 如何检测哪个图像按钮被点击? what is the its datasource? 它的数据源是什么? what is the text or datasource of labes that are in same cell? 同一细胞中的实验室的文本或数据源是什么?

please help me, my code is as blow: 请帮帮我,我的代码就像打击一样:

<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>      

and

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

The sender is the button, it's NamingContainer is the DataListItem : 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;
}

You could also use the ImageButton 's CommandArgument to store the ID or use a hidden control ( Visible=false or HiddenField ) to store it. 您还可以使用ImageButtonCommandArgument来存储ID或使用隐藏控件( Visible=falseHiddenField )来存储它。

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

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