2016-05-24 11 views
2

Ich versuche, eine GridView zu erzeugen und füllen es durch DropDownListSelectedValueasp.net erzeugen Gridview von Dropdownlist-Indexchange

ich ein DropDownList binden an Land Datentabelle und GridView binden an Staat Datentabelle haben und durch den Wert der DropDownList ändern der Daten in der GridView Änderung zu

i'v versucht, diesen Code: dies die Dropdownlist zu füllen:

protected void FillDropdownList() 
{ 
    SqlCommand cmd = new SqlCommand(); 
    SqlDataAdapter adp = new SqlDataAdapter(); 
    DataTable dt = new DataTable(); 
    try 
    { 
     cmd = new SqlCommand("Select * from Country", con); 
     adp.SelectCommand = cmd; 
     adp.Fill(dt); 
     DropDownListCountry.DataSource = dt; 
     DropDownListCountry.DataTextField = "CountryName"; 
     DropDownListCountry.DataValueField = "CountryID"; 
     DropDownListCountry.DataBind(); 
     //DropDownListCountry.Items.Insert(0, "-- Select --"); 
     //OR ddlEmpRecord.Items.Insert(0, new ListItem("Select Emp Id", "-1")); 
    } 
    catch (Exception ex) 
    { 
     ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true); 
    } 
    finally 
    { 
     cmd.Dispose(); 
     adp.Dispose(); 
     dt.Clear(); 
     dt.Dispose(); 
    } 
} 

und dies die gridView

protected void BindGrid() 
{ 

    con.Open(); 
    SqlCommand com = new SqlCommand("select * from State where CountryID='" + DropDownListCountry.SelectedValue + "'", con); 
    SqlDataAdapter sda = new SqlDataAdapter(com); 

    DataTable dt = new DataTable(); 
    sda.Fill(dt); 
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
    con.Close(); 
} 

und schließlich das ist, zu erzeugen, wo ich die Funktionen bin Aufruf:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!Page.IsPostBack) 
    { 
     FillDropdownList(); 

    } 
} 



protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    BindGrid(); 
} 

die ASPX-Datei:

<asp:DropDownList ID="DropDownListCountry" runat="server" DataTextField="CountryName" DataValueField="CountryID" OnSelectedIndexChanged="DropDownListCountry_SelectedIndexChanged" > 
 
     </asp:DropDownList> 
 
     <br /> 
 

 
        <!-- SqldataSource and GridView and Formview for State --> 
 

 
     <br /> 
 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> 
 
      <Columns> 
 
     <asp:BoundField HeaderText="State Id" DataField="StateID" /> 
 
     <asp:BoundField HeaderText="State Name" DataField="StateName" /> 
 
     <asp:BoundField HeaderText="Country Id" DataField="CountryID" /> 
 
     
 
     </Columns> 
 
     </asp:GridView>

aber der Code nicht funktioniert und wenn ich den Wert von DropDownList Ändern der GridView ändert mich nicht

+0

Platz einer Bruchstelle in BindGrid() und Schritt für Schritt durch der Code.Was passiert? Was ist der Wert von dt.Rows.Count? –

+0

Wird DropDownListCountry_SelectedIndexChanged ausgelöst oder überhaupt nicht? –

Antwort

1

Für Ihre Dropdownliste gesetzt AutoPostBack = true

+0

Es funktioniert! Danke – Bayan