Click here to Skip to main content
15,124,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
Iam trying to access a dropdownlist inside grideview but i alaways get null exeption

1-the aspx file
ASP
<pre><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Employee_Mangment.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>   
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" OnRowDeleted="GridView1_RowDeleted" DataKeyNames="ID" OnRowUpdated="GridView1_RowUpdated" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                    <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" ReadOnly="true" />
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:TemplateField HeaderText="Education" SortExpression="Education">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource2" DataTextField="EducationType" DataValueField="EducationType" SelectedValue='<%# Bind("Education") %>'>
                            </asp:DropDownList>
                            <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetEducations" TypeName="Employee_Mangment.Data.EducationDataAccess"></asp:ObjectDataSource>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Education") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:Label ID="Label1" runat="server" Text="" Font-Bold="true"></asp:Label>
            <asp:Label ID="Label2" runat="server" Text="" Font-Bold="true"></asp:Label>
            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetEmployees" TypeName="Employee_Mangment.Data.EmployeeDataAccess" UpdateMethod="UpdateEmployee" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="Original_{0}" DeleteMethod="DeleteEmployee" OnDeleted="ObjectDataSource1_Deleted" OnUpdated="ObjectDataSource1_Updated">
                <DeleteParameters>
                    <asp:Parameter Name="Original_ID" Type="Int32" />
                    <asp:Parameter Name="Original_Name" Type="String" />
                    <asp:Parameter Name="Original_Education" Type="String" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Original_ID" Type="Int32" />
                    <asp:Parameter Name="Original_Name" Type="String" />
                    <asp:Parameter Name="Original_Education" Type="String" />
                    <asp:Parameter Name="Name" Type="String" />
                    <asp:Parameter Name="Education" Type="String" />
                </UpdateParameters>
            </asp:ObjectDataSource>

        </div> 
    </form>
</body>
</html>


2-the code for the accessing is
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
          DropDownList drop =
             (DropDownList)GridView1.Rows[1].FindControl("DropDownList1");


    }
}


What I have tried:

i tried to access it in the
GridView1_RowDataBound()
using this method row proparty byt get null too
Posted
Updated 1-Apr-20 6:32am
Comments
Richard MacCutchan 31-Mar-20 10:23am
   
Use the debugger to see what is in GridView1.Rows[1]. it may be that the GridView has not been initialised.
Member 14479161 31-Mar-20 15:07pm
   
getting null too
Richard MacCutchan 1-Apr-20 3:00am
   
What does that mean?
Member 14479161 1-Apr-20 6:07am
   
null exeption for the value that i attached GridView1.Rows[1] to it

Richard MacCutchan 1-Apr-20 6:37am
   
There is still no point in me trying to figure out what that is referring to. Do what I suggested yesterday and use your debugger to find out which actual item is null. Then work backwards to find out why it has not been initialised.
Maciej Los 31-Mar-20 11:19am
   
Try this (inside GridView1_RowDataBound method):
DropDownList drop = (DropDownList)e.Row.FindControl("DropDownList1");

Let me know if it was helpful.
Member 14479161 31-Mar-20 15:01pm
   
getting null too

1 solution

The list control is in the EditItemTemplate. Therefore, it will only exist if the row is in the edit state.
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) != 0)
    {
        DropDownList drop = (DropDownList)e.Row.FindControl("DropDownList1");
        ...
    }
}
GridViewRow.RowState Property (System.Web.UI.WebControls) | Microsoft Docs[^]
   
Comments
Maciej Los 1-Apr-20 12:13pm
   
I didn't make enough attention...
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900