在Rowdatabound事件之后,Gridview SelectedIndexChanged没有触发,也没有Page获得Postback

时间:2015-03-19 09:15:03

标签: c# asp.net gridview

我有一个有两个事件的gridview- 1.RowDataBound 2.SelectedIndexChanged RowDataBound事件成功触发,但在此事件之后,SelectedIndexChanged和Page正在获取PostBack.I需要进行页面刷新。 以下是GridView的结构。

<asp:GridView ID="grdHead" runat="server" AutoGenerateColumns="false" Width="100%"
                    CssClass="mGrid" DataKeyNames="EmpId,ReqType,S,ReturnComplete,BookletNo" OnRowDataBound="grdHead_RowDataBound"
                    OnSelectedIndexChanged="grdHead_SelectedIndexChanged">
                    <Columns>
                        <asp:TemplateField HeaderText="Sr No.">
                            <ItemTemplate>
                                <asp:Label ID="lblSrNo" runat="server" Text='<%# Container.DataItemIndex + 1  %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="RequisitionNo" HeaderText="Requisition No." ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:BoundField DataField="BookletNo" HeaderText="Booklet No." ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:BoundField DataField="EmpFullName" HeaderText="Employee Name" ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:BoundField DataField="SiteName" HeaderText="Site Name/Branch Name" ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:BoundField DataField="RMName" HeaderText="Requested By" ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:BoundField DataField="CreatedDate" HeaderText="Requested Date" ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Status" HeaderText="Status" ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:BoundField DataField="SingleDate" HeaderText="Status Date" ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                    </Columns>
                </asp:GridView>

我在RowDataBound事件中使用以下语句:

protected void grdHead_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(grdHead, "Select$" + e.Row.RowIndex);
            e.Row.Attributes["style"] = "cursor:pointer";
        }
    }
    catch (Exception)
    {
        throw;
    }
}

所以请帮助我摆脱这个问题。 提前谢谢。

以下是SelectedIndexChanged事件代码:

protected void grdHead_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        SetParameters();

        for (int i = 0; i < grdHead.Rows.Count; i++)
        {
            grdHead.Rows[i].BackColor = System.Drawing.Color.White;
        }

        int Index = grdHead.SelectedRow.RowIndex;

        grdHead.Rows[Index].BackColor = System.Drawing.Color.SkyBlue;

        ViewState["RequisitionId"] = Convert.ToString(grdHead.Rows[Index].Cells[1].Text.Trim());
        ViewState["EmpId"] = Convert.ToString(grdHead.DataKeys[Index]["EmpId"]);

        DataTable Dt = ReqBll.BindMatDetails(CompanyID, ViewState["RequisitionId"].ToString());

        if (Dt.Rows.Count > 0)
        {
            grdDetails.DataSource = Dt;
            grdDetails.DataBind();
        }
        else
        {
            grdDetails.DataSource = null;
            grdDetails.DataBind();
        }

        btnExport.Enabled = true;
    }
    catch (Exception)
    {
        throw;
    }
}

1 个答案:

答案 0 :(得分:0)

选择行后,SelectedIndexChanged事件不会立即触发。如果触发了RowCommand事件,则会触发它,这是在该行中单击某种按钮时完成的。您可以使用以下内容:

AutoGenerateSelectButton = “真”

或者创建自己的。以下是一些类似的问题和答案: OnSelectedIndexChanged Not Firing in GridView
GridView OnSelectedIndexChanged event not firing

编辑:

如果未点击Page_Load事件,请添加:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    this.Load += Page_Load;
}