Domanda

I have many drop down lists in many files. I'm trying to write a function to just display "---Please select--" as the default item each time the page loads - which upon post back cannot be selected and hence added to the database nor added to the list as a new item,. i dont know how to achieve this. instead I'm using insert (which I know is wrong) but i dont know which is the right way to achieve this. please advise

ASPX

<asp:DropDownList ID="DropDownListVType" runat="server" DataSourceID="SqlDataSourceVesselType"
 DataTextField="Vessel_Type" DataValueField="VType_ID" Width="160px" AutoPostBack="False"
  CausesValidation="True" OnDataBound="ddl_DataBound">

CS ....

PublicFunctions pubvar = new PublicFunctions();

protected void ddl_DataBound(object sender, System.EventArgs e)
    {
           pubvar.ddl_DB(DropDownListVType);


    }

PUBLIC FUNCTIONS CS

 public void ddl_DB(DropDownList d)
{
    try
    {
        d.Items.Insert(0, new ListItem("--- Please Select ---", String.Empty));
    }
    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }

}

UPDATE: PUBLIC FUNCTIONS CS

    public object checkNull (Control c)
    { 
    try{

       if (c.GetType()==typeof(TextBox))
       {
           TextBox tb = c as TextBox;

         if (!string.IsNullOrEmpty(tb.Text))
            {
                return tb.Text;
            }

        }

        if(c.GetType()==typeof(DropDownList))
        {
            DropDownList dl = c as DropDownList;

             if ((!string.IsNullOrEmpty(c.??
            {
                return c.???
            }

        }
    }

    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }
    return (DBNull.Value);

CALL in PAGE CS

     command.Parameters.AddWithValue("@VType_ID", DropDownListVType.SelectedValue);
È stato utile?

Soluzione

Alright... As you insert your default option to the index 0 you can just do the following:

if(!d.Items[0].Text.equals("--- Please Select ---"))
    d.Items.Insert("--- Please Select ---");

and else do nothing. or as @Plue mentioned:

if(!Page.IsPostBack)
    d.Items.Insert(0, new ListItem("--- Please Select ---", String.Empty));

Additionaly you should probably move "--- Please Select ---" to a constant.

private const string DEFAULTOPTION = "--- Please Select ---";

Altri suggerimenti

Try adding this to your aspx :

<asp:DropDownList ...  AppendDataBoundItems="true">
    <asp:ListItem Text="-Choose car-" disabled="true"/>
</asp:DropDownList>

This will display -Choose car- by default. Then, when the user click on the ddl, this option won't be selectable.

And do not insert anything in code behind.

Edit

If you bind with each postback, try adding this to your ddl_DB code :

d.Items.Insert(0, new ListItem("--- Please Select ---", String.Empty));
d.Items[0].Attributes.Add("disabled", "disabled");

If you always have a "---Please Select---" option or something similar as the default value. You could always just ignore the first ListItem in each dropdown.

myDropDown_OnSelectedIndexChanged(...){

    if(myDropDown.SelectedIndex != 0){

    //Do stuff, make database changes, etc

    }

}

If the goal is to never allow this control to be selected, put something like this:

try
{
    if(!IsPostBack){
        ListItem defaultOption = new ListItem("---Please Select---", String.Empty);
        d.Items.Insert(0, defaultOption);
        defaultOption.Attributes.Add( "disabled", "disabled" );
    }
}
catch (Exception ex)
{
    Console.WriteLine("{0} Exception caught.", ex);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top