返回首页

大家好,

我的工作asp.net。
我添加一个DropDownList和一个按钮的页面。我显示的DropDownList从数据库中的项目。当我选择从DropDownList的项目上点击按钮,它应该只从DropDownList中显示的第一项。

这里是我的代码

 protected void Page_Load(object sender, EventArgs e)

    {

        ddlCategory.DataSource = DBCategory.GetAllCategory();

        ddlCategory.DataValueField = "CategoryId";

        ddlCategory.DataTextField = "CategoryName";

        ddlCategory.DataBind();

}

protected void Search_Click(object sender, EventArgs e)

    {

        int i = ddlCategory.SelectedValue;

}

"我"是从DropDownList中显示的第一个项目时,我选择在DropDownList中的任何项目..

请告诉我的解决方案......|纳拉扬Shanubhog

回答

评论会员:游客 时间:2012/02/06
什么不负的是,当你点击按钮的Pa​​ge_Load被调用,这将再次填补你的DropDownList所选项目重置为了避免这种行为,把pageLoad的代码!的IsPostBack例如:codeprelang="cs"spanclass="code-keyword"protected/spanspanclass="code-keyword"void/spanPage_Load(spanclass="code-keyword"object/spansender,EventArgse){spanclass="code-keyword"if/span(!isPostBack){ddlCategory.DataSource=DBCategory.GetAllCategory();ddlCategory.DataValueField=spanclass="code-string""/spanspanclass="code-string"CategoryId"/span;ddlCategory.DataTextField=spanclass="code-string""/spanspanclass="code-string"CategoryName"/span;ddlCategory.DataBind();}}/pre/code
王子安东尼G
评论会员:{的C2} 时间:2012/02/06