如何在DetailsView中查找哪个页面包含选定的GridView行

时间:2013-04-12 17:08:01

标签: c# asp.net gridview detailsview

我正在设置HealthMonitoring,我正在撰写几页来管理事件日志。

我有一个页面有GridView而另一个页面有DetailsViewGridView每行都有一个模板按钮,其中有一个onClick事件,用于加载第二页,上面有DetailsView

我想做的是;当我单击GridView上的模板按钮时,然后使用DetailsView加载第二页,并将GridView中的特定记录插入DetailsView表。

DetailsView必须启用分页。我试图找出要从GridView使用正确记录加载的页面索引。目前它只会加载第一页索引,然后我必须单击我需要的记录旁边。

如果没有启用分页,我可以读取我的2个全局变量,然后将正确的记录加载到DetailsView表中,但启用分页后,我不知道该怎么做。

GridView页面上:

protected void Details1_ButtonClick(object sender, EventArgs e)
{
    //I set 2 global variables here of the selected EventId and 
    //Details to read when the next page loads
    Response.Redirect("ErrorDetails.aspx");
}

DetailsView页面上。

protected void Page_Load(object sender, EventArgs e)
{
    //without paging I can set the SqlDataSource1.SelectCommand to select 
    //the correct record using one of the global variables
}

我尝试使用DetailsView1_PageIndexChangedDetailsView1_LoadPage_Load来获取第一行中的值,但由于某种原因,它总是落后于页面。变量始终在加载时显示上一页的ID。我打算尝试跟踪删除记录的页面,但它也不起作用。

这是ErrorDetails.aspx中的DataSource和DetailsView:

<asp:SqlDataSource 
    ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" 
    ProviderName="<%$ ConnectionStrings:DefaultConnection.ProviderName %>">
    <DeleteParameters>
        <asp:Parameter Name="EventId" />
    </DeleteParameters>
</asp:SqlDataSource>

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" DataSourceID="SqlDataSource1" DataKeyNames="EventId" AutoGenerateRows="False" OnItemDeleted="DetailsView1_ItemDeleted" OnItemDeleting="DetailsView1_ItemDeleting" AllowPaging="True" OnLoad="DetailsView1_Load" OnPageIndexChanged="DetailsView1_PageIndexChanged" OnPageIndexChanging="DetailsView1_PageIndexChanging">
    <AlternatingRowStyle CssClass="alt" />
    <Fields>

        <asp:CommandField ShowDeleteButton="True" />

        <asp:BoundField DataField="EventId" HeaderText="EventId" />
        <asp:BoundField DataField="EventTimeUtc" HeaderText="EventTimeUtc" />
        <asp:BoundField DataField="EventTime" HeaderText="EventTime" />
        <asp:BoundField DataField="EventType" HeaderText="EventType" />
        <asp:BoundField DataField="EventSequence" HeaderText="EventSequence" />
        <asp:BoundField DataField="EventOccurrence" HeaderText="EventOccurrence" />
        <asp:BoundField DataField="EventCode" HeaderText="EventCode" />
        <asp:BoundField DataField="EventDetailCode" HeaderText="EventDetailCode" />
        <asp:BoundField DataField="Message" HeaderText="Message" />
        <asp:BoundField DataField="ApplicationPath" HeaderText="ApplicationPath" />
        <asp:BoundField DataField="ApplicationVirtualPath" HeaderText="ApplicationVirtualPath" />
        <asp:BoundField DataField="MachineName" HeaderText="MachineName" />
        <asp:BoundField DataField="RequestUrl" HeaderText="RequestUrl" />
        <asp:BoundField DataField="ExceptionType" HeaderText="ExceptionType" />

        <asp:TemplateField HeaderText="Details">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server">
                    <%= EventVariables.EventDetails %>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:CommandField ShowDeleteButton="True" />

    </Fields>
    <PagerSettings Mode="NextPreviousFirstLast" />
    <PagerStyle CssClass="pager" />
</asp:DetailsView>

这是来自父表单的DataSource和GirdView:

<asp:GridView 
        ID="ErrorGrid" 
        runat="server" 
        AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1"  
        DataKeyNames="EventId"
        OnRowDeleting="ErrorGrid_RowDeleting" 
        AllowPaging="True" 
        AllowSorting="True" 
        Font-Size="Small" 
        CellPadding="10" 
        CellSpacing="1" >
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');" Text="Delete"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="Details1" Text="Details" runat="server" AutoPostBack="true" OnClick="Details1_ButtonClick" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="EventId" HeaderText="EventId" SortExpression="EventId" />
            <asp:BoundField DataField="EventTime" HeaderText="EventTime" SortExpression="EventTime" />
            <asp:BoundField DataField="RequestUrl" HeaderText="RequestUrl" SortExpression="RequestUrl" />
            <asp:BoundField DataField="ExceptionType" HeaderText="ExceptionType" SortExpression="ExceptionType" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource 
        ID="SqlDataSource1" 
        runat="server" 
        ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" 
        SelectCommand="SELECT * FROM [aspnet_WebEvent_Events]" 
        DeleteCommand="DELETE FROM [aspnet_WebEvent_Events] WHERE [EventId]=@EventId">
        <DeleteParameters>
            <asp:Parameter Name="EventId" />
        </DeleteParameters>
    </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

我设法通过计算来解决这个问题。由于GridView每页有10行,我可以通过这样做来计算页面:

EventVariables.EventPage = ((((ErrorGrid.PageIndex + 1) * 10) - 10) + Row.RowIndex);

然后在加载时从变量中加载DetailsView页面。