0

I am trying to populate a drop down list in the footer row of a grid view. my mark up is as follows:

<asp:Label ID="lblGrdImages" runat="server" ></asp:Label>
<asp:GridView 
    ID="grdImages" 
    runat="server"
    AllowPaging="true"
    ShowFooter="true"
    PageSize="5"
    AutoGenerateColumns="false"
    OnPageIndexChanging="grdImages_PageIndexChanging"
    OnRowCancelingEdit="grdImages_RowCancelingEdit"
    OnRowCommand="grdImages_RowCommand"
    OnRowEditing="grdImages_RowEditing"
    OnRowUpdating="grdImages_RowUpdating"
    OnRowDeleting="grdImages_RowDeleting" 
    EmptyDataText="No Data Available at this Time"       >
    <AlternatingRowStyle BackColor="White" ForeColor="#284775"></AlternatingRowStyle>        


    <Columns>

        <asp:TemplateField AccessibleHeaderText="Product ID" HeaderText="Product ID" FooterText="Product ID">
            <ItemTemplate>
                <asp:Label ID="lblProdId" runat="server" Text='<%# Eval("pi.ProductId") %>' ></asp:Label>
            </ItemTemplate>

            <EditItemTemplate>
                <asp:Label ID="lblEditProdId" runat="server" Text='<%# Eval("pi.ProductId") %>' ></asp:Label>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="lstAddProdId" runat="server"  AppendDataBoundItems="true" >
                    <asp:ListItem>Select a product</asp:ListItem>
                </asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField AccessibleHeaderText="Product Main Image" FooterText="Product Main Image" HeaderText="Product Main Image">
            <ItemTemplate>
                <asp:Label ID="lblMainImgId" runat="server" Text='<%# Eval("pi.ImageId") %>' ></asp:Label>
                <asp:Image ID="imgMain" runat="server" Height="250" Width="250" ImageUrl='<%# Eval("pi.ImagePath") %>' />
            </ItemTemplate>
            <FooterTemplate>
                <asp:FileUpload ID="flupMain" runat="server" AllowMultiple="false" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField AccessibleHeaderText="Supporting Images" FooterText="Supporting Images" HeaderText="Supporting Images">
            <ItemTemplate>
                <h2>repeater here</h2>
            </ItemTemplate>
            <FooterTemplate>
                <asp:FileUpload ID="flupExtra" runat="server" AllowMultiple="true" />
            </FooterTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Edit">
            <ItemTemplate>
                <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
                <br />
                <span onclick="return confirm('Are you sure you want to declare this product Discontinued?')">
                    <asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
                </span>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
                <br />
                <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
            </EditItemTemplate>
            <FooterTemplate>
                <asp:Button ID="btnAddRecord" runat="server" Text="Add" CommandName="Add"></asp:Button>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>

    <EditRowStyle BackColor="#999999"></EditRowStyle>

    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></FooterStyle>

    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></HeaderStyle>

    <PagerStyle HorizontalAlign="Center" BackColor="#284775" ForeColor="White"></PagerStyle>

    <RowStyle BackColor="#F7F6F3" ForeColor="#333333"></RowStyle>

    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"></SelectedRowStyle>

    <SortedAscendingCellStyle BackColor="#E9E7E2"></SortedAscendingCellStyle>

    <SortedAscendingHeaderStyle BackColor="#506C8C"></SortedAscendingHeaderStyle>

    <SortedDescendingCellStyle BackColor="#FFFDF8"></SortedDescendingCellStyle>

    <SortedDescendingHeaderStyle BackColor="#6F8DAE"></SortedDescendingHeaderStyle>
</asp:GridView>

My code behind function to populate the drop down list:

protected void lstProducts()
{
    // find dropdownlist in the footer row of the gridview
    DropDownList prods = (DropDownList)grdImages.FooterRow.FindControl("lstAddProdId");
    // define ado.net objects
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("ProductDetails.bnc_ProductImages", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataReader reader;
    // define the sp parameters
    cmd.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 50));
    cmd.Parameters["@Status"].Value = "LstProds";

    try
    {
        con.Open(); // try to connect to db.
        reader = cmd.ExecuteReader();   // execut the reader
        while (reader.Read())
        {                
            ListItem item = new ListItem(); // create listitem
            item.Text = reader["p.ProductName"].ToString();   // add product name to item text
            item.Value = reader["p.ProductId"].ToString();    // add productId to item value
            prods.Items.Add(item);    // populate dropdown list.
        }

    }
    catch (Exception err)
    {
        lblGrdImages.Text = err.Message; // display error message in  a label
    }
    finally
    {
        con.Close();    // close the connection.
    }
}

When I try to run it I get the following error:

An exception of type 'System.NullReferenceException' occurred in App_Web_su5rfbqf.dll but was not handled in user code

I am unsure as to why I am getting this exception. I am assuming it is not able to find the control in the grid view footer row but I do not know why. It may be a silly mistake somewhere but I cannot figure out what I should do

EDIT I am getting the error on this line of lstProducts():

DropDownList prods = (DropDownList)grdImages.FooterRow.FindControl("lstAddProdId");
M_Griffiths
  • 461
  • 5
  • 23

1 Answers1

0

You have to set Datasource to your gridView first.The error you are getting is at grdImages.FooterRow=null.Try populating the grdImages first and then try to access it.

Here is a similar example that does the same

<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
runat="server" AutoGenerateColumns="false" ShowFooter = "true" OnDataBound = "OnDataBound">
<Columns>
    <asp:TemplateField HeaderText = "Name">
        <ItemTemplate>
            <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
        </ItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtName" runat="server" />
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText = "Country">
        <ItemTemplate>
            <asp:Label ID="lblName" runat="server" Text='<%# Eval("Country") %>' />
        </ItemTemplate>
        <FooterTemplate>
            <asp:DropDownList ID="ddlCountries" runat="server">
            </asp:DropDownList>
        </FooterTemplate>
    </asp:TemplateField>
</Columns>

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
            dt.Rows.Add(1, "John Hammond", "United States");
            dt.Rows.Add(2, "Mudassar Khan", "India");
            dt.Rows.Add(3, "Suzanne Mathews", "France");
            dt.Rows.Add(4, "Robert Schidner", "Russia");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }

    protected void OnDataBound(object sender, EventArgs e)
    {
        DropDownList ddlCountries = GridView1.FooterRow.FindControl("ddlCountries") as DropDownList;
    }
Rohit
  • 9,362
  • 6
  • 43
  • 76