ASP.NET Visual Basic 我正在尝试更新 GridView 中显示的已连接 SQL 表的数据。但我的 Visual Basic 代码在 GridView 中找不到 VRMModel-TextBox。我可以在GridView中看到数据。我单击更新数据,它找到 RowIndex,但找不到 TextBox 或 GridView-Control。它将 VRMModel 和 ComponentDefect 的值响应为 NOTHING。这是我的代码:
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
Dim VRMModel As TextBox = CType(row.FindControl("VRMModel"), TextBox)
Dim ComponentDefect As TextBox = CType(row.FindControl("ComponentDefect"), TextBox)
Dim connectionString As String = ConfigurationManager.ConnectionStrings("VRM_Test").ConnectionString
Using connection As New SqlConnection(connectionString)
Using command As New SqlCommand("UPDATE tblVRMModel SET VRMModel = @VRMModel, ComponentDefect = @ComponentDefect ", connection)
command.Parameters.AddWithValue("@VRMModel", VRMModel)
command.Parameters.AddWithValue("@ComponentDefect", ComponentDefect)
connection.Open()
command.ExecuteNonQuery()
End Using
End Using
GridView1.EditIndex = -1
BindGrid()
End Sub
这里缺少什么?
我认为问题发生在以下部分:
Dim VRMModel As TextBox = CType(row.FindControl("VRMModel"), TextBox)
Dim ComponentDefect As TextBox = CType(row.FindControl("ComponentDefect"), TextBox)
它没有找到任何控件(GridView 的单元格)将其声明为 TextBox 来获取其值。
为什么不考虑在 GV 底部放置一个保存按钮,然后允许用户自由切换和编辑任何行(就像在 Excel 中,甚至在桌面数据网格中一样)。因此,用户可以跳来跳去、跳来跳去并对所有行进行更改,然后我们就有一个保存按钮。
上面不仅呈现了一种用于编辑的常见 UI 类型,而且还省去了复杂的 UI,也省去了 GridView 的大量复杂代码和技巧事件。只需转储所有 GridView 事件,并编写一些干净且易于阅读的代码即可。
因此,说出这个标记:
<style>
.borderhide input {
border: none;
background-color: transparent
}
.borderhide textarea {
border: none;
background-color: transparent
}
</style>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="ID"
CssClass="table table-hover borderhide" Width="50%">
<Columns>
<asp:TemplateField HeaderText="First"><ItemTemplate>
<asp:TextBox ID="txtFirst" runat="server" Text='<%#Eval("FirstName") %>' >
</asp:TextBox>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Last"><ItemTemplate>
<asp:TextBox ID="txtLast" runat="server" Text='<%#Eval("LastName") %>' >
</asp:TextBox>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="HotelName"><ItemTemplate>
<asp:TextBox ID="txtHotel" runat="server" Text='<%#Eval("HotelName") %>' >
</asp:TextBox>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="txtDescription"
runat="server" TextMode="MultiLine"
Text='<%# Eval("Description") %>'
Height="60px" Width="330px">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="cmdSave" runat="server" Text="Save/Done"
CssClass="btn btn-dark"
OnClick="cmdSave_Click" />
我们背后的代码也相当简单。
Dim rstHotels As New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadData()
Session("rstHotels") = rstHotels
Else
rstHotels = Session("rstHotels")
End If
End Sub
Sub LoadData()
Dim strSQL =
"SELECT * FROM tblHotelsA
WHERE Active = 1
ORDER BY HotelName"
rstHotels = MyRst(strSQL)
GridView1.DataSource = rstHotels
GridView1.DataBind()
End Sub
Sub GridToTable()
' send grid rows back to persisted table
For Each gRow As GridViewRow In GridView1.Rows
If gRow.RowType = DataControlRowType.DataRow Then
Dim OneRow As DataRow = rstHotels.Rows(gRow.DataItemIndex)
OneRow("FirstName") = DirectCast(gRow.FindControl("txtFirst"), TextBox).Text
OneRow("LastName") = DirectCast(gRow.FindControl("txtLast"), TextBox).Text
OneRow("HotelName") = DirectCast(gRow.FindControl("txtHotel"), TextBox).Text
OneRow("Description") = DirectCast(gRow.FindControl("txtDescription"), TextBox).Text
End If
Next
End Sub
Protected Sub cmdSave_Click(sender As Object, e As EventArgs)
GridToTable()
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand("SELECT * FROM tblHotelsA", conn)
Dim da As New SqlDataAdapter(cmdSQL)
Dim daU As New SqlCommandBuilder(da)
conn.Open()
da.Update(rstHotels)
End Using
End Using
' at this point all changes saved to database
' we no doubt at this point will navagate to another page.
'Response.Redirect("Some other page.aspx")
End Sub
所以,最终结果是这样的:
因此,用户可以进行切换、进行更改。然后我们有一个保存按钮,而且我们也只有一个保存操作,用于将结果发送回数据库。