Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd values to DropDownList that's not represented in the db and still get SelectedValue asp.net
    primarykey
    data
    text
    <p>I have a couple of <code>DropDownLists</code> in my <code>GridView</code> from customer table that binds the selected value from my db. </p> <p>I noticed that if I only have one customer and would like to edit that customers gender it's not possible because I populate the <code>DropDownLists</code> with the SELECT DISTINCT Gender FROM Customer and my first customer is either Herr or Frau and I can only choose from that value. </p> <p>I was thinking I could solve this Problem using a Union select <code>select Gender from ( select Gender = 'Herr' union select Gender = 'Frau' )as Gender </code> to bring both alternatives but then i get this error message 'DropDownList1'** has a SelectedValue which is invalid because it does not exist in the list of items**</p> <p>So my question is how can I add some alternatives to <code>DropDownLists</code>that's not already presented in DB and still bind the <code>SelectedValue</code>?</p> <pre><code>&lt;EditItemTemplate&gt; &lt;asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1" DataTextField="Gender" DataValueField="Gender" SelectedValue='&lt;%# Bind("Gender") %&gt;'&gt; &lt;/asp:DropDownList&gt; &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:EventConnectionString %&gt;" SelectCommand="SELECT DISTINCT [Gender] FROM [Customer]"&gt;&lt;/asp:SqlDataSource&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="Label2" runat="server" Text='&lt;%# Bind("Gender") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; </code></pre> <hr> <p>Thanks for your answers, what do I Need to Change in code behind to make it work? </p> <p>Here is my code behind.</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { } protected void lbInsert_Click(object sender, EventArgs e) { Event.InsertParameters["Gender"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlGender")).SelectedValue; Event.InsertParameters["LastName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text; Event.InsertParameters["FirstName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text; Event.InsertParameters["Street"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtStreet")).Text; Event.InsertParameters["HouseNr"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtHouseNr")).Text; Event.InsertParameters["Zip"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtZip")).Text; Event.InsertParameters["City"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtCity")).Text; Event.InsertParameters["Phone"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtPhone")).Text; Event.InsertParameters["Email"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtEmail")).Text; Event.InsertParameters["Company"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text; Event.InsertParameters["Active"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlActive")).SelectedValue; Event.Insert(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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