简体   繁体   中英

how to dynamically set the value of a cell in gridview based on selection of dropdownlist?

I have a gridview with a dropdown and textbox in footer. My dropdownlist is bounded to a table named Tax. Requirement is to set the value of textbox to corresponding to selection of the tax name in dropdown. I've successfully binded the dropdownlist but can't get value of textbox to be set to its related value.

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

Now just remaining question is how to show related value in footer textbox, when a value is selected in dropdown. Can Anybody help; 在此处输入图片说明

I have worked out a solution after going thorougly in comments. Thanks to Chetan Ranpariya. Here is my findings for the answer.

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"]);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM