我试图从gridview获取值作为RowDataBound,但它返回value =“”

时间:2013-12-08 13:38:36

标签: asp.net

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

我确定cell [0]在gridview中有值,但返回“”

这是我的gridview,我试图在SQL查询中获得RowDataBound的值。查询之后,返回datatable以绑定到其ID为“ddl_gv_examiner_list_n_building”的DropDownList

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

1 个答案:

答案 0 :(得分:0)

在行中,您需要找到Label而不是单元格文本,然后您可以在标签中找到该文本。你应该这样做:

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

希望它有所帮助!