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.D开发者_开发问答ataBind();
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
<asp:LinkButton>
has a CommandArgument
attribute.
Probably something like this:
<asp:LinkButton CommandArgument='<%# DataBinder.Eval(Container.DataItem, "SOME_VALUE")%>' />
Try:
<asp:LinkButton runat="server" Text="Accept" OnClick="AcceptWager" ID="AcceptButton" CommandArgument='<%# ((DataRowView)Container.DataItem)["WagerId"]%>' />
Then in your codebehind you'll want to use:
int wagerId = ((LinkButton)sender).CommandArgument;
Change your LinkButton declaration in the HTML:
<asp:LinkButton CommandArgument='<%# ((SqlDataReader)Container.DataItem)["WagerID"] %>'
runat="server" Text="Accept" OnClick="AcceptWager" ID="AcceptButton" />
You can then extract it by pulling the CommandArgument
property from the button in your command handler method.
It is possible to handle the LinkButton’s DataBinding event and customize set the LinkButton’s CommandArgument equals the “ID” value of the corresponding DataRow. After that, it is possible to handle LinkButton’s Click event, obtain LinkButton’s instance from the sender object, and use its CommandArgument property.
精彩评论