Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Datagrid Control (http://www.velocityreviews.com/forums/f60-asp-net-datagrid-control.html)
-   -   Problem with textbox in template column and databind (http://www.velocityreviews.com/forums/t760919-problem-with-textbox-in-template-column-and-databind.html)

Nathan Nichols 08-04-2004 06:56 AM

Problem with textbox in template column and databind
 
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.CloseConne ction)
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


All times are GMT. The time now is 09:18 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.