how to pass a parameter by pressing a button
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. (U开发者_开发问答sing 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.
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();
}
Set the CommandName and CommandArgument of the buttons during the bind and read them back out on your onclick event.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx
check this link
passing dynamic values for each row in listview
he has done same thing and it also shows how to access the values in code behind
精彩评论