VB GridView的选择错了行编辑 [英] VB Gridview selects wrong row for editing

查看:328
本文介绍了VB GridView的选择错了行编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个搜索框,当我搜索记录它是好的,不过记录显示出来,一旦我点击页面做了回发GridView的编辑和所有的记录显示,备份与选中的编辑第一条记录。如何禁用这个帖子后面,或使其当我点击编辑所有的记录不会再次显示?

VB。codeBehind

 保护小组btnClear_Click(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手btnClear.Click
    txtSearch.text =
    sourcegridview.DataBind()结束小组保护小组btnSearch_Click(发送者为对象,E作为System.EventArgs)把手btnSearch.Click
    SqlDataSource1.SelectCommand =SELECT * FROM Stg_Employee_All_Info其中Name LIKE'%&放大器; txtSearch.Text&安培; %'
    SqlDataSource1.DataBind()
结束小组

的GridView code

 < ASP:GridView控件ID =sourcegridview=服务器AllowPaging =真
        的AutoGenerateColumns =FALSE背景色=白BORDERCOLOR =#3366CC
        边框样式=无边框宽度=1px的CELLPADDING =4
        的DataSourceID =SqlDataSource1WIDTH =422pxAllowSorting =真
        的DataKeyNames =行>
        <柱体和GT;
            < ASP:BoundField的数据字段=行的HeaderText =行
                SORTEX pression =行只读=真/>
            < ASP:BoundField的数据字段=名称的HeaderText =名称只读=真
                SORTEX pression =名称/>
            < ASP:BoundField的数据字段=部门的HeaderText =部门只读=真
                SORTEX pression =部/>
            < ASP:的TemplateField的HeaderText =Hide_Bday>
                <&EditItemTemplate的GT;
                    < ASP:复选框ID =chkBday=服务器选中='<%#绑定(Hide_Bday)%>' />
                < / EditItemTemplate中>
                <&ItemTemplate中GT;
                    < ASP:标签ID =lblHideBday=服务器文本='<%#的eval(Hide_Bday)%>'>< / ASP:标签>
                < / ItemTemplate中>
            < / ASP:的TemplateField>
            < ASP:的TemplateField的HeaderText =Hide_Anniv>
                <&EditItemTemplate的GT;
                    < ASP:复选框ID =chkAnniv=服务器
                        选中='<%#绑定(Hide_Anniv)%>' />
                < / EditItemTemplate中>
                <&ItemTemplate中GT;
                    < ASP:标签ID =lblHideAnniv=服务器文本='<%#的eval(Hide_Anniv)%>'>< / ASP:标签>
                < / ItemTemplate中>
            < / ASP:的TemplateField>
            < ASP:CommandField中ShowEditButton =真/>
        < /专栏>
        < EmptyDataTemplate>
            < ASP:复选框ID =chkboxHideBday=服务器
                选中='<%#的eval(Hide_Bday)%>'可见='<%#的eval(Hide_Bday)%>' />
        < / EmptyDataTemplate>

我充实和更新与引用一个存储过程一个SqlDataSource GridView的

的GridView填充code

 < ASP:SqlDataSource的ID =SqlDataSource1=服务器
    的ConnectionString =下;%$的ConnectionStrings:a_Kronos%>中
    的SelectCommand =SELECT [行],[名],[部],[Hide_Bday],[Hide_Anniv] FROM [Stg_Employee_All_Info]
    更新命令=usp_insertoptoutUpdateCommandType =StoredProcedure的>
    < UpdateParameters>
        < ASP:参数名称=Hide_BdayTYPE =的Int32/>
        < ASP:参数名称=Hide_AnnivTYPE =的Int32/>
    < / UpdateParameters>
< / ASP:SqlDataSource的>


解决方案

问题是,当你在搜索点击设置的SelectCommand,但只对单次往返到服务器。 SelectCommand中没有任何地方,所以坚持,当你点击编辑,电网进入右后卫使用SqlDataSource1的初始命令。

ASP.NET的GridView控件的

早期版本存储在ViewState中SelectCommand中的文本,但是这是出于安全原因删除。正如他们所说的:


  

为了安全起见,SqlDataSource控件不再存储在ViewState中默认命令。既然是技术上可以去客户端上的code ViewState中的内容,存储有关这一领域的数据库后端的敏感信息可能暴露你的信息泄露威胁。


所以取而代之,你需要将网格手动绑定,并找到自己坚持在多个后背上,SelectCommand中的方式。下面是一个例子使用Session:

 保护小组的Page_Load(发送者为对象,E作为System.EventArgs)把手Me.Load
    SqlDataSource1.SelectCommand = IF(会话(SearchSelectCommand),选择[行],[名],[部],[Hide_Bday] ...)
    SqlDataSource1.DataBind()
结束小组保护小组btnSearch_Click(发送者为对象,E作为System.EventArgs)把手btnSearch.Click
    昏暗SEARCHQUERY作为字符串=从Stg_Employee_All_Info那里选择* ...
    会议(SearchSelectCommand)= SEARCHQUERY
    SqlDataSource1.SelectCommand = SEARCHQUERY
    SqlDataSource1.DataBind()
结束小组

Hello I have a searchbox and when I search for a record it is fine the records shows up however once I click edit on the gridview the page does a postback and all the records show back up with the first record selected for editing. How can I disable this post back or make it when I click edit all the records do not display again?

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 Code

<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>

I populate and update the gridview with a sqldatasource which references a stored procedure

Gridview populate code

<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>

解决方案

The problem is when you click on search it sets the SelectCommand, but only for that single round-trip to the server. The SelectCommand is not persisted anywhere so when you are clicking "Edit", the grid goes right back to using SqlDataSource1's initial command.

Early versions of the ASP.NET GridView stored the SelectCommand text in ViewState, but that was removed for security reasons. As they put it:

For security purposes, the SqlDataSource control no longer stores commands in ViewState by default. Since it is technically possible to decode the contents of ViewState on the client, storing sensitive information about the database backend in this field could expose you to an information disclosure threat.

So instead, you will need to bind the Grid manually and find your way of persisting the SelectCommand across multiple post-backs. Here is an example using 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

这篇关于VB GridView的选择错了行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆