这是我的 asp 代码:
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="Id" InsertVisible="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="firstname">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("firstname") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl" runat="server" Text='<%# Bind("firstname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
这是我的VB代码:
Imports System.Data.SqlClient
Imports System.Data
Partial Class testt
Inherits System.Web.UI.Page
Public connectionString As String = ConfigurationManager.ConnectionStrings("TestConnectionString").ConnectionString
Public cn As SqlConnection = New SqlConnection(connectionString)
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim ds As New DataSet
Dim dt As New DataTable
If cn.State = ConnectionState.Closed Then
cn.Open()
End If
Dim command As SqlCommand = New SqlCommand("selecttest", cn)
command.CommandType = CommandType.StoredProcedure
Dim da As New SqlDataAdapter(command)
da.Fill(ds, "testtt")
dt = ds.Tables(0)
GridView1.DataSource = dt
If Not IsPostBack Then
GridView1.DataBind()
End If
If cn.State = ConnectionState.Open Then
cn.Close()
End If
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim strPersonID As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox1"), TextBox).Text
Dim strLastName As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox2"), TextBox).Text
Try
Dim command As SqlCommand = New SqlCommand("updatetest", cn)
command.CommandType = CommandType.StoredProcedure
command.Parameters.AddWithValue("@pid", strPersonID)
command.Parameters.AddWithValue("@pfirstname", strLastName)
If cn.State = ConnectionState.Closed Then
cn.Open()
End If
command.ExecuteNonQuery()
MsgBox("1 row updated")
If cn.State = ConnectionState.Open Then
cn.Close()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub GridView1_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
If Not IsPostBack Then
GridView1.DataBind()
End If
End Sub
End Class
1-网格中的列是重复的,这意味着我从存储过程中获取选定的数据,并将数据绑定在新列中的模板字段中,因此我得到的不是只有 2 列,而是 4 具有相同的数据那么我该如何解决呢?
2-rowupdating 事件中没有错误一切正常,但数据库中的数据没有更新。请注意,存储过程在 SQL 中运行良好。这可能是因为前面提到的重复列...
有什么帮助吗??
谢谢你 !