设置dropdown.selectedvalue时的asp下拉重新加载页面

时间:2015-05-13 13:08:35

标签: c# asp.net

我有以下asp下拉列表:

    <asp:DropDownList ID="ArticleCategories" runat="server" AppendDataBoundItems="true" CssClass="ArticleCategoriesDropDown" onselectedindexchanged="mCategoryItemSelected" AutoPostBack="True"> 
        <asp:ListItem Text="Select Category" Value="" />
    </asp:DropDownList>

完成选择并加载数据后,我想在下拉列表中设置值,以显示用户选择的值。

我这样做是通过:

ArticleCategories.SelectedValue = CategoryID.ToString(); 

第一次工作正常,但从那时起页面就停留在该选择上,因为在新值加载之前加载了所选值。

我尝试通过以下方式禁用itemchangelistener:

ArticleCategories.SelectedIndexChanged += new EventHandler(doNothing);

但这不起作用。

我也尝试禁用回发功能,但也无效。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

您应该只在第一次加载页面时设置选择。在ASP.NET中,您可以通过检查IsPostBack属性:

来实现此目的
if(!IsPostBack)
{
    ArticleCategories.SelectedValue = CategoryID.ToString(); 
}

答案 1 :(得分:0)

你可以做Autopostback = false

func someFunction(externalParameterName localParameterName: Int) {
    // function body goes here, and can use localParameterName
    // to refer to the argument value for that parameter
}

答案 2 :(得分:0)

嘿,在从Dropdown中选择另一个项目之前,您应该清除之前的选择,然后选择另一个。

ArticleCategories.ClearSelection();

希望它对你有所帮助。