[英]DropDownList does not postback on SelectedIndexChanged
I am attempting to get a DropDownList to AutoPostBack when the selected item is changed. 更改所选项目时,我尝试将DropDownList设置为AutoPostBack。 I'm going a little stir-crazy as to why this isn't working.
我对这为什么不起作用有些疯狂。
Does anyone have any quick ideas?Please Help 有人有任何快速的想法吗?请帮助
Here is code:- 这是代码:-
<asp:DropDownList ID="ddl_username" runat="server"
AutoPostBack="True"
onselectedindexchanged="ddl_username_SelectedIndexChanged"
DataTextField="Shortname" DataValueField="ID">
</asp:DropDownList>`
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind_Users();
}
}
private DataSet Bind_Users()
{
DataSet ds;
ds = cs.BindUsers();
if (ds.Tables[0] != null)
{
if (ds.Tables[0].Rows.Count > 0)
{
ddl_username.DataSource = ds.Tables[0];
ddl_username.DataTextField = "Shortname";
ddl_username.DataValueField = "ID";
ddl_username.DataBind();
ListItem li = new ListItem();
li.Text = "Select";
li.Value = "-1";
ddl_username.Items.Insert(0, li);
ddl_username.SelectedIndex = 0;
}
//ddl_username.SelectedIndex = 0;
//ddl_username.Text = "-----Select-----";
}
return ds;
}
Have you checked if the initial loading of the ddl occurs again after the repost? 您是否检查过重新发布后是否再次发生了ddl的初始加载? Just put a debug break-point in your selected-index-changed method and one in your oninit/page-load method - whereever you load the data to the ddl.
只要将调试断点放在您的selected-index-changed方法中,将一个调试断点放在oninit / page-load方法中,就可以将数据加载到ddl。 if it loads again before firing your event - you need to put an
如果在触发您的事件之前再次加载-您需要放置一个
if(!IsPostBack)
before the load method - like: 加载方法之前-如:
if(!IsPostBack)
this.MyDDL.DataSource = GetDatasourceForMyDDL();
Page_Load
is called each time page do postback , so you have to put Bind_Users()
inside a 页面每次回发时都会调用
Page_Load
,因此您必须将Bind_Users()
放在
if(!IsPostBack)
block , because Page_Load
is called before ddl_username_SelectedIndexChanged
if(!IsPostBack)
块,因为在ddl_username_SelectedIndexChanged
之前ddl_username_SelectedIndexChanged
Page_Load
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.