简体   繁体   English

SQLDatasource查询更改后,GridView行是否未更新?

[英]GridView Row is not updating after SQLDatasource query changed?

I have a grid view and i have bonded sql data source to that. 我有一个网格视图,我已经绑定了sql数据源。 on page i have drop down, as soon as selected value of drop down get changes, i change the select query of sql data source and again binding the data to grid view. 在页面上,我具有下拉菜单,一旦下拉列表的选定值得到更改,我就会更改sql数据源的选择查询,然后再次将数据绑定到网格视图。 before doing this if i update the grid view row its get updated but after doing previous process my update is not working. 在执行此操作之前,如果我更新了网格视图行,则它会更新,但是在执行先前的过程后,我的更新无法正常工作。 it don't show me any error. 它不会显示任何错误。 it don't take edited values. 它不接受已编辑的值。

my grid view 我的网格视图

<asp:GridView ID="gvTests" runat="server" AutoGenerateColumns="False" EmptyDataText="Testes are not assigned to this sample type."
    CellPadding="4" CssClass="border" DataKeyNames="TestId" DataSourceID="SqlDS"
    AlternatingRowStyle-BackColor="#E0ECF8" HeaderStyle-Height="20px" ForeColor="#333333"
    HeaderStyle-HorizontalAlign="Left" GridLines="None" Width="100%" OnRowCommand="gvTests_RowCommand"
    AllowPaging="True" AllowSorting="True" OnRowDataBound="gvTests_RowDataBound">
    <AlternatingRowStyle />
    <Columns>
        <asp:TemplateField HeaderText="TestId" InsertVisible="False" SortExpression="TestId"
            Visible="False">
            <EditItemTemplate>
                <asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblId" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Test" SortExpression="TestName" HeaderStyle-HorizontalAlign="Center">
            <EditItemTemplate>
                <asp:TextBox CssClass="smallinput_t200" Width="100px" Text='<%# Bind("Name") %>'
                    ID="txtTestName" runat="server"></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField ShowHeader="False">
            <EditItemTemplate>
                <asp:LinkButton ID="LinkButtonUpdate" runat="server" CausesValidation="True" CommandName="Update"
                    Text="Update"></asp:LinkButton>
                &nbsp;<asp:LinkButton ID="LinkButtonCancel" runat="server" CausesValidation="False"
                    CommandName="Cancel" Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButtonEdit" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButtonDelete" runat="server" CausesValidation="False" CommandName="Del"
                    Text="Delete" ></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButtonSelect" runat="server" CausesValidation="False" CommandName="Select"
                    Text="Edit"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#6B89AD" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle CssClass="mytr" />
    <SelectedRowStyle BackColor="#6B89AD" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

sql data source is like this sql数据源是这样的

<asp:SqlDataSource ID="SqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"       
    SelectCommand="select * from tests"         
    UpdateCommand="Update [Tests] set [Name]=@TestName where [Id]=@TestId"
   >
   <FilterParameters>
   </FilterParameters>
    <UpdateParameters>
        <asp:Parameter Name="Name" DbType="String" />
        <asp:Parameter Name="Id" DbType="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

my drop down is as 我的下拉列表是

<asp:DropDownList ID="ddlType" runat="server"
                        DataSourceID="SqlDS" DataTextField="Name" 
                        DataValueField="Id" OnSelectedIndexChanged="ddlType_SelectedIndexChanged"
                        AutoPostBack="True" OnDataBound="ddlSampleType_DataBound">
                    </asp:DropDownList>  

index change code is as 索引更改代码为

protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDS.SelectCommand = "select * from Tests t where t.Id in (1,2,3,4,5)";
        SqlDS.Select(DataSourceSelectArguments.Empty);
        gvTests.EditIndex = -1;
        gvTests.DataBind();
    }

Row edit code is as 行编辑代码为

protected void gvTests_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
            SqlDSTests.UpdateParameters["Name"].DefaultValue = (row.FindControl("txtName") as TextBox).Text;
            SqlDSTests.UpdateParameters["Id"].DefaultValue = (row.FindControl("lblId") as Label).Text;
            SqlDSTests.Update();
            SqlDS.Select(DataSourceSelectArguments.Empty);
            gvTests.EditIndex = -1;
            gvTests.DataBind();
        }
   }

I suspect that this is happening because SelectCommand property of Sql Data source is not backed up by view-state (for security purposes). 我怀疑这是由于Sql Data source的SelectCommand属性未按视图状态备份(出于安全目的)而发生的。 So when you change the select command in one of the event, the change is reflected only for that request. 因此,当您在一个事件中更改选择命令时,更改仅反映该请求。 On subsequent post-back (for update event), the design time select command will be used and it may change the data bound to grid-view causing update to fail. 在随后的回发(用于更新事件)上,将使用设计时间选择命令,并且该命令可能会更改绑定到网格视图的数据,从而导致更新失败。

As a solution, you can try using FilterExpression and FilterParameters if it make sense in your scenario (check the example in FilterExpression help). 作为解决方案,如果在您的情况下有意义,可以尝试使用FilterExpressionFilterParameters (请参阅FilterExpression帮助中的示例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM