Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net dropdownlist static listitems

Tags:

asp.net

im working on an asp.net website with c#. I have a dropdownlist with data from an sql database. The dropdownlist is on a gridview.

The dropdownlist is inside a templatefield, on the edititemtemplate.

The data is loaded on the dropdownlist and once a link button is pressed the dropdownlist is enabled for editing, with the corresponding value, .............but also all the other values from the column from the database are loaded there(this last thing is something I dont want).

The dropdownlist is binded on the GridView1_RowDataBound event:

Control ctrlgridMonedaEdit = e.Row.FindControl("gridMonedaEdit");
            if (ctrlgridMonedaEdit != null)
            {
                DropDownList dd = ctrlgridMonedaEdit as DropDownList;
                //more code here
                dd.DataSource = dt;
                dd.DataValueField = "Moneda";
                dd.DataTextField = "Moneda";
                dd.DataBind();
                DataRowView rowView = (DataRowView)e.Row.DataItem;
                string tempDate = rowView["Moneda"].ToString();
                dd.SelectedIndex = dd.Items.IndexOf(dd.Items.FindByText(tempDate));
                sqlConn.Close();
            }

What I want is 3 values which must load when the dropdownlist is enabled for editing. And also that the corresponding value stay selected. I have tried this:

<EditItemTemplate>
      <asp:DropDownList Width="90" runat="server" id="gridMonedaEdit" AutoPostBack="true" Enabled ="true"   >
         <asp:ListItem Value="MONEDA"> MONEDA </asp:ListItem>
                  <asp:ListItem Value="USD"> USD </asp:ListItem>
                  <asp:ListItem Value="MONEDA/USD"> MONEDA/USD </asp:ListItem>
      </asp:DropDownList>                          
</EditItemTemplate>

But these values do not show up.

How can I solve this?

like image 582
pyram Avatar asked Dec 22 '25 00:12

pyram


2 Answers

I'd insert the static list items in the code behind after calling dataBind() on your dropdownlist.

like image 78
Andrew Walters Avatar answered Dec 24 '25 01:12

Andrew Walters


You can use the AppendDataBoundItems property to tell ASP.NET that the DataSource should be appended to the "static" item(s).

 <asp:DropDownList id="gridMonedaEdit" AppendDataBoundItems="True"    runat="server">
like image 31
Purab Kumar Das Avatar answered Dec 24 '25 03:12

Purab Kumar Das



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!