我有一个带有下拉列表的网格视图和页脚中的文本框。我的下拉列表以一个名为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"));
}
答案 0 :(得分:0)
我在评论中做了大量的工作后得出了一个解决方案。感谢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"]);
}
}