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.
What did I do wrong? Please help me, thank you.
Cells("StudentID")
would give a compile error. – ConnorsFanGridView1_SelectedIndexChanged
? – ConnorsFan