asp.net - 带有文本框和 SQL Server 数据库的 Gridview
问题描述
我正在使用 Visual Studio 和 VB.net 开发一个 Web 应用程序。我有一个带有文本框的网格视图。当我尝试从每一行的文本框中获取字符串来更新我的数据库时,虽然里面有东西,但它是空的。
我想知道为什么这是 gridview 中文本框的声明;
<asp:TemplateField HeaderText="TpNote">
<ItemTemplate>
<asp:TextBox ID="TpTextBox" runat="server" width="50px" BorderWidth="0px"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="CiNote">
<ItemTemplate>
<asp:TextBox ID="CiTextBox" runat="server" width="50px" BorderWidth="0px" AutoPostBack="False" CausesValidation="False" ClientIDMode="Inherit"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="CfNote">
<ItemTemplate>
<asp:TextBox ID="CfTextBox" runat="server" width="50px" BorderWidth="0px"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
下面是代码: Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim rqt1, rqt2, rqt3, rqt4 As String
con.Open()
Dim i As Integer
i = 0
Try
For Each row As GridViewRow In GridView1.Rows
Dim t As TextBox
Dim ccNote As String
t = CType(row.Cells(i).FindControl("CcTextBox"), TextBox)
ccNote = t.Text
Dim t1 As TextBox
Dim tpNote As String
t1 = CType(row.Cells(i).FindControl("TpTextBox"), TextBox)
tpNote = TextBox2.Text
Dim t2 As TextBox
Dim ciNote As String
t2 = CType(row.Cells(i).FindControl("CiTextBox"), TextBox)
ciNote = t2.Text
MsgBox(t2.Text)
Dim t3 As TextBox
Dim cfNote As String
t3 = CType(row.Cells(i).FindControl("CfTextBox"), TextBox)
cfNote = t3.Text
Dim matricule As String
Try
matricule = GridView1.Rows(i).Cells(0).Text
Catch ex As Exception
End Try
i = i + 1
rqt1 = "Update INSCRITMODULE set CcNote ='" + ccNote + "' where (INSCRITMODULE.Matricule)='" + matricule + "'"
rqt2 = "Update INSCRITMODULE set TpNote ='" + tpNote + "' where (INSCRITMODULE.Matricule)='" + matricule + "'"
rqt3 = "Update INSCRITMODULE set CiNote ='" + ciNote + "' where (INSCRITMODULE.Matricule)='" + matricule + "'"
rqt4 = "Update INSCRITMODULE set CfNote ='" + cfNote + "' where (INSCRITMODULE.Matricule)='" + matricule + "'"
Dim commande1 As New SqlCommand With {
.CommandText = rqt1,
.Connection = con
}
commande1.ExecuteNonQuery()
Dim commande2 As New SqlCommand With {
.CommandText = rqt2,
.Connection = con
}
commande2.ExecuteNonQuery()
Dim commande3 As New SqlCommand With {
.CommandText = rqt3,
.Connection = con
}
commande3.ExecuteNonQuery()
Dim commande4 As New SqlCommand With {
.CommandText = rqt4,
.Connection = con
}
commande4.ExecuteNonQuery()
Next
con.Close()
Catch ex As Exception
End Try
End Sub
谢谢
解决方案
I would suggest,
<asp:TemplateField HeaderText="TpNote">
<ItemTemplate>
<asp:TextBox ID="TpTextBox" runat="server" width="50px"
Text ='<%#Eval("CCNote")' BorderWidth="0px"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
similarly bind values all textboxes which you want to get value in code behind.
and then find textbox and get its value
推荐阅读
- javascript - 通过将属性相乘来更新数组总属性
- javascript - AWS Cognito:更改用户属性或删除用户如何需要密码?
- mapping - MRTK v2 空间映射隐藏多边形的可视化
- rust - 从先前完成的 Stream 的结果中产生一个 Stream
- google-mlkit - 如何使用 MLKit FaceDetector 跟踪图像的人脸
- python - Kivy在wi上返回黑屏
- android - 从中心而不是左上角设置视图的位置
- python - 使用 BeautifulSoup 以与 html 中相同的格式从 html 中提取文本
- android-intent - 无法将 MediaPlaybackService 与意图绑定并且无法检测到 MediaSessionCompat.Callbacks
- java - 如何使用 Talend 从 tOracleRow 组件执行 PLSQL 脚本?