首页 > 解决方案 > 带有文本框和 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

谢谢

标签: asp.net

解决方案


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

推荐阅读