【问题标题】:how to dynamically set the value of a cell in gridview based on selection of dropdownlist?如何根据下拉列表的选择动态设置gridview中单元格的值?
【发布时间】:2017-07-22 14:33:37
【问题描述】:

我有一个带有下拉列表和页脚文本框的网格视图。我的下拉列表绑定到一个名为 Tax 的表。要求是将文本框的值设置为与下拉列表中选择的税名相对应。我已成功绑定下拉列表,但无法将文本框的值设置为其相关值。

DropDownList ddlTax = (DropDownList)TaxGV.FooterRow.FindControl("Footer_ddlTaxName");

        //fill the dropdown
        FillDropDownListTaxName(ddlTax);


public void FillDropDownListTaxName(DropDownList ddl)
    {
        string str = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
        SqlConnection con = new SqlConnection(str);
        con.Open();
        SqlCommand cmd = new SqlCommand("Select Id, Name from Taxes", con);
        cmd.CommandType = CommandType.Text;
        ddl.DataSource = cmd.ExecuteReader();
        ddl.DataTextField = "Name";
        ddl.DataValueField = "Id";
        ddl.DataBind();
        ddl.Items.Insert(0, new ListItem("--Select--", "0"));
    }

现在剩下的问题是,当在下拉列表中选择一个值时,如何在页脚文本框中显示相关值。 任何人都可以帮忙吗?

【问题讨论】:

  • 你想用javascript还是往返服务器?
  • 你选择了下拉列表的indexchanged事件吗?您可以使用它从下拉列表中检索选定的值并将其设置为文本框。
  • @GarrGodfrey 我想把它作为到服务器的往返。
  • @ChetanRanpariya 我将使用 selectedindexchanged 事件,但是,我将如何为“Value”文本框赋值?
  • 您可以像查找下拉列表一样查找文本框并设置其值。

标签: c# asp.net gridview


【解决方案1】:

在深入研究 cmets 后,我找到了解决方案。感谢 Chetan Ranpariya。这是我的答案。

protected void Footer_ddlTaxName_SelectedIndexChanged(object sender, EventArgs e)
    {
        string str = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
        SqlConnection con = new SqlConnection(str);
        con.Open();
        SqlCommand cmd = new SqlCommand("Select Value from Taxes where Id=@TaxId", con);
        DropDownList ddl_Tax = (DropDownList)TaxGV.FooterRow.FindControl("Footer_ddlTaxName");
        cmd.Parameters.AddWithValue("@TaxId", ddl_Tax.SelectedValue);
        SqlDataReader reader = cmd.ExecuteReader();
        TextBox tb_Value = (TextBox)TaxGV.FooterRow.FindControl("Footer_txtValue");
        if(reader.Read())
        {
            tb_Value.Text = Convert.ToString(reader["Value"]);
        }
    }

【讨论】:

    猜你喜欢
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 2013-09-23
    • 2017-03-19
    • 1970-01-01
    • 2015-07-06
    相关资源
    最近更新 更多