过滤ASP GridView可以工作,但会破坏其他功能

时间:2016-12-20 17:50:21

标签: c# asp.net gridview

在ASP.NET GridView上工作。 它工作正常,但它的默认过滤器列不是我想要的。 所以我将这一行添加到Page_Load方法中:

 i_gvOption.Sort("idWsgProgramOption", SortDirection.Ascending);

按预期过滤,编辑显示仍然完美... 但后来我的更新和插入命令不再工作。

GridView代码可简化如下:

  <asp:SqlDataSource ID="i_sdsGvOption" runat="server" ConnectionString="<%$ ...%>"
        SelectCommand="spWsgProgramOptionSelectNom" SelectCommandType="StoredProcedure" 
        InsertCommand="spWsgProgramOptionInsert" InsertCommandType="StoredProcedure"
        UpdateCommand="spWsgProgramOptionUpdateNom" UpdateCommandType="StoredProcedure" 
        DeleteCommand="spWsgProgramOptionDelete" DeleteCommandType="StoredProcedure">

        <InsertParameters>
            <asp:Parameter Name="nomWsgProgramOption" DefaultValue="(name)" Type="String"/>
        </InsertParameters>
    </asp:SqlDataSource>
    <asp:UpdatePanel ID="i_up" runat="server">
        <ContentTemplate>
            <asp:GridView ID="i_gvOption" runat="server" AutoGenerateColumns="False" DataKeyNames="idWsgProgramOption"
                DataSourceID="i_sdsGvOption" AllowPaging="true"  EnableModelValidation="True" AllowSorting="true">
                <Columns>
                    <asp:CommandField ButtonType="Image" CancelImageUrl="~/...gif"
                        CancelText="Annuler" EditImageUrl="~/...gif"
                        EditText="Update" HeaderText="M" ShowEditButton="True" UpdateImageUrl="~/...gif"
                        UpdateText="Save">
                    </asp:CommandField>


                     <!-- my displayed columns, not worth listing their code-->


                    <asp:TemplateField HeaderText="Del">
                        <ItemTemplate>
                            <asp:ImageButton ID="i_imgbtnDel" runat="server" 
                                CausesValidation="False" CommandName="Delete" ImageAlign="Middle" ImageUrl="~...gif"  />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>

            </asp:GridView>

为什么此排序行会导致其他功能停止工作????

编辑: 我刚才注意到了什么。 如果我在update事件中添加一个触发方法的数据源:

OnUpdating="i_sdsGvOption_Updating"

    protected void i_sdsGvOption_Updating(object sender, SqlDataSourceCommandEventArgs e) 
    {
var id = e.Command.Parameters["@idWsgProgramOption"].Value;
var nom = e.Command.Parameters["@nomWsgProgramOption"].Value;
}

如果我的OnLoad方法不包含排序指令: id和nom变量都有预期值。

当我将sort指令添加到OnLoad方法时,nom变量的值仍然是i_sdsGvOption_Updating,但是id返回null。

这仍然无法帮助我回答这个问题,但我相信它会帮助你找到答案。

0 个答案:

没有答案