IsPostBack and DropdownList.

Encounted the issue accident when helping my classmate dealing with his homework assignment,it turns out that I have to consider about 'IsPostBack' when trying to combind the 'DropdownList' in code behind(or it won't get what we expect):

 1 protected void Page_Load(object sender, EventArgs e) {
 2         if (!IsPostBack) {
 3             Response.Write("<script>alert('Is not PostBack')</script>");
 4             ddlstSinger.Items.Clear();
 5             adapter = new t_musicTableAdapter();
 6             var data = from d in adapter.GetData().AsQueryable()
 7                        group d by d.singer
 8                            into mySinger
 9                            select mySinger.First();
10             foreach (var d in data) {
11                 ddlstSinger.Items.Add(d.singer);
12             }
13         }
14     }
15     protected void btnSearch_Click(object sender, EventArgs e) {
16         if (ddlstSinger.SelectedItem == null) {
17             Alert("Please enter the search condition!");
18             return;
19         }
20         lbInvisible.Text = ddlstSinger.Text;
21         lvwSearch.DataSourceID = "ObjectDataSrcSearch";
22         lvwSearch.DataBind();
23     }

 

posted @ 2014-03-02 09:12  wonkju  阅读(228)  评论(0编辑  收藏  举报