2
votes

I was working on ASP.NET using VB and encountered the problem by showing the selected data from grid view to text box. I am not sure whether I do my data binding to textbox is correct or not, here is my code for data binding to text box. For grid view I just direct choose from the data source window.

TextBox data binding code in aspx:

<asp:TextBox Text='<%# Bind("TextData")%>' ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox Text='<%# Bind("TextData")%>' ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox Text='<%# Bind("TextData")%>' ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox Text='<%# Bind("TextData")%>' ID="TextBox4" runat="server"></asp:TextBox>
<asp:TextBox Text='<%# Bind("TextData")%>' ID="TextBox5" runat="server"></asp:TextBox>
<asp:TextBox Text='<%# Bind("TextData")%>' ID="TextBox6" runat="server"></asp:TextBox>
<asp:TextBox Text='<%# Bind("TextData")%>' ID="TextBox7" runat="server"></asp:TextBox>
<asp:TextBox Text='<%# Bind("TextData")%>' ID="TextBox8" runat="server"></asp:TextBox>

I enable the selection on the grid view so that when the row is selected, its data will show in the text box.

The code to show in the text box:

Imports System.Data.SqlClient
Public Class Edit
Inherits System.Web.UI.Page
Public con As New SqlConnection("Data Source=localhost;Initial Catalog=Tuition_Information;Integrated Security=True")
Public cmd As New Data.SqlClient.SqlCommand

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged

End Sub

Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
    Dim row As GridViewRow = GridView1.SelectedRow
    TextBox1.Text = row.Cells("StudentID").Text
    TextBox2.Text = row.Cells("StudentName").Text
    TextBox3.Text = row.Cells("HomeAddress").Text
    TextBox4.Text = row.Cells("ContactNumber").Text
    TextBox5.Text = row.Cells("SubjectCode").Text
    TextBox6.Text = row.Cells("SubjectName").Text
    TextBox7.Text = row.Cells("ParentName").Text
    TextBox8.Text = row.Cells("ParentContact").Text
End Sub
End Class

Here is my pages for the view, I want the data show in the textbox so that I can edit it and update to the database. It will also used in delete too. But when I click the link SELECT, it did not show the data in textbox.

Edit Pages View

What did I do wrong? Please help me, thank you.

1
Can you run the code as written in your example? I think that Cells("StudentID") would give a compile error.ConnorsFan
Yes I can run, I try this also : TextBox1.Text = row.Cells(0).Text , but both of them did not show data in the textbox.Silver Archer
And you moved the code inside of GridView1_SelectedIndexChanged?ConnorsFan

1 Answers

2
votes

First, you can remove the binding from your TextBoxes:

<asp:TextBox ID="TextBox1" runat="server" />
<asp:TextBox ID="TextBox2" runat="server" />
<asp:TextBox ID="TextBox3" runat="server" />
<asp:TextBox ID="TextBox4" runat="server" />
<asp:TextBox ID="TextBox5" runat="server" />
<asp:TextBox ID="TextBox6" runat="server" />
<asp:TextBox ID="TextBox7" runat="server" />
<asp:TextBox ID="TextBox8" runat="server" />

Then you can move the code inside your event handler and use the cell indexes to retrieve the values:

Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
    Dim row As GridViewRow = GridView1.SelectedRow
    TextBox1.Text = row.Cells(1).Text
    TextBox2.Text = row.Cells(2).Text
    TextBox3.Text = row.Cells(3).Text
    TextBox4.Text = row.Cells(4).Text
    TextBox5.Text = row.Cells(5).Text
    TextBox6.Text = row.Cells(6).Text
    TextBox7.Text = row.Cells(7).Text
    TextBox8.Text = row.Cells(8).Text
End Sub