选择DropDownList项目总是返回第一项

时间:2013-01-03 17:10:24

标签: c# asp.net webforms

我似乎遇到了DropDownList的问题。我有一个按钮,应该从选定的DropDownList中正常获取文本,并在按钮单击时在我的代码中进行比较。问题是DropDownList每次都返回第一个项目。这是我的代码:

 <asp:DropDownList ID="SubcategoryList" runat="server" 
                DataSourceID="SubCategoryDataSource" DataTextField="SubCategoryName" 
                DataValueField="CategoryId">
  </asp:DropDownList>
<asp:SqlDataSource ID="SubCategoryDataSource" runat="server" 
                ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True" 
                ProviderName="System.Data.SqlClient" 

                SelectCommand="SELECT [SubCategoryName], [CategoryId] FROM [forum_subcategories] WHERE ([CategoryId] = @CategoryId)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="CategoryListSelect" Name="CategoryId" 
                        PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>

string item = SubcategoryList.SelectedItem.Text;

每当我按下我的按钮而不是项目获取我选择的项目的值时,我会得到第一个项目的值。

我在这里做错了什么?

按钮代码

protected void Button9_Click( object sender , EventArgs e ) {
        try
        {
            Label4.Visible = true;
            Label4.Text = "Subcategory deleted";

            string itemSelected = SubcategoryList.SelectedItem.Text;
            DataAccess.insertDeleteCategory(itemSelected , "forum_delete_subcategory");
            addForumData();
        }catch(Exception ex)
        {
            Label4.Text = ex.Message;
        }

}

0 个答案:

没有答案