2015-03-19 29 views
0

後得到回傳我有有兩個事件 - 1.RowDataBound 2.SelectedIndexChanged RowDataBound事件成功觸發,但在此之後的SelectedIndexChanged事件也不網頁獲得的回發一個gridview。我需要進行頁面刷新。 以下是GridView的結構。的GridView的SelectedIndexChanged不點火,也不頁面RowDataBound事件

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

你能顯示grdHead_SelectedIndexChanged的代碼? – sr28 2015-03-19 10:21:04

+0

該頁面沒有獲得PostBack.It甚至不會去PageLoad事件。 – 2015-03-19 10:24:33

+0

嘗試將RowDataBound事件中的邏輯添加到RowCreated事件中。它看起來像你試圖爲整行創建一個點擊事件,而不是使用標準的提交按鈕。我認爲需要添加到RowCreated事件中。 – sr28 2015-03-19 10:51:23

回答

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

這不工作sr28。 – 2015-03-19 10:06:48

+0

@AnandDhamne - 你的意思是'它不工作'?你究竟做了什麼以及結果如何? – sr28 2015-03-19 10:08:18

+0

我設置AutoGenerateSelectButton =「True」,但點擊後選擇鏈接按鈕仍然不會觸發SelectedIndexChanged事件&甚至沒有頁面正在獲取回傳。 – 2015-03-19 10:11:16

相關問題