Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It looks like the method <code>GetVehicles()</code> is dynamically creating the drop-down list, since you are adding the drop-down to Controls collection on the last line of second if statement.</p> <p>When you are dynamically creating controls, you have to recreate them on every postback. </p> <p>Instead, place the drop-down control inside <code>EditItemTemplate</code> and then find this control using the <code>FindControl</code> method and populate it in code behind like you are doing now.</p> <p>Here is the example of GridView definition:</p> <pre><code>&lt;asp:GridView runat="server" ID="GridViewVehicles" OnRowDataBound="GridViewVehicles_RowDataBound" OnRowUpdating="GridViewVehicles_RowUpdating"&gt; &lt;Columns&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;!-- Text of selected drop-down item --&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:DropDownList runat="server" ID="ddlVehicles" /&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; </code></pre> <p>And the code-behind:</p> <pre><code>protected void GridViewVehicles_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow &amp;&amp; e.Row.RowState == DataControlRowState.Edit) { //Get the drop-down datasource and perform databinding } } protected void GridViewVehicles_RowUpdating(object sender, GridViewUpdateEventArgs e) { DropDownList ddlVehicles = GridViewVehicles.Rows[e.RowIndex].FindControl("ddlVehicles") as DropDownList; if (ddlVehicles != null) { string selectedValue = ddlVehicles.SelectedValue; } } </code></pre> <p>Hope it helps!</p> <p>Regards,</p> <p>Uroš</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