我有一个asp.net的dropdownlist控件,我想跟踪dropdownlist中选择的项目。让我展示我的所作所为。
这是DropdownList控件:
<asp:DropDownList ID="Dd_Cat" runat="server"
DataValueField="CatId" DataTextField="CatName"
OnSelectedIndexChanged="wtf" >
</asp:DropDownList>
这是后面的代码,用于填充dropdownlist
from数据库。
protected void bindDropdown()
{
string mystr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
SqlConnection con = new SqlConnection(mystr);
con.Open();
SqlCommand cmd = new SqlCommand("AddCat", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@action", "select");
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Dd_Cat.DataSource = ds;
Dd_Cat.DataBind();
con.Close();
con.Dispose();
}
控件的onselected
属性dropdownlist
:
protected void wtf(object sender, EventArgs e)
{
string st = Dd_Cat.SelectedValue;
}
这总是显示值“ 1” nomatter选择了什么。请帮助。
我假设您bindDropdown
在Page_Load
不使用!IsPostBack
-check的情况下进行呼叫。然后,您将始终加载默认值,并且SelectedIndexChanged
不会触发该事件。
因此,如果EnableViewState
将其设置为true
(默认值),请使用此选项:
protected void Page_Load(Object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// page is loaded the first time, load from database
bindDropdown();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句