Note that there are some explanatory texts on larger screens.

plurals
  1. PONull Reference Exception when attempting to bind dropdownlist inside formview
    text
    copied!<p>I have an edit button and a dropdownlist inside a formview. I am using Linq To Entities to get the data I need to work with and have no problem populating and viewing the formview itemtemplate. </p> <p>However, the dropdownlist control (<code>id="ddlEligibility"</code>) is only in the<code>edititemtemplate</code> (I use a textbox in the <code>itemtemplate</code> to display the current value) and I am having a problem binding this control to the datasource. </p> <p>Specifically, when I click the Edit button I am getting a <code>NullReferenceException - "Object not set to an instance of an object"</code> on the last line of code below. Does anyone have an idea what I am doing wrong?</p> <pre><code>protected void btnEdit_Click(object sender, EventArgs e) { fvSubscriber.ChangeMode(FormViewMode.Edit); fvSubscriber.DataBind(); // Adding this line solved the first problem where I could not find the control LifeLineDSEntities context = new LifeLineDSEntities(): var program = from p in context.EligibilityPrograms select p; DropDownList ddlEligibility = (DropDownList)(fvSubscriber.FindControl("ddlEligibility"))); if (ddlEligibility != null) { ddlEligibility.DataSource = program; ddlEligibility.DataTextField = "ProgramName"; ddlEligibility.DataValueField = "eligibilityCode"; ddlEligibility.DataBind(); statusMessage.InnerHtml = "It is NOT null"; } else { statusMessage.InnerHtml = "It is null"; } } </code></pre> <p>DropDownlist in FormView...</p> <pre><code>&lt;form id="form1" runat="server"&gt; &lt;asp:FormView ID="fvSubscriber" runat="server" RenderOuterTable="false" DefaultMode="Readonly" OnModeChanging="fvSubscriberChanging"&gt; &lt;ItemTemplate&gt; // mark up here &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:DropDownList ID="ddlEligibility" runat="server" /&gt; &lt;/EditItemTemplate&gt; &lt;/asp:FormView&gt; &lt;/form&gt; </code></pre> <p><b>UPDATE:</b> I tried the FindControlRecursive method discussed in the accepted answer in the link below (for FormView though) to see if for some reason the control was not where I thought it was but still no luck, it always comes back null. When I view source on the page, the control "ddlEligibility" is there.</p> <p><a href="https://stackoverflow.com/questions/13336839/asp-net-findcontrol-and-gridview-returning-null">ASP.net .FindControl() and GridView returning null</a></p> <p><b>New Update</b> I was able to find the control by adding fvSubsriber.databind() as shown in the code above, however I am unable to get the edititemtemplate to show the value in the dropdownlist that was displayed in the itemtemplate. In the edititemtemplate is just default to the first item in dropdownlist. I have added the code above. I have moved this new problem to a new post</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload