How to get the value of a textbox that has been added to a template field in a gridview. VB.Net

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 used

ItemPrice = 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
Dan5150
12/23/2007 10:04:06 AM
asp.net.presentation-controls 72751 articles. 2 followers. Follow

7 Replies
744 Views

Similar Articles

[PageSpeed] 38

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
anas
12/23/2007 11:17:44 AM

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
Dan5150
12/23/2007 12:18:36 PM

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
hunterx1983
12/23/2007 1:34:55 PM

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
anas
12/23/2007 2:02:44 PM

<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
Dan5150
12/23/2007 2:39:49 PM

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
anas
12/23/2007 3:09:30 PM

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! Big Smile


Dan5150
www.5150creations.com
-1
Dan5150
12/23/2007 3:31:31 PM
Reply:

Similar Artilces:

How to add values to template control fields textbox control in GridView..
Hi all,    <asp:GridView ID="GridView1" EnableViewState="true" DataKeyNames="LicenseTemplateID"                       runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"                       Width="345px">                       <Columns>                           <asp:Templa...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

How to hide template field in GridView in VB.Net?
Hi,    I have a template field in gridview.   The template field is textbox.This template field is bound to a datafield.   I am binding data to the grid.   I need to have "100" in all textboxes when that textbox is visible.  I need this textbox to be invisible in some  rows and visible in some rows.  How to make the textbox invisible /visible while binding data to the gridview.  Please help in this.  Thanks a lot inadvance!!!  Lokanadham M.Hyderabad. GridView.Column[0].Visible =  false  Vikram www.vikraml...

How do I assign a value to a textbox in a template field in a gridview (VB)
I have a Gridview, containing a template column where each row contains a template field with a textbox and a commandfield with a selectbutton - the asp-stuff looks like this:  <asp:TemplateField HeaderText="Antal">    <ItemTemplate>        <asp:TextBox ID="txtAntal" runat="server" Width="50px"></asp:TextBox>    </ItemTemplate></asp:TemplateField><asp:CommandField SelectText="Tilføj" ShowSelectButton="True" >  &...

Get Value From A DropDownList Template Field In Gridview
Im just trying to call a value from a dropdownlist on a particular line in a gridview..    Ive tried this code, I know it must be simple... just cant figure it out.. Protected Sub gvProducts_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvProducts.RowCommand Dim SelectedRow As GridViewRow Dim intQuantity As Integer SelectedRow = Me.gvProducts.SelectedRow 'Message.Text = "You selected " & row.Cells(2).Text & "." intQuantity = SelectedRow.Cells(3).Text 'fetches the quantity value from the current row. lblTes...

GridView child of GridView
I have a Parent GridView and when you click in the Edit one of the columns has a Template Field which has a Child GridView. When you click Edit on the Child GridView I have a Template Field that has a TextBox. How to I retreive this value of the TextBox? GridViewParent-- GridViewChild (Edit - Template Field containing this GridViewChild)----- TextBox inside Template Field in GridViewChild. Thank you.Peace in ChristMarco Napolihttp://www.ourlovingmother.org What you can do as a solution is to override the RowDataBound and try to find the child GridView control. Once you get a reference to...

How to get cell values when GridView is bound with Template Control Fields
Hi, I have following GridView <asp:GridView ID="gdvProcAdmin" runat="server" AutoGenerateColumns="false" AllowSorting="true"> <Columns> <asp:TemplateField HeaderText="ID" Visible="false"> <ItemTemplate> <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Category" SortExpression="Type"> <ItemTemplate> <asp:Lab...

Sending SMS using vb.net or C#.net using vb.net or c#.net
Hi  My requirement is I hav one csv file with these fields id,mobilenum,messgae,status.intiallu staus is 0. once i read the all fileds and take that mobile number.using tat mobile number  i need to send sms .after sending sms i shuld change status as 1 How to send sms thru coding (please dont provide any links.if it is provide also please give working links becox i checked codeproject .i didnt get any nice link. and also provide the how to update the status field im csv file   Thank ssandhya   To send SMS, you need some third party SMS providers. if you consul...

VB.net add textbox values together to get total
Hi Guys Im using visual studio 2005 vb.net. sql express, im a newbie and im basically trying to create a customer calculator. Ive got this code, basically i want to add the integer values of each of my text boxes together and then display them in a textbox called textbox1.  I think i have the code write to do the calculation but im stuck on how to get it to display the total in the textbox once ive hit the calculate button, ive tried several ways, the page posts back, i get no errors but nothing in textbox1 toocan you take a look and help me out please. Thanks and Kind Regards &...

Trying to populate a drowonlist template field in a gridview in vb.net
I have a gird view and one my second colume is a template field.  The item template is a label but the edit is a dropdownlist.  I have used dropdownlist before in a gridview tempalte and added items to it in the aspx but this time I need to bind data from a data set into the drowdownlist.  I know how to do this but I do not know how to populate it when its a edit template.  If i just had a dropdownlist called ddl1 that was just on my web form i would do the following ddl1.datasource = getXYZ 'getXYZ is a call to a function that returns a dataset that i want to populate t...

Datagrid(.net 1.1) to GridView(.net 2.0) and using the RowUpdating event how to extract the values from the controls.
Ok  This is the settings: I use a Viewgrid with only itemtemplate colums for example. <asp:TemplateField HeaderText="Test"><ItemTemplate><asp:Label id="LBL_Test" text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' runat="server" meta:resourcekey="LBL_TestResource1"></asp:Label></font></ItemTemplate> <EditItemTemplate><asp:TextBox ID="HDN_Test" Text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' Runat="server" meta:resourcekey="HDN_TestResource1" /></EditItemTemplate></asp:TemplateField>  The ...

How to get the HREF value of the Anchor tag in a web page using the web browser control in vb.net
Hi Friends,               i have a webserver control in my application.i load the web browser control on a button click using the below code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click                AxWebBrowser1.Navigate("http://www.rateitall.com/i-322335-.aspx", False) End sub once the page is loaded in the browser i want the <a> HREF value in that page.For example if that page has ...

How to get RS (From VB6) to .NET (VB.NET)
Hi Friends,         I have requirement, there is one function in VB6 it returns a RS, I need to call that function in  VB.NET, how to get those values into .NET, if can any body can help (pass me some code snippets), that will be greate, Thank you.Madhu... -Madhu hey guys i got this one just by using fill method.-Madhu...

Both C# ASPX pages and VB.NET ASPX in single VB.NET Web Applcation Template
Hi.. Is it possible to have both c# pages and VB pages in same Webapplication, what i chose is VB.NET Web application Template. With Thanks, Jaya.... I don't see why you couldn't, but for the sake of consistency I don't know why you would want to do this. When you choose a C# or VB.NET application template(I'm assuming you're using VS or WebMatrix), it simply creates a .aspx file that includes a page directive that specifies the page language. If you created a C# template but want to use VB.Net instead, you need to go from: <%@ Page Language="C#" .... to...

Web resources about - How to get the value of a textbox that has been added to a template field in a gridview. VB.Net - asp.net.presentation-controls

Template - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Facebook page template & tips for 2014
As Facebook page admins get started with 2014, the social marketing experts at ShortStack created a detailed template , including image sizes, ...

Frame Artist Pro - Best Photo Collage Editor, Stitch Your Photos with Pic Frame Templates & Filter Effect ...
Get Frame Artist Pro - Best Photo Collage Editor, Stitch Your Photos with Pic Frame Templates & Filter Effect & Frames on the App Store. See ...

Paper wireframe templates - Flickr - Photo Sharing!
jasonrobb.com/v2/i/Wireframe-templates-JasonRobb.zip jasonrobb.com

United Airlines accidentally sends letter template to unhappy passenger
A US air passenger may have received what is quite possibly the world’s least sincere apology letter.

TV Templates - Transcripts
Lorem ipsum dolor sit amet, aihiad adaph dpas, osaf das. Oi asdfpo ijafj ai.

Mockup Apple Watch apps and compare screen sizes with these official bezel templates
... for developers, readers interested in Apple Watch can also use the images to compare the overall physical size of the two sizes. The templates ...

Today’s apps gone free: Practice+, Templates for Keynote, Dr. Panda’s Mailman and more
... and packages. Dr. Panda’s Postman is available for free for a limited time. It has a 4.5-star rating with a total of 44 ratings. Templates ...

Templates, Insights and Dictation in Google Docs
Google's standalone web apps for Docs , Sheets and Slides now show a list of templates you can choose to quickly create a document, spreadsheet ...

Yellen May Emulate Taper Template and Raise Rates in December
TIME Yellen May Emulate Taper Template and Raise Rates in December Bloomberg Federal Reserve Chair Janet Yellen shows signs of taking a page ...

Resources last updated: 11/28/2015 10:29:20 AM