Problem with textbox in template column and databind

Discussion in 'ASP .Net Datagrid Control' started by Nathan Nichols, Aug 4, 2004.

  1. I am having a problem with the datagrid and a template column that is
    giving me fits.

    In the HTML, I have the grid defined at design time as just having one
    template column that contains a text box named txtQty.

    At runtime, I add the bound fields. (I'm doing this at runtime
    because there will be a situation where I will have different bound
    fields, but I've left that out to simplify the code for this message).

    The behavior I would like to have is that the user fills in a quantity
    in the text boxes. When cmdAdd is clicked, I want to iterate through
    each txtQty, and perform a database insert for each one that has a
    numeric value greater than 0.

    I am running into a problem where all bound columns disappear after
    cmdAdd is clicked, unless I bind the grid again in the cmdAdd_click
    sub (see comment in code sample). However, when I start doing the
    databind in this sub, I lose the value of txtQty as soon as the grid
    is bound. So, I have a situation where I can do another databind in
    the cmdAdd_click sub and get all of my bound fields accessible and
    lose my txtQty values, or not do a databind and lose the bound columns
    but keep txtQty values.

    Anyone have any suggestions? Any help would be greatly appreciated!




    <asp:datagrid id="dgCatalog" runat="server"
    AutoGenerateColumns="False">
    <Columns>
    <asp:TemplateColumn HeaderText="Quantity">
    <ItemTemplate>
    <asp:TextBox id="txtQty" runat="server"
    Width="42px"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    If Not IsPostBack() Then
    bindDataGrid()
    End If
    End Sub

    Private Sub bindDataGrid()

    dgCatalog.DataKeyField = "kid"

    Dim datagridcol0 As New BoundColumn
    datagridcol0.HeaderText = "ID"
    datagridcol0.Visible = False
    datagridcol0.DataField = "kid"
    datagridcol0.ReadOnly = True
    dgCatalog.Columns.Add(datagridcol0)

    Dim datagridcol1 As New BoundColumn
    datagridcol1.HeaderText = "Kit Name"
    datagridcol1.DataField = "kitname"
    datagridcol0.ReadOnly = True
    dgCatalog.Columns.Add(datagridcol1)

    Dim datagridcol2 As New BoundColumn
    datagridcol2.HeaderText = "Description"
    datagridcol2.DataField = "description"
    datagridcol0.ReadOnly = True
    dgCatalog.Columns.Add(datagridcol2)

    Dim datagridcol3 As New BoundColumn
    datagridcol3.HeaderText = "Price"
    datagridcol3.DataField = "price"
    datagridcol3.DataFormatString = "{0:F2}"
    datagridcol3.ItemStyle.HorizontalAlign = HorizontalAlign.Right
    datagridcol0.ReadOnly = True
    dgCatalog.Columns.Add(datagridcol3)

    Dim myConnection As New
    SqlConnection(CType(Application("connectString"), String))

    Dim myCommand As New SqlCommand("get_kit_catalog",
    myConnection)
    myCommand.CommandType = CommandType.StoredProcedure

    myConnection.Open()

    dgCatalog.DataSource =
    myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    dgCatalog.DataBind()

    myConnection.close()

    End Sub

    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles cmdAdd.Click

    Dim ctl As Control
    Dim textView As TextBox
    Dim textEdit As TextBox
    Dim dgItem As DataGridItem

    ' If the grid is bound at this point, all of the bound columns
    ' are accessible, but the text box txtQty is cleared.
    '
    ' If the grid is NOT bound, bound columns are not available,
    but
    ' the value of txtQty is accessible.

    bindDataGrid()

    For Each dgItem In dgCatalog.Items

    'Response.Write("0: " + dgItem.Cells(0).Text + "<BR>")
    'Response.Write("1: " + dgItem.Cells(1).Text + "<BR>")
    'Response.Write("2: " + dgItem.Cells(2).Text + "<BR>")
    'Response.Write("3: " + dgItem.Cells(3).Text + "<BR>")
    'Response.Write("4: " + dgItem.Cells(4).Text + "<BR>")

    ctl = dgItem.FindControl("txtQty")

    If Not ctl Is Nothing Then
    textEdit = CType(ctl, TextBox)
    Response.Write(textEdit.Text)
    End If
    Next

    End Sub
     
    Nathan Nichols, Aug 4, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S2FybCAxNDAuNg==?=

    ASP.NET 2.0 Databind To Textbox

    =?Utf-8?B?S2FybCAxNDAuNg==?=, May 31, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    4,059
    =?Utf-8?B?S2FybCAxNDAuNg==?=
    May 31, 2006
  2. =?Utf-8?B?UXVhbiBOZ3V5ZW4=?=

    Databind problem with DetailsView's template fields

    =?Utf-8?B?UXVhbiBOZ3V5ZW4=?=, Aug 27, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    540
    =?Utf-8?B?UXVhbiBOZ3V5ZW4=?=
    Aug 27, 2007
  3. Finn Stampe Mikkelsen

    Column count / Access GridView Column after DataBind

    Finn Stampe Mikkelsen, Aug 19, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    3,207
    Finn Stampe Mikkelsen
    Aug 20, 2010
  4. Michael
    Replies:
    0
    Views:
    229
    Michael
    Dec 29, 2003
  5. David

    manually created template column databind

    David, Jan 22, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    149
    David
    Jan 22, 2005
Loading...

Share This Page