简体   繁体   中英

Gridview SelectedIndexChanged is not firing nor Page is getting Postback after Rowdatabound event

I have a gridview which has two events- 1.RowDataBound 2.SelectedIndexChanged RowDataBound Event is firing successfully but after this event SelectedIndexChanged nor Page is getting PostBack.I need to make Page Refresh. Following is the structure of 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>

I'm using following Statements in RowDataBound Event :

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;
    }
}

So Please help me to get out of this problem. Thanks in advance.

Below is SelectedIndexChanged Event Code :

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;
    }
}

The SelectedIndexChanged event doesn't fire as soon as you select a row. It is fired when the RowCommand event if fired, which is done when there is some sort of button clicked in that row. You can use the following:

AutoGenerateSelectButton="True"

Alternatively create your own. Here are some similar questions and answers: OnSelectedIndexChanged Not Firing in GridView
GridView OnSelectedIndexChanged event not firing

EDIT:

If the Page_Load event isn't hit then add this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM