2

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

我有一页有一个GridView,另一个有一个DetailsView。每一行GridView都有一个带有 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_PageIndexChanged,DetailsView1_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>
4

1 回答 1

0

我设法通过计算解决了这个问题。由于每页有 10 行,因此GridView我可以通过执行以下操作来计算页面:

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

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

于 2013-04-13T11:58:08.650 回答