New2This
New2This

Reputation: 253

set dropdownlist selection to hardcoded aspx defined value

how do i set the dropdown selection to the option i hardcoded in the aspx page? "

i defined it like this:

 <asp:DropDownList ID="DropDownListTug" runat="server" 
  DataSourceID="SqlDataSourceTugs"
  DataTextField="Tug_Name" DataValueField="Tug_ID" AutoPostBack="True"         
  AppendDataBoundItems="True"OnSelectedIndexChanged="ShowNewRateBtn">
 <asp:ListItem Value="0" Text="&lt;Select&gt;" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>

I'm trying to reset in the codebehind by using either

       protected void NewTug_Click(object sender, EventArgs e)
    {
        processTugs.Visible = true;
        tname.Visible = true;
        allButtons.Visible = true;

        pubvar.EnableAllControls(Page);
        //DropDownListTug.ClearSelection();
        //DropDownListTug.SelectedIndex = 0;
        //DropDownListTug.SelectedValue = "0";
        BtnNewRate.Visible = false;
        BtnDelete.Visible = false;
        BtnUpdate.Visible = false;
        BtnSave.Visible = false;
        BtnNewTugSave.Visible = true;
        BtnCancel.Visible = true;

    }

but its causing it to select the first index of the table and not the above index 0 i defined in the aspx page

Upvotes: 1

Views: 2442

Answers (1)

Adil
Adil

Reputation: 148150

Adding datasource and bind the drop down will remove the existing element. Add the element through Insert at index zero in code behind after binding.

DropDownListTug.DataSource = datatable;
DropDownListTug.DataBind(); 
DropDownListTug.Items.Insert(0, new ListItem("Add New", ""));
DropDownListTug.ClearSelection();
DropDownListTug.SelectedIndex = 0;

Upvotes: 1

Related Questions