DropDownList值问题,在线等!
protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack)
{
string typeIdStr = Request.QueryString["typeId"];
this.drpChannel.DataSource = ChannelManager.GetChannelName();
this.drpChannel.DataTextField = "channelName";
this.drpChannel.DataValueField = "channelId";
this.drpChannel.DataBind();
this.drpCountry.DataSource = CountryManager.GetCountryName();
this.drpCountry.DataTextField = "countryName";
this.drpCountry.DataValueField = "countryId";
this.drpCountry.DataBind();
int typeId = Convert.ToInt32(typeIdStr);
string countryName = CountryManager.GetCountryByTypeId(typeId).CountryName;
string channelName = ChannelManager.GetChannelNameByTypeId(typeId).ChannelName;
//this.drpCountry.SelectedItem.Text = countryName;
//this.drpChannel.SelectedItem.Text = channelName;
for (int i = 0; i < drpChannel.Items.Count; i++)
{
if (drpChannel.Items[i].Text == channelName)
{
drpChannel.Items[i].Selected = true;
}
}
for (int i = 0; i < drpCountry.Items.Count; i++)
{
if (drpCountry.Items[i].Text == countryName)
{
drpCountry.Items[i].Selected = true;
}
}
this.txtCost.Text = Convert.ToString(CostManager.GetCostByTypeId(typeId).Cost);
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
int channelId = Convert.ToInt32(this.drpChannel.SelectedItem.Value);
int countryId = Convert.ToInt32(this.drpCountry.SelectedItem.Value);
string channelName = this.drpChannel.SelectedItem.Text;
string countryName = this.drpCountry.SelectedItem.Text;
}
为什么无论怎么选择dropdownlist下的值,都只显示是第一个? --------------------编程问答-------------------- 给DropDownList赋值最好用SelectedValue
你可以获取到countryId
然后直接his.drpCountry.Selectedvalue=countryId; --------------------编程问答-------------------- 修改完了 绑定下 --------------------编程问答--------------------
楼上正解了 --------------------编程问答-------------------- ++ --------------------编程问答-------------------- Up++
--------------------编程问答-------------------- drpChannel.Items[i].Selected = true;........
设置Selectedvalue或者SelectedIndex --------------------编程问答-------------------- /// <summary>
/// 选中指定列表框和选中值的列表项
/// </summary>
/// <param name="list"></param>
/// <param name="selVal"></param>
/// <param name="isText"></param>
public void SelDropDownItem(DropDownList list, string selVal, bool isText)
{
if (string.IsNullOrEmpty(selVal))
{
return;
}
ListItem selItem = null;
if (isText)
{
selItem = list.Items.FindByText(selVal);
}
else
{
selItem = list.Items.FindByValue(selVal);
}
if (selItem != null)
{
list.SelectedIndex = -1;
selItem.Selected = true;
}
} --------------------编程问答-------------------- LZ真的在线等吗? --------------------编程问答-------------------- 一个是item.text,一个是values
注意ispostback
补充:.NET技术 , ASP.NET