开发者

dropdownlist onselectedIndexChanged problem

开发者 https://www.devze.com 2023-03-28 10:05 出处:网络
I placed a dropdown list in my asp page <asp:DropDownList ID=\"authorList\" runat=\"server\" OnSelectedIndexChanged=\"authorList_Changed\" AutoPostBack=\"true\"

I placed a dropdown list in my asp page

 <asp:DropDownList ID="authorList" runat="server"     
  OnSelectedIndexChanged="authorList_Changed" AutoPostBack="true"
   ></asp:DropDownList>

Then in codebehind, I bind it to a filed in database and then fire selectedindexchanged

  protected void Page_Load(object sender, EventArgs e)
    {

        var a = (from s in p.Authors
                 select s.FirstName);
        authorList.DataSource = p.Authors;
        authorList.DataTextField = "Firstname";
        authorList.DataValueField = "FirstName";
        authorList.DataBind();
        authorList.SelectedIndexChanged += new EventHandler(authorList_Changed); 
        var q = (from s in p.Authors
                 where s.FirstName.Contains("m") 
                 select s);
        Grid1.DataSource = q;
        Grid1.DataBind();
       // authorList.DisplayMember = "FirstName";
    }
    public void authorList_Changed(Object sender, EventArgs e)
    {
        //Author a = (Author) authorList.SelectedItem;   
        var a = authorList.SelectedValue;
        var v = authorList.SelectedItem;
        var q2 = (from s in p.Authors
                  from w in p.Payrolls
                  where authorList.SelectedValue == s.FirstName
                  where s.AuthorID == w.AuthorID
                  select w);
        List<Payroll> d = q2.ToList();
        if (d.Count > 0)
        {
            payroltextbox.Text = d.First().PayrollID.ToString();
            //authorList.DataBind();
        }
        else
            payroltextbox.Text = "";
           // authorList.DataBind();

    }
    PublishingCompanyEntities p = new PublishingCompanyEntities();

But the problem has been that when my dropdown has values say 1 2 3 4 ....... 1 bring default....... So, when I select 4 it still returns 1 on postback and return the value in textbox(payroltextbox) associated with 1.. Can u ple开发者_如何转开发ase help me.....


It's because you're forgetting to check for Page.IsPostback in Page_Load...

Page_Load happens each time the page is loaded, even if it's on postback, so the firstdrop-down is being repopulated BEFORE the authorList_Changed event. This is causing the firtst item to be re-selected.

Refer to the Page Lifecycle.

and put your databinding code in the Page_Load inside an if statement like so:

  protected void Page_Load(object sender, EventArgs e)
    {
       if(!Page.IsPostback)
       {
          var a = (from s in p.Authors
                   select s.FirstName);
          authorList.DataSource = p.Authors;
          authorList.DataTextField = "Firstname";
          authorList.DataValueField = "FirstName";
          authorList.DataBind();
          authorList.SelectedIndexChanged += new EventHandler(authorList_Changed); 
          var q = (from s in p.Authors
                   where s.FirstName.Contains("m") 
                   select s);
          Grid1.DataSource = q;
          Grid1.DataBind();
         // authorList.DisplayMember = "FirstName";
       }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消