asp.net中,我想在页面加载时拿到DropDownLis控件里面的索引值,我用ddlType.SelectedIndex获取的总是0,是为什么?

解决方案 »

  1.   

    同意 1楼取到的是第一项的index他还有很多属性  SelectedValue  SelectedItem之类的  你看好哪个就可以取哪个呀
      

  2.   


    明显没有把绑定放在 ispostback 里面
      

  3.   

    这是代码
    public partial class message_message : System.Web.UI.Page
    {
        private MessageManager mm = new MessageManager();
        private int type; //
        #region 加载
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                type = ddlType.SelectedIndex;//获取dropDownList的索引
                ViewState["page"] = 0;
                MsgBind(type);
            }
        }
        #endregion    #region 数据绑定
        private void MsgBind(int type)
        {
            PagedDataSource pds = new PagedDataSource();
            pds.AllowPaging = true;
            pds.CurrentPageIndex = Pager;
            pds.PageSize = 5;
            lblcurrentPage.Text = "第" + (pds.CurrentPageIndex + 1).ToString() + "页 共" + (pds.PageCount.ToString())+"页";
            pds.DataSource = mm.GetAllMessages(type);        SetEnable(pds);  //用于设置上下按钮的有效状态
            dlMessage.DataSource = pds;
            dlMessage.DataBind();
        }
        #endregion
      

  4.   

    这个 type = ddlType.SelectedIndex;//获取dropDownList的索引
    放到if (!IsPostBack)  {} 的外面就可以了
      

  5.   


    在if (!IsPostBack) {} 里面加载绑定方法