ASP.Net GridView奇怪的编辑行为与分页

时间:2013-01-23 13:08:25

标签: c# asp.net gridview webforms objectdatasource

我有一个ObjectDataSource

<asp:ObjectDataSource SelectCountMethod="GetCount" EnablePaging="true" SortParameterName="sortExpression" ID="customersDS" runat="server" SelectMethod="GetList" TypeName="expenses.Classes.ExpenseFactory" DeleteMethod="Delete" UpdateMethod="Update"  >
    <SelectParameters>
        <asp:ControlParameter ControlID="IdHidden" PropertyName="Value" Name="userId" />   
        <asp:Parameter DbType='Boolean' DefaultValue='false' Name='isExpense' />      
         <asp:Parameter DbType='Boolean' DefaultValue='false' Name='containRepeated' />  
         <asp:ControlParameter DbType="Int32" DefaultValue="" ControlID="CategorySelector2" Name="categoryId" />        
          <asp:ControlParameter DbType="DateTime" DefaultValue="" ControlID="FromSpentDateCalendarBox" Name="from" />  
          <asp:ControlParameter DbType="DateTime" DefaultValue="" ControlID="ToSpentDateCalendarBox" Name="to" />  
    </SelectParameters>
    <UpdateParameters>
     <asp:ControlParameter ControlID="IdHidden" PropertyName="Value" Name="userId" />   
        <asp:Parameter DbType='Boolean' DefaultValue='false' Name='isExpense' />   
    </UpdateParameters>
    </asp:ObjectDataSource>

连接到GridView

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    AllowPaging="True" AllowSorting="True"
            DataSourceID="customersDS" CssClass="gtable sortable width100" DataKeyNames="Id"
            AlternatingRowStyle-CssClass="odd" CellPadding="0" GridLines="None" OnPreRender="PreRender"
        OnRowDataBound="GridView1_RowDataBound" 
           >

使用CommandField中的编辑和删除按钮

<asp:CommandField ItemStyle-Width="75px" HeaderStyle-Font-Bold=true HeaderText="<%$ Resources:Default, Actions %>"  EditImageUrl="~/img/edit.png" ButtonType='Image'  ShowEditButton="True" UpdateImageUrl="~/img/save.gif" CancelImageUrl="~/img/cancel.png" >                                                                      
   <ControlStyle CssClass="my_edit_buttons" />                             
</asp:CommandField>

一切都在内部和UpdatePanel。 GridView和ObjectDataSource支持和使用分页。分页工作没有任何问题。问题是编辑。当我在第一页上单击编辑时,一切都按预期工作。当我在第二个或其他页面(大于一个)上的第n个项目上单击编辑时,GridView切换到第一个页面并选择第n个项目进行编辑。

(更多概念示例:我切换到第二页,我选择要编辑的页面上的项目编号2,GridView切换到第一页并选择要编辑的项目编号2(在第一页上) )。

任何想法可能是什么问题?

修改

PreRender(设置GridView标题):

    protected void PreRender(object sender, EventArgs e)
    {
        try
        {
            GridView1.UseAccessibleHeader = false;
            GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
        catch
        {

        }
    }

RowDataBound(changind删除按钮到自定义配置提示符|:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // we are using a html anchor and a hidden asp:button for the delete
            HtmlAnchor linkDelete = (HtmlAnchor) e.Row.FindControl("linkDelete");
            ImageButton btnDelete = (ImageButton) e.Row.FindControl("btnDelete");

            string prompt = Resources.Default.DeletePrompt;
            linkDelete.Attributes["onclick"] = string.Format(prompt, btnDelete.ClientID);

            Expense expense = e.Row.DataItem as Expense;
            if (expense!=null)
            {
                if (expense.SpentDate>DateTime.Now)
                {
                    e.Row.CssClass = "future";   
                }
            }

            e.Row.Cells[1].CssClass = e.Row.Cells[4].CssClass = "red";               
        }

0 个答案:

没有答案