Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

GridView and TextBox

Hi I am really a beginner in ASP.NET so I am sorry for possible stupid question.
I have GridView on the form, inside GridView I have textbox, I am trying to get text from text box and for some reason I am getting empty string. Here is the code example:

.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server"
ID="test1"
AutoGenerateColumns="false"
Visible="true"
AllowPaging="true"
DataKeyNames="CompanyName"
ShowHeader="false"
ShowFooter="false">
<EmptyDataRowStyle BackColor="AliceBlue" />
<Columns >
<asp:BoundField DataField="CompanyName" ItemStyle-Width="200" />
<asp:TemplateField ItemStyle-Width="200" Visible="true" >
<ItemTemplate >
<asp:TextBox runat="server" id="txt1" Rows="3">
</asp:TextBox>
</ItemTemplate >
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button runat="server" ID="btText" />
</div>
</form>
</body>
</html>
.aspx.vb page
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim connection As SqlConnection = New SqlConnection("Persist Security Info=False;Integrated Security=true;Initial Catalog=Northwind;server=(local)")
Dim adapter As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerID,CompanyName,Address from Customers", connection)
Dim table As DataTable = New DataTable("Table")
Dim dataSet As DataSet = New DataSet("dataSet")
adapter.Fill(table)
Dim dv As DataView = table.DefaultView
test1.DataSource = dv
test1.DataBind()
End Sub
Protected Sub test1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles test1.PageIndexChanging
test1.PageIndex = e.NewPageIndex
test1.DataBind()
End Sub
Protected Sub btText_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btText.Click
For Each curRow As GridViewRow In test1.Rows
Dim strR As String = CType(curRow.FindControl("txt1"), TextBox).Text
Response.Write("TEST : " & strR & "<br>")
Next
End Sub
End Class
[3221 byte] By [michurin] at [2007-11-11 10:10:42]
# 1 Re: GridView and TextBox
The problem is that you're re-binding the GridView on every page load, so the value in the TextBox is lost. Try surrounding the code in your Page_Load event with an "If Not IsPostBack" test:

Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim connection As SqlConnection = ...
Phil Weber at 2007-11-11 23:12:09 >
# 2 Re: GridView and TextBox
Thank you. Works
michurin at 2007-11-11 23:13:03 >