I have a gridview that shows products, description, price. I have added a Buttonfield and a TemplateField that has a Textbox with the id QuantityTextBox.
What I am trying to do is get the value of the Quantity TextBox in the RowCommand event of the gridview. I can get the price but need to get the quantity also. To get the price I usedItemPrice = Server.HtmlDecode(row.Cells(5).Text)
My template field is cell 6 but the above routine does'nt work.
Any help would be greatly appreciated.
Dan5150
www.5150creations.com
![]() |
-1 |
![]() |
Hi
use this:
VB:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand Dim QuantityTextBox As TextBox = TryCast(TryCast(e.CommandSource, Control).NamingContainer.FindControl("QuantityTextBox"), TextBox) Dim Quantity As Integer = QuantityTextBox.Text End Sub
C#:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
TextBox QuantityTextBox = (e.CommandSource as Control).NamingContainer.FindControl("QuantityTextBox") as TextBox;
int Quantity = QuantityTextBox.Text;
}
Regards,
Anas Ghanem.
Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!
![]() |
-1 |
![]() |
I tried the vb.net code and get an error. If I change Quantity to a string I get a 0 everytime, no matter what I put in the textbox.
Dan5150
www.5150creations.com
![]() |
-1 |
![]() |
hi,
i think your problem is u added your textbox control in ItemTemplate in TemplateField, when GridView renders itself , many copy of TextBox control was copied
so, in your RowCommand Event, u must detect which row is being active ( or which TextBox is actived )
it is simply if u add Select command name in ButtonField or set CommandArg = RowIndex,
Hope it help
if i wrong, plz dont hate my answer, thanks
Ultimate Guide to Link Building - What you always want to know!
![]() |
1 |
![]() |
Hi Dan5150,
Can you paste the ASPX code of the GridView ?
Regards,
Anas Ghanem.
Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!
![]() |
1 |
![]() |
<asp:GridView ID="ItemsGridView" runat="server" AutoGenerateColumns="False"
DataKeyNames="EventId" DataSourceID="ObjectDataSource_Items" AllowPaging="True" PageSize="5">
<Columns>
<asp:BoundField DataField="EventId" HeaderText="EventId" SortExpression="EventId" />
<asp:BoundField DataField="OptionID" HeaderText="OptionID" SortExpression="OptionID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Size" HeaderText="Size" SortExpression="Size" />
<asp:BoundField DataField="Price" DataFormatString="{0:c}" HeaderText="Price" HtmlEncode="False"
SortExpression="Price" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="QuantityTextBox" runat="server" Width="50px">1</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" CommandName="Add" Text="Add">
<ControlStyle Font-Bold="True" />
</asp:ButtonField>
</Columns>
</asp:GridView>
Dan5150
www.5150creations.com
![]() |
-1 |
![]() |
Hi
change the GridView ASPX code to this:
<asp:GridView ID="ItemsGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="EventId" DataSourceID="ObjectDataSource_Items" AllowPaging="True" PageSize="5"> <Columns> <asp:BoundField DataField="EventId" HeaderText="EventId" SortExpression="EventId" /> <asp:BoundField DataField="OptionID" HeaderText="OptionID" SortExpression="OptionID" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /> <asp:BoundField DataField="Size" HeaderText="Size" SortExpression="Size" /> <asp:BoundField DataField="Price" DataFormatString="{0:c}" HeaderText="Price" HtmlEncode="False" SortExpression="Price" /> <asp:TemplateField HeaderText="Quantity"> <ItemTemplate> <asp:TextBox ID="QuantityTextBox" runat="server" Width="50px">1</asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Button ID="btnAdd" Font-Bold="true" runat="server" CommandName="Add" Text="Add" CommandArgument='<%# DataBinder.Eval(Container,"RowIndex") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
and in item_Command :
Protected Sub ItemsGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand Dim rowId As Integer = e.CommandArgument Dim QuantityTextBox As TextBox = ItemsGridView.Rows(rowId).FindControl("QuantityTextBox") Dim Quantity As Integer = QuantityTextBox.Text End Sub
Regards,
Anas Ghanem.
Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!
![]() |
1 |
![]() |
It worked perfectly! Thanks for all you help Anas. I have been looking through post after post on the internet trying to find the answer to this. None of the other post mentioned the CommandArgument for the button. Thanks again!
Dan5150
www.5150creations.com
![]() |
-1 |
![]() |