---------------------------------------Form Design---------------------------
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" Height="188px"
Width="127px" DataSourceID="SqlDataSource1"
onrowcommand="GridView1_RowCommand1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Add to Cart" />
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
------------------------------code-----------------------------
protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
Label txt = (Label)GridView1.Rows[index].FindControl("Label1");
string test = txt.Text;
Label name = (Label)GridView1.Rows[index].FindControl("label2");
Response.Write(test);
Response.Write(name.Text);
}
}
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" Height="188px"
Width="127px" DataSourceID="SqlDataSource1"
onrowcommand="GridView1_RowCommand1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Add to Cart" />
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
------------------------------code-----------------------------
protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
Label txt = (Label)GridView1.Rows[index].FindControl("Label1");
string test = txt.Text;
Label name = (Label)GridView1.Rows[index].FindControl("label2");
Response.Write(test);
Response.Write(name.Text);
}
}
No comments:
Post a Comment