2013-12-08 57 views
0
protected void gv_examiner_list_OnRowDataBound(object sender, GridViewRowEventArgs e) 
{ 

    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     string n = (string)DataBinder.Eval(e.Row.DataItem, "id_examiner"); //this can return value from Label 
     string n_2 = e.Row.Cells[0].Text; //this return "" 
    } 
} 

我敢肯定,細胞[0]在GridView的價值,但返回「」我試圖從GridView中獲取價值的RowDataBound,但它返回值=「」

這是我的GridView,我試圖讓RowDataBound的價值加上SQL查詢。查詢後,然後返回數據表綁定到DropDownList的ID爲「ddl_gv_examiner_list_n_building」

<asp:GridView ID="gv_examiner_list" runat="server" AutoGenerateColumns="False" 
OnRowDataBound="gv_examiner_list_OnRowDataBound" 
OnRowEditing="gv_examiner_list_OnRowEdition" 
OnRowCancelingEdit="gv_examiner_list_OnRowCancelingEdit" 
OnRowUpdating="gv_examiner_list_OnRowUpdating"> 
<Columns> 
     <asp:TemplateField HeaderText="รหัสผู้สอบ"> 
      <ItemTemplate> 
       <asp:Label ID="lb_gv_examiner_list_id_examiner" runat="server" Text='<%# Eval("id_examiner")%>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="ชื่อ"> 
      <ItemTemplate> 
       <asp:Label ID="lb_gv_examiner_list_fname" runat="server" Text='<%# Eval("fname")%>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="นามสกุล"> 
      <ItemTemplate> 
       <asp:Label ID="lb_gv_examiner_list_lname" runat="server" Text='<%# Eval("lname")%>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="อาคาร"> 
      <ItemTemplate> 
       <asp:Label ID="lb_gv_examiner_list_n_building" runat="server" Text='<%# Eval("n_building")%>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:DropDownList ID="ddl_gv_examiner_list_n_building" runat="server"/> 
      </EditItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="ชั้น"> 
      <ItemTemplate> 
       <asp:Label ID="lb_gv_room_provided_floor" runat="server" Text='<%# Eval("floor") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="ชื่อห้อง"> 
      <ItemTemplate> 
       <asp:Label ID="lb_gv_room_provided_n_exam_room" runat="server" Text='<%# Eval("n_exam_room")%>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField>   
     <asp:TemplateField HeaderText="เลขที่นั่งสอบ"> 
      <ItemTemplate> 
       <asp:Label ID="lb_gv_room_provided_seat_number" runat="server" Text='<%# Eval("seat_number")%>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 

     <asp:CommandField ShowEditButton="True" /> 
    </Columns> 
</asp:GridView> 
+0

編輯你的問題,包括你的gridview標記 – codingbiz

+0

也顯示你的HTML。 –

回答

0

在你需要找到的標籤,而不是單元格文本的行,然後你可以找到在標籤中的文本。你應該做如下圖所示:

protected void gv_examiner_list_OnRowDataBound(object sender, GridViewRowEventArgs e) 
{ 

    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     string n = string.Empty; 
     string n2 = string.Empty; 

     Label lblId = e,Row.FindControl("lb_gv_examiner_list_id_examiner") as Label; 
     Label lblFirstName = e,Row.FindControl("lb_gv_examiner_list_fname") as Label; 

     if (lblId != null && lblFirstName != null) 
     { 
      n= lblId.Text; 
      n2 = lblFirstName.Text; 
     } 
    } 
} 

希望它能幫助!

+0

謝謝你,你是對的,我必須從標籤上找到 – user3079892