本文介绍了找到具有相同ID"lblCustomerID"的多个控件. FindControl要求控件具有唯一的ID.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
前端代码
front end code
<asp:TemplateField HeaderText="CustomerID" >
<ItemTemplate>
<asp:Label runat="server" ID="lblCustomerID" Text='<%# Eval("CustomerID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="TxtCustomerID" Text='<%# Eval("CustomerID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" >
<ItemTemplate><asp:Label runat="server" ID="lblCustomerID" Text='<%# Eval("Name") %>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtName" Text='<%# Eval("Name") %>'></asp:TextBox></EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company" >
<ItemTemplate><asp:Label runat="server" ID="lblCustomerID" Text='<%# Eval("Company") %>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCompany" Text='<%# Eval("Company") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
后端代码
Backend code
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim CustomerID As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtCustomerID"), TextBox).Text
Dim Name As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtName"), TextBox).Text
Dim Company As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtCompany"), TextBox).Text
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("connectionstring").ConnectionString)
con.Open()
Dim update As String = "update customer set Name=@Name,Company=@Company where CustomerID=@CustomerID" & " select * from customer"
Dim com As New SqlCommand(update, con)
com.Parameters.AddWithValue("@Name", Name)
com.Parameters.AddWithValue("@Company", Company)
com.Parameters.AddWithValue("@CustomerID", CustomerID)
Dim adp As SqlDataAdapter = New SqlDataAdapter(com)
Dim dt As New DataTable
adp.Fill(dt)
adp.UpdateCommand.Parameters.AddWithValue("@Name", Name)
adp.UpdateCommand.Parameters.AddWithValue("@Company", Company)
adp.UpdateCommand.Parameters.AddWithValue("@CustomerID", CustomerID)
GridView1.EditIndex = -1
con.Close()
BindData()
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("connectionstring").ConnectionString)
Dim CustomerID As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("lblCustomerID")TextBox).Text
con.Open()
Dim delete As String = "delete from Customer where CustomerID=@CustomerID " & "select * from Customer "
Dim cmd As New SqlCommand(delete, con)
cmd.Parameters.AddWithValue("@CustomerID", CustomerID)
con.Close()
End Sub
在删除Gridview时遇到错误,我突出显示并加了下划线,提示我出现错误
Getting an error in Gridview deleting i have highlighted and underlined where i m getting an error
推荐答案
<asp:TemplateField HeaderText="Name" >
<ItemTemplate><asp:Label runat="server" ID="lblCustomerID" Text='<%# Eval("Name") %>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtName" Text='<%# Eval("Name") %>'></asp:TextBox></EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company" >
<ItemTemplate><asp:Label runat="server" ID="lblCustomerID" Text='<%# Eval("Company") %>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCompany" Text='<%# Eval("Company") %>'></asp:TextBox>
</EditItemTemplate>
变成类似lblName或lblCompany
into something unique like lblName or lblCompany
这篇关于找到具有相同ID"lblCustomerID"的多个控件. FindControl要求控件具有唯一的ID.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!