转到与网格视图中的特定记录对应的页面索引

时间:2013-05-21 06:14:34

标签: c# asp.net

您好我正在使用ASPC#编写的网页,我可以在其中添加edit and delete来自database的记录。网页的grid view每行包含select, edit and delete links,行按日期排序。所以我的问题是: 如何插入新记录,并自动转到相应的页面? (例如,网格视图页面大小为5)

  

这是网格视图的一部分

<asp:GridView ID="grdSchedulerApplications" runat="server" AutoGenerateColumns="False"
                            Visible="false" ShowFooter="True" PageSize="5" DataKeyNames="sa_ID, sa_ApplicationID"
                            AllowPaging="True" OnRowCommand="grdSchedulerApplications_RowCommand">
                            <Columns>
                                <asp:TemplateField HeaderText="Application" HeaderStyle-HorizontalAlign="Left">
                                    <EditItemTemplate>
                                        <asp:DropDownList ID="ddlEditSchedulerApplications" runat="server" AutoPostBack="True"
                                            DataTextField="app_Name" DataValueField="app_Id">
                                        </asp:DropDownList>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <%# Eval("Application")%>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        <asp:DropDownList ID="ddlSchedulerApplications" runat="server" AutoPostBack="True"
                                            DataTextField="app_Name" DataValueField="app_Id">
                                        </asp:DropDownList>
                                    </FooterTemplate>
                                </asp:TemplateField>
  

然后我有C#代码:

protected void grdSchedulerApplications_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Insert"))
        {
            try
            {
                int saSchdlrID;
                int.TryParse(grdBulkScheduler.SelectedDataKey.Values[0].ToString(), out saSchdlrID);
                DropDownList ddlSchedulerApps = (DropDownList)grdSchedulerApplications.FooterRow.FindControl("ddlSchedulerApplications");

                int appID;
                int.TryParse(ddlSchedulerApps.SelectedValue.ToString(), out appID);

                //calling a method from another class to add/update records into database
                object obj = cData.AddUpdateBulkSchedulerApplications(0, saSchdlrID, appID);

                //query returns the automatically incremented ID of newly created record into obj
                if (obj.ToString() != "0")
                {
                    //do some action
                    //here I need to go to the corresponding page Index
                }
            }
            catch (Exception ex)
            {
                //exception
            }
        }

1 个答案:

答案 0 :(得分:1)

如果数据源返回按日期排序的行,并且您不修改此顺序,则需要设置页面索引并将网格重新绑定到其数据源:

if (obj.ToString() != "0")
{
    grdSchedulerApplications.PageIndex = Int32.Parse(obj) / 5;
    // TODO: Rebind your grid (set the data source and call DataBind())
}