【问题标题】:Gridview SelectedIndexChanged is not firing nor Page is getting Postback after Rowdatabound event在 Rowdatabound 事件之后,Gridview SelectedIndexChanged 没有触发,页面也没有得到回发
【发布时间】:2015-03-19 09:15:02
【问题描述】:

我有一个 gridview 有两个事件- 1.RowDataBound 2.SelectedIndexChanged RowDataBound 事件已成功触发,但在此事件 SelectedIndexChanged 之后,页面也没有得到 PostBack。我需要刷新页面。 下面是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;
    }
}

【问题讨论】:

  • 你能显示 grdHead_SelectedIndexChanged 的​​代码吗?
  • 页面没有得到 PostBack。它甚至没有进入 PageLoad 事件。
  • 尝试将 RowDataBound 事件中的逻辑添加到 RowCreated 事件中。看起来您正在尝试为整行创建单击事件,而不是使用标准提交按钮。我认为这需要添加到 RowCreated 事件中。
  • 我创建了 RowCreatedEvent 并将 Statements 添加到其中,但仍然无法正常工作。
  • RowCreated 事件是否被命中?

标签: c# asp.net gridview


【解决方案1】:

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

AutoGenerateSelectButton="True"

或者创建您自己的。以下是一些类似的问题和答案: 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;
}

【讨论】:

  • @AnandDhamne - “它不工作”是什么意思?你到底做了什么,结果是什么?
  • 我设置了 AutoGenerateSelectButton = "True" 但点击选择链接按钮后仍然没有触发 SelectedIndexChanged 事件,甚至页面都没有收到 PostBack。
  • @AnandDhamne - 所以 Page_Load 事件仍然没有被触发?你确定?所以你在你的 Page_Load 事件上设置了一个断点并且它从未被命中?
  • 它只在绑定 GridView 之前被触发。在绑定 Gridview 之后它不会被触发。
  • @AnandDhamne 你能显示你的 Page_Load 事件和你的 SelectedIndexChanged 事件的代码吗?
猜你喜欢
  • 2012-10-31
  • 2012-01-11
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多