我的搜索按钮链接到一个 GridviewW,它的每一行都有一个编辑按钮。当我按下搜索按钮时,数据发生变化并发生 databind()。之后,如果我尝试使用编辑按钮,它会显示另一行进行编辑,而不是选定的行(最大的问题)。单独测试时,两个按钮都可以正常工作,但不能一个接一个。我解决了GridView1.DataBind()
从编辑按钮事件中删除的问题,但随后需要单击 2 次才能显示编辑模板(另一个问题)。
编辑:我想问题出在搜索按钮上。你能给出一个不依赖于sqldatasource的好的搜索代码吗?
'Where data is loaded into GV
Dim SqlDataSource1 As New SqlDataSource
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SqlDataSource1.SelectCommand = "SELECT * FROM [TABLE]"
SqlDataSource1.ConnectionString = "Conn String"
If Not IsPostBack Then
Dim conn As New SqlConnection
conn.ConnectionString = con.GetConnectionString
Dim cmd As New SqlCommand()
cmd.CommandText = "SELECT [AREA], [LEADER_USER] FROM [AREA]"
cmd.CommandType = CommandType.Text
cmd.Connection = conn
conn.Open()
Dim adpt As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
adpt.Fill(ds)
GridView1.DataSource = ds
GridView1.DataBind()
conn.Close()
End If
End Sub
'Search button
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Try
SqlDataSource1.SelectCommand = "SELECT * FROM TABLE WHERE id LIKE @id"
SqlDataSource1.SelectParameters.Clear()
SqlDataSource1.SelectParameters.Add(New Parameter("id", DbType.String, "%" + txtSearch.Text + "%"))
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
'Edit button
Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()
End Sub
标记:
<asp:TextBox ID="TextBox1" runat="server" BackColor="#D9ECFF"
style="height: 20px; width: 186px" AutoPostBack="True"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" BackColor="#0066cc"
BorderColor="#0066cc" BorderStyle="Outset" Font-Bold="True" ForeColor="White"
style=" height: 26px; width: 56px" Text="Search" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" OnRowEditing="EditRow"
OnRowCancelingEdit="CancelEditRow" DataKeyNames="AREA" DataMember="DefaultView">
<Columns>
<asp:BoundField DataField="AREA" HeaderText="AREA" ReadOnly="True"
SortExpression="AREA" />
<asp:TemplateField HeaderText="LEADER_USER" SortExpression="LEADER_USER">
<ItemTemplate><%#Eval("leader_user")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtleaderuser" runat="server" Text='<%#Eval("leader_user")%>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="editButton" runat="server" CommandName="Edit"
ImageUrl="images/pencil1.png" Text="Edit" ToolTip="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" CommandName="Update"
Text="Update" />
<asp:Button ID="BtnCancel" runat="server" CommandName="Cancel"
Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
请帮我。这 2 个 gridview 功能应该如何编码以协同工作?这是我知道如何做到这一点的唯一方法,但如果可行,任何想法对我来说都可以。如果你是 C# 人,你可以使用 C#-to VB 转换器:http: //www.developerfusion.com/tools/convert/csharp-to-vb/