Programmatically display the image button in asp.net c#

I get my expected output displayed as below, but how do I know which button the user clicked?
enter image description here

Below here is my code in asp.net, or maybe I should use asp:Repeater to do this ?

    <table style="width: auto">
     

        <% int count = 1;

            for (int i = 0; i < (No_of_Bed / 2); i++){%>

        <tr>

        <% for (int j = 0; j < 2; j++) { %>
                <td>
                    Position <%=count %> :
                </td>
                <td>
                    <asp:ImageButton ID="bed" runat="server" ImageUrl="~/img.png" />
                </td>
            
        <% count++; }%>

           </tr>
         <%   } %>
            
        <tr>
</table>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Okay, so here is an example of how to do it.

<table style="width: auto">
     

        <% int count = 1;

            for (int i = 0; i < (No_of_Bed / 2); i++){%>

        <tr>

        <% for (int j = 0; j < 2; j++) { %>
                <td>
                    Position <%=count %> :
                </td>
                <td>
                    <asp:ImageButton ID="bed" runat="server" OnClick="bed_Click" CommandName='Parse here the value you want' ImageUrl="~/img.png" />
                </td>
            
        <% count++; }%>

           </tr>
         <%   } %>
            
        <tr>
</table>

You can use by the way CommandName and CommandArgument, it does not really matter.
Backend:

 protected void bed_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = sender as ImageButton;

            string commandname = btn.CommandName;
        }

Method 2

for that, you need a different Id for every Image buttons,
not sure if it is useful for your Requirement, but you can try using this

Change ID=”bed_0″ -> For Image one ,
ID=”bed_1″ So on

For that, you can use int j


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x