c# - DropDownList with SelectedIndex=0 -
I write a simple program that connects to the database and reads data from two tables and has two dropdown lists DDL). DDL has structures like parent and child, this is my code:
protected zero page_load (Object Sender, EventEurge E) {FillControls (); If (! Ipostback) {ddl_SelectedIndexChanged (dropdown list) placeholder 1. FontControl ("DDLTurrentory"), blank); } Else {if (((DropDownList) PlaceHolder1.FindControl ( "ddlTurCountry")) SelectedIndex == 0.) Ddl_SelectedIndexChanged ((DropDownList) PlaceHolder1.FindControl ( "ddlTurCountry"), NULL); // This string does not call ddl_SelectedIndexChanged () without drop when it is selectedIndex = 0}} Private Zero FillControls () {Panel Panel = New Panel (); Labeled lbl = new label (); Lbl.Text = "Country:"; Panel.Controls.Add (LbL); Dropdown list ddl = new DropDownList (); Ddl.ID = "ddlTurCountry"; Ddl.DataTextField = "Country Names"; Ddl.DataValueField = "Country Names"; Ddl.SelectedIndexChanged + = new event handler (ddl_SelectedIndexChanged); Ddl.AutoPostBack = True; Panel.Controls.Add (DDL); Panel. Control. Add (New Little Control ("& lt; br / & gt;")); Lbl = new label (); Lbl.Text = "Crow:"; Panel.Controls.Add (LbL); Ddl = new DropDownList (); Ddl.ID = "ddlTurKurort"; Ddl.DataTextField = "KurortName"; Ddl.DataValueField = "KurortName"; Panel.Controls.Add (DDL); PlaceHolder1.Controls.Add (panel); FillDDL ("ddlTurCountry", "Country Describe Country"); } Private void ddl_SelectedIndexChanged (object sender, EventArgs e) {if (((DropDownList) sender) .id == "ddlTurCountry") {FillDDL ( "ddlTurKurort", "Kurorts.KurortName elected within Kurorts in Countries.idCountry = countries Join Kurorts.idCountry WHERE (Deshkdesh name = n '+ ((dropdown menu) placeholder 1. Frntkantrol ( "Deedieltiuarti")). Cynitwu + "')");}} private void FillDDL (string Deedielaidi, string SQLCommand) {((DropDownList) PlaceHolder1.FindControl (ddlID)). DataSource = GetDataFromDB (SQLCommand);. ((DropDownList) PlaceHolder1.FindControl (ddlID)) DataBind ();} private Detatabl GetDataFromDB (string C SQLCommand) {DataTable dt = New DataTable (); String sqlcnString = @ "Data Source =. \ SQLEXPRESS; Initial catalog = tour; Integrated security = true; Pooling = false "; SqlConnection sqlcn = new SqlConnection (sqlcnString); if (sqlcn.State.ToString () ==" off ") sqlcn.Open (); SqlDataAdapter data adapter = new SQL data adapter (SQLCommand, sqlcn); dataAdapter.Fill Dt; sqlcn.Close (); return dt;}
This code does not work correctly. When I run this program it starts well and the data Two DDLs are filled out. Then I start changing the value in DDL first and it works, but it only works when I do not select the first value.
If I do not first select the first value to fill the DDL. When I look with the debugger, I think the method ddl_SelectedIndexChanged
then applies In the case I said this line:
if (((DropDownList) PlaceHolder1.FindControl ("ddlTurCountry")) SelectedIndex == 0, if the selected DDL equals zero. ) Ddl_SelectedIndexChanged (DropDownList) PlaceHolder1.FindControl ("ddlTurCountry"), zero); in Page_load
, but I do not like it , And I do not understand why it does not work when selectedIndex = 0
.
Resolved
Protected Zero Page_Init (Object Sender, EventArgs e) {FillControls () ; } Zero Page_Load (object sender, EventArgs e) protected {if ddl_SelectedIndexChanged (DropDownList) PlaceHolder1.FindControl ("ddlTurCountry"), tap) (IsPostBack!); }
Every time your page is refreshed, you pop the drop down list Do not do this just call FillControls ()! Page.Postback
Protected Zero Page_load (Object Sender, EventArgs e) {if (IsPostBack) {FillControls (); // Make sure you have it here, probably ddl_SelectedIndexChanged is not required (DropDownList) PlaceHolder1.FindControl ("ddlTurCountry"), blank); } Else {}}
Comments
Post a Comment