2

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.

3
  • Can you run the code as written in your example? I think that Cells("StudentID") would give a compile error. Commented Apr 10, 2016 at 14:07
  • 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. Commented Apr 10, 2016 at 14:09
  • And you moved the code inside of GridView1_SelectedIndexChanged? Commented Apr 10, 2016 at 14:11

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.