Solved

Shopping Cart does read textbox value from gridview

Posted on 2008-06-21
4
3,013 Views
Last Modified: 2013-11-06
I have a gridview with a selection of databound items as well as a textbox and a button, the purpose of the textbox is to allow the user to enter the quantity required and then click on the button which will save the data and show the shopping cart. The textbox is in a itemtemplate tag, and the client doesn't want to select the row (ie edit mode) then add the value.

The problem is I can't get access to the textbox value I either get no value or an "object reference not found error" Surely there is some way of doing this? I have tried various methods ie :-
Dim tmpqty As TextBox = Me.GridView1.SelectedRow.Cells(4).Controls(0)
Dim tmpqty As TextBox = CType(Me.GridView1.FindControl("TextBox1"), TextBox)
etc
in various events BUT NOTHING WORKS HELP!!!

Imports System.Data.SqlClient
Imports System.Data
Partial Public Class OnDemandLine
    Inherits System.Web.UI.Page
 
    Public cnstring As String
 
    Private Sub OnDemandLine_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
 
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Select Case Request.Item("t")
            Case "ond"
                CType(Me.Master.FindControl("lblPageTitle"), Label).Text = "On Demand Product Lines"
                ViewState("type") = 3
            Case "bwrk"
                CType(Me.Master.FindControl("lblPageTitle"), Label).Text = "Bespoke Workbooks"
                ViewState("type") = 4
            Case "std", "pblc"
                Response.Redirect("standardproductline.aspx?t=" & Request.Item("t"))
            Case Else
                Response.Redirect("standardproductline.aspx?t=" & Request.Item("t"))
 
        End Select
 
        cnstring = ConfigurationManager.ConnectionStrings("Rogensi").ConnectionString
        If Not Me.IsPostBack Then
 
            binddata()
        End If
 
    End Sub
 
    Sub binddata()
        Dim cmd As Data.SqlClient.SqlCommand
        Dim da As New Data.SqlClient.SqlDataAdapter
        Dim ds As New DataSet
        Dim cn As New Data.SqlClient.SqlConnection
        cn.ConnectionString = cnstring
 
        cmd = New Data.SqlClient.SqlCommand
        cmd.CommandText = "showproductline"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = cn
 
        Dim pagep As New SqlParameter("@type", ViewState("type")) 'ViewState("id")) '
        cmd.Parameters.Add(pagep)
 
        da.SelectCommand = cmd
        da.Fill(ds)
 
        Me.GridView1.DataSource = ds.Tables(0)
        Me.GridView1.DataBind()
 
 
        da.Dispose()
        cmd.Dispose()
        cn.Close()
    End Sub
 
--------------ASPX page---------------
<%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/RogenSi/Site1.Master" CodeBehind="OnDemandLine.aspx.vb" Inherits="RogenSi.OnDemandLine" 
    title="On Product Line" %>
<%@ MasterType VirtualPath="~/RogenSi/Site1.Master" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="None" GridLines="None" HorizontalAlign="Left" DataKeyNames="id">
        <Columns>
            <asp:ImageField DataAlternateTextField="title" DataImageUrlField="image" DataImageUrlFormatString="~/siteimages/{0}" HeaderText="Item">
                <ItemStyle Height="50px" Width="50px" />
            </asp:ImageField>
            <asp:TemplateField HeaderText="Short Description" SortExpression="SDescription">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Title") %>'></asp:Label><br />
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("SDescription") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" SortExpression="UnitPrice" />
            <asp:BoundField DataField="StockLevel" HeaderText="Stock Qty" SortExpression="StockLevel" />
            <asp:TemplateField HeaderText="Order Qty">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Width="50px"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:ButtonField ButtonType="Button" CommandName="select" Text="ADD" />
            <asp:BoundField DataField="LeadTime" HeaderText="LeadTime" SortExpression="LeadTime" Visible="False" />
            <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                SortExpression="id" Visible="False" />
            <asp:BoundField DataField="ProductType" HeaderText="ProductType" SortExpression="ProductType"
                Visible="False" />
            <asp:BoundField DataField="ReStockLevel" HeaderText="ReStockLevel" SortExpression="ReStockLevel" Visible="False" />
        </Columns>
        <EmptyDataTemplate>
            Sorry there is no data for this category
        </EmptyDataTemplate>
    </asp:GridView><br />
    <asp:Label ID="Label3" runat="server" Text="Label4"></asp:Label>
 
</asp:Content>

Open in new window

0
Comment
Question by:markej
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 7

Accepted Solution

by:
kGenius earned 500 total points
ID: 21837364
Make sure the row is selected before searching for the textbox.
Then in the SelectedIndexChanged event of your gridview add
Dim tmpqty As TextBox = CType(e.row(index).FindControl("TextBox1"), TextBox)

(hope the syntax is correct, as I never use VB)

HTH
Karl
0
 

Author Comment

by:markej
ID: 21837600
How do I select the Row, The only selection type control is the "add" button?
0
 

Author Comment

by:markej
ID: 21837637
The actual syntax is this and it seems to work So Thanks!

Mark

Private Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

        Dim tmpqty As TextBox = CType(Me.GridView1.SelectedRow.FindControl("TextBox1"), TextBox)
        Response.Redirect("orderdetail.aspx?qty=" & tmpqty.Text)
   
End Sub
0
 

Author Closing Comment

by:markej
ID: 31469401
Thanks for the soln, the syntax was slightly out BUT you gave me the correct pointer/idea

Mark
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help with a query 14 39
Blending HTML output from R Studio into a ASP.Net/C# project 4 23
c#, datatable, aspx 4 23
reading an excel file vb.net 2 20
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question