无法将下拉列表设为空 [英] Cannot make empty a dropdownlist
问题描述
你好,
我有2个下拉列表:
1- ddlCity:显示数据库中的所有城市
2-ddlBranch:显示每个城市的所有分支.
因此,一旦我选择了一个城市,就应该选择相关的分支机构.
我在C#源代码中有一个方法可以清除包含2个下拉列表的表单.因此,一旦我单击清除"按钮,则2下拉列表应该为空:
1-ddlcity显示:"--Select--"
2-ddlBranch显示:"----"
现在的问题是,当我单击清除"按钮时,会出现错误消息:"
Hello,
i have 2 dropdown lists:
1- ddlCity: that display all cities in the database
2-ddlBranch:display all branches per city.
So once i select a city, related branches should be selected.
and i have a method in C# source code that clear my form which contains the 2 dropdownlists. So once i click on the "Clear" button, the 2 dropdown list should be empty:
1-ddlcity display: "--Select--"
2-ddlBranch display: "----"
Now the problem is that When i click on the button "Clear" an error message occurs: "
'ddlBranch' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
请我需要您的帮助,并在此先感谢您.
天冬氨酸净代码:
Please I need your help and thank you in advance.
Asp. Net Code:
<asp:DropDownList ID="ddlCity" runat="server" Width="170px"
DataSourceID="tblCity" DataTextField="city" DataValueField="cityID" AutoPostBack="true" AppendDataBoundItems="true"
onselectedindexchanged="ddlCity_SelectedIndexChanged">
<asp:ListItem Text="--Select--" Value=""></asp:ListItem>
<asp:SqlDataSource ID="tblCity" runat="server" ConnectionString="<%$ ConnectionStrings:SmartBookingEngineConn %>" SelectCommand="SELECT [cityID],[city] FROM [tblCity]"></asp:SqlDataSource>
<asp:DropDownList ID="ddlBranch" runat="server" Width="170px" AutoPostBack="true"
onselectedindexchanged="ddlBranch_SelectedIndexChanged" >
<asp:ListItem Text="----" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
<telerik:RadAjaxManagerProxy ID="rp" runat="server">
</telerik:RadAjaxManagerProxy>
C#代码:
C# code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// dropdown branches per city load
ddlCity.DataBind();
if (ddlCity.SelectedValue !="")
{
dtbranch = null;
string selectedIDCity = ddlCity.SelectedValue;
string mysqlSelectBranch = "select Branch,CityID,BranchID from viewBranchPerCity where CityID=" + selectedIDCity;
dtbranch = db.sqlServer.SelectRecord(mysqlSelectBranch);
ddlBranch.DataSource = dtbranch;
ddlBranch.DataValueField = "BranchID";
ddlBranch.DataTextField = "Branch";
}
ddlBranch.DataBind();
}
rp.AjaxSettings.AddAjaxSetting(ddlCity, ddlBranch, RadAjaxLoadingPanel1);
}
protected void btnClear_Click(object sender, EventArgs e)
{
clearForm();
}
protected void clearForm()
{
ddlCity.Enabled = true;
ddlBranch.Enabled = true;
ddlCity.SelectedValue = string.Empty;
ddlBranch.SelectedValue = string.Empty;
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
dtbranch = null;
string selectedIDCity = ddlCity.SelectedValue;
string mysqlSelectBranch = "select Branch,CityID,BranchID from viewBranchPerCity where CityID=" + selectedIDCity;
dtbranch = db.sqlServer.SelectRecord(mysqlSelectBranch);
ddlBranch.DataSource = dtbranch;
ddlBranch.DataValueField = "BranchID";
ddlBranch.DataTextField = "Branch";
ddlBranch.DataBind();
}
推荐答案
ConnectionStrings:SmartBookingEngineConn%>" SelectCommand =" < > < asp:DropDownList ID =" runat 服务器" 宽度 170像素" AutoPostBack true" span> onselectedindexchanged =" ddlBranch_SelectedIndexChanged" > < asp:ListItem 文本 =" 值 =" 已选择 =" True" < /asp:ListItem > < /asp:DropDownList > < telerik:RadAjaxManagerProxy ID =" =" 服务器" < /telerik:RadAjaxManagerProxy >
ConnectionStrings:SmartBookingEngineConn %>" SelectCommand="SELECT [cityID],[city] FROM [tblCity]"></asp:SqlDataSource> <asp:DropDownList ID="ddlBranch" runat="server" Width="170px" AutoPostBack="true" onselectedindexchanged="ddlBranch_SelectedIndexChanged" > <asp:ListItem Text="----" Value="" Selected="True"></asp:ListItem> </asp:DropDownList> <telerik:RadAjaxManagerProxy ID="rp" runat="server"> </telerik:RadAjaxManagerProxy>
C#代码:
C# code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// dropdown branches per city load
ddlCity.DataBind();
if (ddlCity.SelectedValue !="")
{
dtbranch = null;
string selectedIDCity = ddlCity.SelectedValue;
string mysqlSelectBranch = "select Branch,CityID,BranchID from viewBranchPerCity where CityID=" + selectedIDCity;
dtbranch = db.sqlServer.SelectRecord(mysqlSelectBranch);
ddlBranch.DataSource = dtbranch;
ddlBranch.DataValueField = "BranchID";
ddlBranch.DataTextField = "Branch";
}
ddlBranch.DataBind();
}
rp.AjaxSettings.AddAjaxSetting(ddlCity, ddlBranch, RadAjaxLoadingPanel1);
}
protected void btnClear_Click(object sender, EventArgs e)
{
clearForm();
}
protected void clearForm()
{
ddlCity.Enabled = true;
ddlBranch.Enabled = true;
ddlCity.SelectedValue = string.Empty;
ddlBranch.SelectedValue = string.Empty;
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
dtbranch = null;
string selectedIDCity = ddlCity.SelectedValue;
string mysqlSelectBranch = "select Branch,CityID,BranchID from viewBranchPerCity where CityID=" + selectedIDCity;
dtbranch = db.sqlServer.SelectRecord(mysqlSelectBranch);
ddlBranch.DataSource = dtbranch;
ddlBranch.DataValueField = "BranchID";
ddlBranch.DataTextField = "Branch";
ddlBranch.DataBind();
}
请按以下方式更新您的代码.
Hi,
Please update your code as below.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//ddlCity.DataBind();
if (ddlCity.SelectedValue !="")
{
dtbranch = null;
string selectedIDCity = ddlCity.SelectedValue;
string mysqlSelectBranch = "select Branch,CityID,BranchID from viewBranchPerCity where CityID=" + selectedIDCity;
dtbranch = db.sqlServer.SelectRecord(mysqlSelectBranch);
ddlBranch.DataSource = dtbranch;
ddlBranch.DataValueField = "BranchID";
ddlBranch.DataTextField = "Branch";
ddlBranch.DataBind();
}
//ddlBranch.DataBind(); //Move this line in if Block
}
rp.AjaxSettings.AddAjaxSetting(ddlCity, ddlBranch, RadAjaxLoadingPanel1);
}
protected void btnClear_Click(object sender, EventArgs e)
{
clearForm();
}
protected void clearForm()
{
//ddlCity.Enabled = true;
//ddlBranch.Enabled = true;
ddlCity.SelectedIndex= 0;
ddlBranch.Items.Clear();
ddlBranch.DataBind();
ListItem li = new ListItem("----","");
ddlBranch.Items.Add(li);
ddlBranch.SelectedIndex= 0;
ddlBranch.DataBind();
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
dtbranch = null;
string selectedIDCity = ddlCity.SelectedValue;
string mysqlSelectBranch = "select Branch,CityID,BranchID from viewBranchPerCity where CityID=" + selectedIDCity;
dtbranch = db.sqlServer.SelectRecord(mysqlSelectBranch);
ddlBranch.DataSource = dtbranch;
ddlBranch.DataValueField = "BranchID";
ddlBranch.DataTextField = "Branch";
ddlBranch.DataBind();
}
希望对您有帮助.
Hope this will help you.
您好,您可以使用以下内容清除下拉列表.
Hi, You can use the following for clear dropdown list.
protected void clearForm()
{
ddlCity.Items.Clear();
ddlBranch.Items.Clear();
ddlCity.SelectedValue.Selected = false;
ddlBranch.SelectedValue.Selected = false;
}
谢谢.
Thank You.
这篇关于无法将下拉列表设为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!