【发布时间】: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 事件是否被命中?