Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass a parameter by pressing a button

Tags:

c#

asp.net

i have the situation: i'm showing many lines from the DB on the page. just creating dynamic lines (<% foreach (res in DBVar) %>). Every line has a button. every button use just 1 OnClick method. I really dont care of the name(value) of these buttons, but i can't take, how can i pass a parameter (e.g. a ID of a line from DB (res.ID)) from the .aspx page to OnClick Method. (Using LINQ to SQL) I tried to take my param to the name(value) of a button with "<input type="button" value="<%= "string"+DBVar.ID%>" and so on. the runat=server even can't take the variable on the name(value) coz of this i used just input method.

like image 464
Simon Avatar asked May 18 '11 13:05

Simon


1 Answers

Use OnCommand event and assign CommandArgument

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

in code behind

protected void Button1_Click(Object sender, CommandEventArgs e) 
{
string ID=e.CommandArgument.ToString();
}
like image 138
Govind Malviya Avatar answered Sep 20 '22 00:09

Govind Malviya