anthonypliu anthonypliu - 1 year ago 68
ASP.NET (C#) Question

How can I pass an argument through the OnClick property in an controller

Here is my ASP code:

<asp:GridView ID="WagerTable" runat="server" AutoGenerateColumns="False" CssClass="basix" >
<asp:BoundField DataField="GameName" HeaderText="Game Name" />
<asp:BoundField DataField="Amount" HeaderText="Amount" />
<asp:BoundField DataField="Comment" HeaderText="Comment" />
<asp:BoundField DataField="CreateTime" HeaderText="Create Time" />
<asp:LinkButton runat="server" Text="Accept" OnClick="AcceptWager" ID="AcceptButton" />

This is my code behind:

protected void Page_Load(object sender, EventArgs e)
AccountManager accManager = new AccountManager();
MembershipUser newUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
Guid UserId = (Guid)newUser.ProviderUserKey;
String myConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
SqlDataReader reader;
using (SqlConnection myConnection = new SqlConnection(myConnectionString))

String selectSql = "SELECT * FROM aspnet_Wagers INNER JOIN aspnet_Games ON aspnet_Wagers.GameId = aspnet_Games.GameId";

SqlCommand myCommand = new SqlCommand(selectSql, myConnection);
myCommand.Parameters.AddWithValue("@UserId", UserId);

reader = myCommand.ExecuteReader();

WagerTable.DataSource = reader;

protected void AcceptWager()


I want to be able to have it so that each LinkButton can pass through an ID from the database as an parameter into the AcceptWager function and then I will do with it from there. The sql column for the ID is WagerId. It is returned in that query that i execute, so it is already in the
. Thanks

Answer Source

<asp:LinkButton> has a CommandArgument attribute.

Probably something like this:

  <asp:LinkButton CommandArgument='<%# DataBinder.Eval(Container.DataItem, "SOME_VALUE")%>' />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download