LoginView中的GridView给出了错误的行数

时间:2016-08-12 16:17:00

标签: c# asp.net

我在LoginView中有一个GridView,我得到的索引超出范围错误。当我在GridView中显示行数时,它总是显示0,这会导致此错误。

  

我需要做些什么才能获得GridView   在LoginView中正常工作?

下面是我单击GridView中的按钮时运行的代码,错误仅出现在GridView row = gv.Rows[e.RowIndex];行上。如果我只使用e.RowIndex我没有收到错误,它实际上会返回正确的数字。如果我执行gv.DataKeys.Count,它将返回正确的计数。如果我gv.Rows.Count它总是为0.我认为它与PostBack有关,因为如果我在page_load中进行行计数,那么它会返回正确的计数。如果您还有其他任何需要我发布的内容,请告诉我?

protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
    {
        GridView gv = ReviewLoginView.FindControl("gvReview") as GridView;
        GridViewRow row = gv.Rows[e.RowIndex];

        string Id = (row.FindControl("lblID") as Label).Text;

        string constr = System.Configuration.ConfigurationManager.AppSettings["ObservationCardCS"];
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("cardReview"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id", Id);

                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        gv.EditIndex = -1;
        this.BindGrid();

    }
    catch (Exception ex)
    {

        GridView gv = ReviewLoginView.FindControl("gvReview") as GridView;

        int index = e.RowIndex;

        lblError.ForeColor = System.Drawing.Color.Red;
        lblError.Text = ex.Message + " " + index.ToString() + " " + gv.Rows.Count;
    }
}

以下是aspx页面中的LoginView。

    <asp:LoginView runat="server" ViewStateMode="Disabled" ID="ReviewLoginView">
        <LoggedInTemplate>
        <%--<AnonymousTemplate>--%>
            <div>
                <asp:GridView ID="gvReview" runat="server" AutoGenerateColumns="false" DataKeyNames="ID"
                    OnRowDataBound="OnRowDataBound" OnRowDeleting="OnRowDeleting" EnableViewState="true"
                    EmptyDataText="No records have been added." AllowSorting="true" ShowHeaderWhenEmpty="true"
                    AlternatingRowStyle-BackColor="#e0e0e0" HeaderStyle-BackColor="#d0d0d0" ViewStateMode="Disabled"
                    EnableSortingAndPagingCallbacks="false">
                    <Columns>
                        <asp:TemplateField HeaderText="ID" ShowHeader="false" Visible="true">
                            <ItemTemplate>
                                <asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>' CssClass="cmsID"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="User">
                            <ItemTemplate>
                                <asp:Label ID="lblSubmittedBy" runat="server" Text='<%# Eval("submittedBy") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Location">
                            <ItemTemplate>
                                <asp:Label ID="lblLocation" runat="server" Text='<%# Eval("location") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Date">
                            <ItemTemplate>
                                <asp:Label ID="lblSubmittedDate" runat="server" Text='<%# Eval("submittedDate") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="What Was Observed?">
                            <ItemTemplate>
                                <asp:Label ID="lblWhatWasObserved" runat="server" Text='<%# Eval("whatWasObserved") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="What Action Was Taken?">
                            <ItemTemplate>
                                <asp:Label ID="lblWhatActionWasTaken" runat="server" Text='<%# Eval("whatActionWasTaken") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="What Agreement Was Reached?">
                            <ItemTemplate>
                                <asp:Label ID="lblWhatAgreementWasReached" runat="server" Text='<%# Eval("whatAgreementWasReached") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Edit">
                            <ItemTemplate>
                                <asp:Button ID="btnSubmit" runat="server" Text="Edit" CssClass="editbutton" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField HeaderText="Review" ButtonType="Button" ShowDeleteButton="true" DeleteText="Review" />
                    </Columns>
                </asp:GridView>
            </div>
            </LoggedInTemplate> 
        <%--</AnonymousTemplate>--%>
                    <AnonymousTemplate>
            You must login to view submitted Observation Cards.
        </AnonymousTemplate>
    </asp:LoginView>

下面是BindGrid()

private void BindGrid()
{
    try
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ObservationCardCS"]);
        {
            SqlCommand comm = new SqlCommand("cardSelectNew2", conn);
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = comm;
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.AddWithValue("@Begin", "1/1/1950");
            comm.Parameters.AddWithValue("@End", "12/31/2049");
            comm.Parameters.AddWithValue("@Reviewed", "0");
            comm.Parameters.AddWithValue("@OrderBy", "CH.id");
            comm.Parameters.AddWithValue("@AscDesc", "Asc");
            comm.Parameters.AddWithValue("@DateRange", "Last 30 Days");

            comm.Connection = conn;
            sda.SelectCommand = comm;

            DataTable dt = new DataTable();
            sda.Fill(dt);

            GridView gv = ReviewLoginView.FindControl("gvReview") as GridView;

            gv.DataSource = dt;
            gv.DataBind();

        }
    }
    catch (Exception ex)
    {
        lblError.ForeColor = System.Drawing.Color.Red;
        lblError.Text = ex.Message;
    }
}

以下是我的page_load

protected void Page_Load(object sender, EventArgs e)
{

    if (!this.IsPostBack)
    {
        this.BindGrid();
        lblError.ForeColor = System.Drawing.Color.Black;
        lblError.Text = "";
    }

}

1 个答案:

答案 0 :(得分:0)

你走在正确的轨道上!但是,如果没有看到整个类和标记,我们只能推测。

检查您是否正在清除或重建页面加载事件处理程序中的数据,如果在标记中定义了Grid和数据源,那么它应该在页面回发之间缓存或重新查询数据,检查是否已在上面设置了EnableRowCache GridView使您无需在回发期间重新查询数据。

看起来您正在使用#OldSchool BindGrid()数据加载模式,也许最简单的解决方案可能是在尝试访问行之前通过调用BindGrid来确保首先加载数据:

protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
    this.BindGrid();
    try
    {
... delete logic
        this.BindGrid(); // re-load after the change
    }
    catch (Exception ex)
    {
...
    }
}

虽然您不必启用行缓存,但必须确保以相同的顺序将相同的数据加载到网格中,或者基于行索引(而不是行的主键)删除此类逻辑可能会导致您删除错误的数据行。