anthonypliu anthonypliu - 21 days ago 5
ASP.NET (C#) Question

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

Here is my ASP code:

<asp:GridView ID="WagerTable" runat="server" AutoGenerateColumns="False" CssClass="basix" >
<columns>
<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:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Accept" OnClick="AcceptWager" ID="AcceptButton" />
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>


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))
{

myConnection.Open();
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;
WagerTable.DataBind();
myConnection.Close();
}



}
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
reader
. Thanks

Answer

<asp:LinkButton> has a CommandArgument attribute.

Probably something like this:

  <asp:LinkButton CommandArgument='<%# DataBinder.Eval(Container.DataItem, "SOME_VALUE")%>' />