Asp.net网络表单下拉列表未更改选定值 [英] Asp.net web form dropdownlist not changing selected value

查看:68
本文介绍了Asp.net网络表单下拉列表未更改选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我用它来更改下拉列表的选定值。这个下拉列表项目不受数据限制,我在设计时已经添加了项目。



I have this below code which I am using to change the selected value of dropdownlist. This dropdownlist items are not data-bound and I have added items to them at design time.

if (dataTable.Rows.Count > 0 && dataTable.Columns.Count > 0)
{
  if (dataTable.Rows[0]["IsSpeechActive"].ToString().Equals(bool.TrueString)) { chkSpeechActive.Checked = true; } else { chkSpeechActive.Checked = false; }
  ddlSL1.SelectedValue = dataTable.Rows[0]["Value1"].ToString(); //en-us
  ddlSL2.SelectedValue = dataTable.Rows[0]["Value2"].ToString(); //1.0
  ddlSL3.SelectedValue = dataTable.Rows[0]["Value3"].ToString(); //1.5
  ddlSL4.SelectedValue = dataTable.Rows[0]["Value4"].ToString(); //default
  ddlSL5.SelectedValue = dataTable.Rows[0]["Value5"].ToString(); //0.5
}
else
{
  ShowMessage("Could not find user speech details");
}







<asp:DropDownList ID="ddlSL5" CssClass="cm-input-dataform-select cm-input-font" runat="server">
  <asp:ListItem Text="0.0" Value="0.0"></asp:ListItem>
  <asp:ListItem Text="0.1" Value="0.1"></asp:ListItem>
  <asp:ListItem Text="0.2" Value="0.2"></asp:ListItem>
  <asp:ListItem Text="0.3" Value="0.3"></asp:ListItem>
  <asp:ListItem Text="0.4" Value="0.4"></asp:ListItem>
  <asp:ListItem Text="0.5" Value="0.5"></asp:ListItem>
  <asp:ListItem Text="0.6" Value="0.6"></asp:ListItem>
  <asp:ListItem Text="0.7" Value="0.7"></asp:ListItem>
  <asp:ListItem Text="0.8" Value="0.8"></asp:ListItem>
  <asp:ListItem Text="0.9" Value="0.9"></asp:ListItem>
  <asp:ListItem Text="1.0" Value="1.0"></asp:ListItem>
</asp:DropDownList>





在上面的代码中,第一个下拉列表工作正常但其他人没有将值更改为我从数据库中获取并将其设置为的值。



请告知出了什么问题?这是一个奇怪的错误。



我尝试过:



我删除了asp.net web表单并重新创建它。



In the code above the first dropdown works fine but others are not changing the value to the one that I am fetching from the DB and setting them to.

Please advise as to what is wrong? A weird error this is.

What I have tried:

I deleted the asp.net web form and re-created it.

推荐答案

首先,如果你在中预先选择值> SelectedIndexChanged 事件 DropDownList ,然后你需要将 AutoPostback 设置为TRUE以触发 SelectedIndexChanged 事件。



其次,如果你有绑定并在预订选择页面_Load event,然后确保将代码包装在!IsPostback 块中。



第三,尝试做类似的事情,根据数据库中的数据预先选择值:



First off, if you are pre-selecting values at SelectedIndexChanged event of DropDownList, then you need to set AutoPostback to TRUE to trigger the SelectedIndexChanged event.

Second, if you are binding and doing the pre-selection at Page_Load event, then make sure you wrap your code within !IsPostback block.

Third, try to do something like this to pre-select value based from the data in your database:

ListItem item = ddlSL5.Items.FindByValue(dataTable.Rows[0]["Value5"].ToString());
if (item != null){
     	ddlSL5.ClearSelection();
     	item.Selected = true;
}





对其余的 DropDownLists 执行相同操作。


下面的代码解决了这个问题



This code below solved the issue

string value = ""; decimal rowvalue;
rowvalue = decimal.Parse(dataTable.Rows[0]["Value5"].ToString());
value = Convert.ToString(rowvalue.ToString("0.0"));
if (ddlSL5.Items.FindByValue(value) != null) { ddlSL5.Items.FindByValue(value).Selected = true; }


这篇关于Asp.net网络表单下拉列表未更改选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆