在TextBox ASP.NET VB中显示GridView中的选定数据

时间:2016-04-10 13:45:16

标签: asp.net vb.net gridview

我正在使用VB处理ASP.NET,并通过将所选数据从网格视图显示到文本框来解决问题。 我不确定我的数据绑定到文本框是否正确,这是我的数据绑定到文本框的代码。对于网格视图,我只是直接从数据源窗口中进行选择。

aspx中的TextBox数据绑定代码:

<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>

我在网格视图上启用选择,以便在选择行时,其数据将显示在文本框中。

要在文本框中显示的代码:

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

这是我的视图页面,我希望数据显示在文本框中,以便我可以编辑它并更新到数据库。它也将用于删除。 但是当我点击链接SELECT时,它没有在文本框中显示数据。

Edit Pages View

我做错了什么?请帮帮我,谢谢。

1 个答案:

答案 0 :(得分:2)

首先,您可以从TextBox中删除绑定:

<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" />

然后,您可以在事件处理程序中移动代码并使用单元格索引来检索值:

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