3

my webform app have a datalist in the updatepanel, and datalist contain:{imagebutton, label,label}. datalist is connected to bank. in imagebutton thumbs of images will be show. I want when a user clicked each imagebutton in datalist, original image will be shown. all data successfully shown in datalist but problem is after click on imagebutton. 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?
}
Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
Robert
  • 45
  • 6

1 Answers1

4

The sender is the button, it's NamingContainer is the DataListItem:

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.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • thank you, but sometime 45 item appear when i selected * from db, then how to findcontrol from 45 lblcname? – Robert Jul 31 '14 at 12:15
  • @Robert: with my code above you will always find the controls in the same item of the datalist which was clicked. Is that what you want? The `NamingContainer` always refers to the `DataListItem` of the control. – Tim Schmelter Jul 31 '14 at 12:16
  • how to find a div inside updatepanel but outside the button cell? – Robert Jul 31 '14 at 17:16