How to pass a parameter by pressing a button

I have a situation: I show a lot of rows from the database on the page. just by creating dynamic rows ( <% foreach (res in DBVar) %> ). Each line has a button. each button uses only one OnClick method. I really don’t care about the name (value) of these buttons, but I can’t take how I can pass the parameter (for example, the row identifier from the DB ( res.ID )) from the .aspx page to the OnClick method, (Using LINQ to SQL) I tried to assign my parameter to the name (value) of the button with " <input type="button" value="<%= "string"+DBVar.ID%> "etc. runat = the server cannot even take the variable to the name (value) coz of this I used only the input method.

+7
source share
3 answers

Use the OnCommand event and assign a CommandArgument

  <asp:Button ID="Button1" runat="server" Text="Submit" CommandArgument='<%= res.ID %>' OnCommand="Button1_Click" /> 

in code for

 protected void Button1_Click(Object sender, CommandEventArgs e) { string ID=e.CommandArgument.ToString(); } 
+8
source
+3
source

check this link

passing dynamic values ​​for each row as a list

he did the same and he also shows how to access the values ​​in the code in

+3
source

All Articles