开发者

After filling a DropDownList with date from a database, how can I set the default selection to "Select One"?

开发者 https://www.devze.com 2023-03-28 20:55 出处:网络
This is what I tried... ddlC开发者_高级运维lientsInSearchResult.DataSource = dtClients; ddlClientsInSearchResult.DataValueField = dtClients.Columns[0].ToString();

This is what I tried...

ddlC开发者_高级运维lientsInSearchResult.DataSource = dtClients;
ddlClientsInSearchResult.DataValueField = dtClients.Columns[0].ToString();
ddlClientsInSearchResult.DataTextField = dtClients.Columns[1].ToString();
ddlClientsInSearchResult.DataBind();
ddlClientsInSearchResult.Items.Insert(0, "Select One");

so that the first item in the list is "Select One", but this doesn't seem to be working for me. This is in the code behind an .aspx page, so I was thinking maybe there was a way I could do it over there...but my internet searching has not turned up a solution.

<asp:DropDownList ID="ddlClients" runat="server" Font-Size="8pt" Font-Names="Tahoma"
 Width="200px" AutoPostBack="true" OnSelectedIndexChanged="ddlClients_SelectedIndexChanged">
</asp:DropDownList>

That's the ASP.net code for the DropDownBow that I'm trying to default to "Select One".

Thanks for the help!


Remove this ddlClientsInSearchResult.Items.Insert(0, "Select One"); from code behind and do like...

<asp:DropDownList ID="ddlClients" runat="server" Font-Size="8pt" Font-Names="Tahoma"
        Width="200px" AutoPostBack="true" OnSelectedIndexChanged="ddlClients_SelectedIndexChanged"
        AppendDataBoundItems="true">
        <asp:ListItem Text="Select one" Value="0"></asp:ListItem>
    </asp:DropDownList>


Just add this to your markup:

<asp:DropDown>
     <asp:ListItem Text="Select One" Value="" Selected="true">Select One</asp:ListItem>
</asp:DropDown/>


the code that worked for me is as below

ddlMonth.SelectedIndex = Convert.ToDateTime(row["cstDOB"].ToString()).Month;
ddlYear.SelectedItem.Text = Convert.ToDateTime(row["cstDOB"].ToString()).Year.ToString();
ddlState.SelectedItem.Text = row["cstState"].ToString();


Add "Select One" as an item declaratively and Use the AppendDataBoundItems="true"

<asp:DropDownList ID="ddlClients" runat="server" AppendDataBoundItems="true" AutoPostBack="true" >
    <asp:ListItem Value="0">Select One</asp:ListItem>
</asp:DropDownList>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号