【问题标题】:asp.net generate Gridview by DropDownList IndexChangeasp.net 通过 DropDownList IndexChange 生成 Gridview
【发布时间】:2016-09-21 15:18:41
【问题描述】:

我正在尝试生成一个GridView 并用DropDownList SelectedValue 填充它

我有一个 DropDownList 绑定到 Country 数据表,GridView 绑定到 State 数据表,并通过更改 DropDownList 中的数据值GridView 也变了

我试过这段代码: 这填充下拉列表:

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();
    }
}

这会生成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();
}

最后这就是我调用函数的地方:

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

    }
}



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

.aspx 文件:

<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>

但是代码不起作用,当我更改DropDownList 的值时,GridView 没有改变

【问题讨论】:

  • 在 BindGrid() 中设置断点并单步执行代码。会发生什么?dt.Rows.Count 的值是多少?
  • DropDownListCountry_SelectedIndexChanged 是否触发?

标签: c# html asp.net gridview drop-down-menu


【解决方案1】:

为您的下拉列表设置AutoPostBack = true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    相关资源
    最近更新 更多