带有文本框和SQL Server数据库的Gridview

时间:2019-03-29 13:04:04

标签: asp.net

我正在使用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>

这是背后的代码: 受保护的Sub Button2_Click(发送者作为对象,e作为EventArgs)处理Button2.Click         将Dim rqt1,rqt2,rqt3,rqt4转换为字符串

    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

谢谢

2 个答案:

答案 0 :(得分:0)

我建议先调试代码,然后尝试确保每个asp文本框值是否都到达t1,t2,.....作为后端,然后我们才能弄清其余内容。

答案 1 :(得分:0)

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