本文介绍了如何添加下拉列表中的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用asp.net从SQL表中读取数据到下拉列表。问题是,当我默认选择的下拉列表。它并不需要的默认值。
请参阅code
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
如果(!的IsPostBack)
{
conn.Open();
CMD的SqlCommand =新的SqlCommand(选择States_agri *,康恩);
SqlDataReader的博士= cmd.ExecuteReader();
ddl_state.DataSource =博士;
ddl_state.Items.Clear();
ddl_state.Items.Add( - 请选择state--);
ddl_state.DataTextField =Statename的;
ddl_state.DataValueField =STATEID;
ddl_state.DataBind();
conn.Close();
}
}
`
另请参阅下拉列表ASPX code,供大家参考。
< ASP:的UpdatePanel ID =FormUpdate=服务器的UpdateMode =条件>
<&触发器GT;
< ASP:AsyncPostBackTrigger控件ID =ddl_state事件名称=的SelectedIndexChanged/>
< /触发器>
<&的ContentTemplate GT;
<表>
&所述; TR>
< TD>国家* LT; / TD>
&所述; TD>
< ASP:DropDownList的ID =ddl_state=服务器的CssClass =cbfld-popup1的AutoPostBack =真OnSelectedIndexChanged =ddl_state_SelectedIndexChanged>
< ASP:列表项启用=真实所选=真文本=请选择国家>< / ASP:ListItem的>
< / ASP:DropDownList的>
< / TD> < / TR>
&所述; TR>
< TD>区* LT; / TD>
&所述; TD>
< ASP:DropDownList的ID =ddl_district的CssClass =cbfld-popup1=服务器>
< ASP:列表项启用=真实所选=真文本=请选择城市>< / ASP:ListItem的>
< / TD>
< / TR>
< /表>
< /&的ContentTemplate GT;
< / ASP:的UpdatePanel>
解决方案
数据绑定后,做到这一点:
ddl_state.Items.Insert(0,新的ListItem(选择,NA)
或标记添加为:
< ASP:DropDownList的.. AppendDataBoundItems =真正的>
<项目>
< ASP:ListItem的文本=选择VALUE =/>
< /项目>
< / ASP:DropDownList的>
I am using asp.net for fetching data from sql table to dropdown list. The problem is that, when I give default selection to the dropdown list. It does not take the default value.Please see the code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from States_agri", conn);
SqlDataReader dr = cmd.ExecuteReader();
ddl_state.DataSource = dr;
ddl_state.Items.Clear();
ddl_state.Items.Add("--Please Select state--");
ddl_state.DataTextField = "StateName";
ddl_state.DataValueField = "StateID";
ddl_state.DataBind();
conn.Close();
}
}
`
Also Please see the dropdown list aspx code for your reference.
<asp:UpdatePanel ID="FormUpdate" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl_state" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<table>
<tr>
<td>State*</td>
<td>
<asp:DropDownList ID="ddl_state" runat="server" CssClass="cbfld-popup1" AutoPostBack="true" OnSelectedIndexChanged="ddl_state_SelectedIndexChanged">
<asp:ListItem Enabled="true" Selected="True" Text="Please select State"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>District*</td>
<td>
<asp:DropDownList ID="ddl_district" CssClass="cbfld-popup1" runat="server">
<asp:ListItem Enabled="true" Selected="True" Text="Please select city"></asp:ListItem>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
解决方案
After data-binding, do this:
ddl_state.Items.Insert(0, new ListItem("Select","NA")
Or add it in markup as:
<asp:DropDownList .. AppendDataBoundItems="true">
<Items>
<asp:ListItem Text="Select" Value="" />
</Items>
</asp:DropDownList>
这篇关于如何添加下拉列表中的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!