您好我有一个搜索框,当我搜索记录时,记录显示很好但是一旦我点击gridview上的编辑页面进行回发并且所有记录显示备份,并选择第一条记录进行编辑。当我点击编辑所有记录不再显示时,如何禁用此帖子或进行此操作?
VB.CodeBehind
Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtSearch.text = ""
sourcegridview.DataBind()
End Sub
Protected Sub btnSearch_Click(sender As Object, e As System.EventArgs) Handles btnSearch.Click
SqlDataSource1.SelectCommand = "select * from Stg_Employee_All_Info where Name like '%" & txtSearch.Text & "%'"
SqlDataSource1.DataBind()
End Sub
Gridview代码
<asp:GridView ID="sourcegridview" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataSourceID="SqlDataSource1" Width="422px" AllowSorting="True"
DataKeyNames="Row">
<Columns>
<asp:BoundField DataField="Row" HeaderText="Row"
SortExpression="Row" ReadOnly="True" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True"
SortExpression="Name" />
<asp:BoundField DataField="Dept" HeaderText="Dept" ReadOnly="True"
SortExpression="Dept" />
<asp:TemplateField HeaderText="Hide_Bday">
<EditItemTemplate>
<asp:CheckBox ID="chkBday" runat="server" Checked='<%# Bind("Hide_Bday") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblHideBday" runat="server" Text='<%# Eval("Hide_Bday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hide_Anniv">
<EditItemTemplate>
<asp:CheckBox ID="chkAnniv" runat="server"
Checked='<%# Bind("Hide_Anniv") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblHideAnniv" runat="server" Text='<%# Eval("Hide_Anniv") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
<EmptyDataTemplate>
<asp:CheckBox ID="chkboxHideBday" runat="server"
Checked='<%# Eval("Hide_Bday") %>' Visible='<%# Eval("Hide_Bday") %>' />
</EmptyDataTemplate>
我使用引用存储过程的sqldatasource填充和更新gridview
Gridview填充代码
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:a_Kronos %>"
SelectCommand="SELECT [Row], [Name], [Dept], [Hide_Bday], [Hide_Anniv] FROM [Stg_Employee_All_Info]"
UpdateCommand="usp_insertoptout" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="Hide_Bday" Type="Int32" />
<asp:Parameter Name="Hide_Anniv" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
答案 0 :(得分:5)
问题是,当您单击搜索时,它会设置SelectCommand,但仅适用于服务器的单次往返。 SelectCommand不会在任何地方保留,因此当您单击“编辑”时,网格将立即返回使用SqlDataSource1的初始命令。
早期版本的ASP.NET GridView在ViewState中存储了SelectCommand文本,但出于安全原因,该文本已被删除。正如他们所说:
出于安全考虑,默认情况下,SqlDataSource控件不再在ViewState中存储命令。由于在技术上可以在客户端上解码ViewState的内容,因此在此字段中存储有关数据库后端的敏感信息可能会使您面临信息泄露威胁。
因此,您需要手动绑定Grid,并找到在多个post-backs之间保持SelectCommand的方法。以下是使用Session的示例:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
SqlDataSource1.SelectCommand = If(Session("SearchSelectCommand"), "SELECT [Row], [Name], [Dept], [Hide_Bday] ...")
SqlDataSource1.DataBind()
End Sub
Protected Sub btnSearch_Click(sender As Object, e As System.EventArgs) Handles btnSearch.Click
Dim searchQuery As String = "select * from Stg_Employee_All_Info where ..."
Session("SearchSelectCommand") = searchQuery
SqlDataSource1.SelectCommand = searchQuery
SqlDataSource1.DataBind()
End Sub