2013-06-20 36 views
0

我在一個網頁中有兩個Dropdownlist。第二個依賴於第一個加載。例如,如果第一個是「墨爾本」,則第二個列出墨爾本的所有郊區。在PageLoad上填充兩個Dropdownlist

代碼是完美的工作,但是當我第一次加載頁面時,第二個dropdownlist2不會填充。 我需要再次選擇「墨爾本」以填充第二個下拉列表。

這裏是我的頁面加載

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     if (Session["Username"] == null) 
     { 
      Response.Redirect("LoginPage.aspx"); 
     } 

     if (getAccess(Session["Username"].ToString()) == false) 
     { 
      Response.Redirect("Unauthorized.aspx"); 
     } 

     DataSet ds = GetAllCategory(); 
     if (ds.Tables.Count > 0) 
     { 
      DropDownList1.DataTextField = "identifier"; 
      DropDownList1.DataValueField = "OS_ID"; //Change field to one you want. 
      DropDownList1.DataSource = ds.Tables[0]; 
      DropDownList1.DataBind(); 
     } 
    } 
} 

代碼這裏是選定的索引更改代碼

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    DataSet ds = softwareType(Convert.ToInt32(DropDownList1.SelectedValue)); 
    if (ds.Tables.Count > 0) 
    { 
     DropDownList2.DataTextField = "identifier"; 
     DropDownList2.DataValueField = "ST_ID"; //Change field to one you want. 
     DropDownList2.DataSource = ds.Tables[0]; 
     DropDownList2.DataBind(); 
    } 
} 

我不知道如何解決這個簡單的問題?

+0

是否已經設置的AutoPostBack = 1中真正下拉??? – Anuj

+0

我已經完成了.. –

+0

你想在第二個下拉列表中填充哪個事件? – Anuj

回答

1

試試這個

DataSet ds = GetAllCategory(); 
    if (ds.Tables.Count > 0) 
    { 
     DropDownList1.DataTextField = "identifier"; 
     DropDownList1.DataValueField = "OS_ID"; //Change field to one you want. 
     DropDownList1.DataSource = ds.Tables[0]; 
     DropDownList1.DataBind(); 
    } 

    if(DropDownList1.Items.Count > 0) 
    { 
     DropDownList1.SelectedIndex = 0; 
     DropDownList1_SelectedIndexChanged(this,null); 
    } 
+0

完美.. –

+0

很高興聽到Amrit ....如何投票答案:D –

1

一個想法可以得到從GetAllCategory
綁定在頁面加載第二dropdown list的第一個值。

public void PopulateDropdownList2(int selectedValue) 
{ 
    DropDownList2.Items.Clear(); 
    DataSet ds = softwareType(selectedValue); 
    if (ds.Tables.Count > 0) 
    { 
      DropDownList2.DataTextField = "identifier"; 
      DropDownList2.DataValueField = "ST_ID"; //Change field to one you want. 
      DropDownList2.DataSource = ds.Tables[0]; 
      DropDownList2.DataBind(); 
    } 
} 

通話

if (!Page.IsPostBack) 
{ 

} 

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     if (Session["Username"] == null) 
     { 
      Response.Redirect("LoginPage.aspx"); 
     } 

     if (getAccess(Session["Username"].ToString()) == false) 
     { 
      Response.Redirect("Unauthorized.aspx"); 
     } 

     DataSet ds = GetAllCategory(); 
     if (ds.Tables.Count > 0) 
     { 
      DropDownList1.DataTextField = "identifier"; 
      DropDownList1.DataValueField = "OS_ID"; //Change field to one you want. 
      DropDownList1.DataSource = ds.Tables[0]; 
      DropDownList1.DataBind(); 
      PopulateDropdownList2(Convert.ToInt32(ds.Tables[0].Rows[0]["identifier"].ToString())); 
     } 

    } 
} 
+0

我怎樣才能通過代碼選擇下拉列表中的第一項,以便第二個填充頁面加載 –

+0

讓我試試.. –

0

更新與您的標記上面的功能,是否設置爲Autopost="true"或者您可以使用Ajaxtoolkit's cascading dropdowns

+0

或第一次下拉數據綁定後,您可以調用'DropDownList1_SelectedIndexChanged(null,null);' –

0

嘗試添加下面DropDownList1.DataBind這一行();

DropDownList1.SelectedIndex = 0; 
0

調用這個頁面加載,如果(!的IsPostBack)以及第一DDL

private void fillSecondDdl() 
{ 
DataSet ds = softwareType(Convert.ToInt32(DropDownList1.SelectedValue)); 
    if (ds.Tables.Count > 0) 
    { 
     DropDownList2.DataTextField = "identifier"; 
     DropDownList2.DataValueField = "ST_ID"; //Change field to one you want. 
     DropDownList2.DataSource = ds.Tables[0]; 
     DropDownList2.DataBind(); 
    } 
} 
0

調用這個函數在頁面加載到底

fillSecondDropdown(Convert.ToInt32(DropDownList1.SelectedValue)); 



public void fillSecondDropdown(int firstdropdownValue) 
    { 
    DataSet ds = softwareType(firstdropdownValue); 
     if (ds.Tables.Count > 0) 
     { 
      DropDownList2.DataTextField = "identifier"; 
      DropDownList2.DataValueField = "ST_ID"; //Change field to one you want. 
      DropDownList2.DataSource = ds.Tables[0]; 
      DropDownList2.DataBind(); 
     } 
    } 
0

我的選擇指數變化d將函數從DropDownList1_SelectedIndexChanged()中重構並將其放入一個新函數中:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    ddl1Changed(DropDownList1.SelectedValue); 
} 

public void ddl1Changed(string selectedValue) 
{ 
    DataSet ds = softwareType(Convert.ToInt32(selectedValue)); 
    if (ds.Tables.Count > 0) 
    { 
     DropDownList2.DataTextField = "identifier"; 
     DropDownList2.DataValueField = "ST_ID"; //Change field to one you want. 
     DropDownList2.DataSource = ds.Tables[0]; 
     DropDownList2.DataBind(); 
    } 
} 
在你的Page_Load

然後

public void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     if (Session["Username"] == null) 
     { 
      Response.Redirect("LoginPage.aspx"); 
     } 

     if (getAccess(Session["Username"].ToString()) == false) 
     { 
      Response.Redirect("Unauthorized.aspx"); 
     } 

     DataSet ds = GetAllCategory(); 
     if (ds.Tables.Count > 0) 
     { 
      DropDownList1.DataTextField = "identifier"; 
      DropDownList1.DataValueField = "OS_ID"; //Change field to one you want. 
      DropDownList1.DataSource = ds.Tables[0]; 
      DropDownList1.DataBind(); 
     } 

     ddl1Changed("Melbourne"); 
    } 


}