从下拉列表中填充文本框 [英] Populate the textbox from a dropdownlist

查看:95
本文介绍了从下拉列表中填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个下拉列表和一个文本框。为此我有一个名为mrp的表,其中包含三个字段(MRP,ITEM,RATE)。我用MRP填充的第一个下拉列表。无论选择哪个MRP,根据MRP的项目,第二个下拉列表将填充。现在,在从第二个下拉列表中选择项目时,项目的速率应显示在文本框中。

我已经填写了下拉列表。任何人都可以告诉我如何使用第二个下拉列表中所选项目的速率填充文本框。我想要答案在c#



我尝试过:



i have 2 dropdownlist and one textbox.for this i have a table named mrp which contain three fields(MRP,ITEM, RATE). the first dropdownlist i populated with MRP. whichever MRP is selected ,on the basis of that MRP's items the second dropdownlist will populate. now on the selection of the items from the second dropdownlist the rate of the items should be shown in the textbox.
i have done populating both the dropdown. can anyone tell me how i populate the textbox with rate of the selected item in the second dropdownlist. i want the answer in c#

What I have tried:

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack) 
  { 
    SqlConnection con = new SqlConnection("Data Source=MICKEY\\SQLEXPRESS;Initial Catalog=rr;Integrated Security=True");
    SqlCommand com = new SqlCommand("select distinct MRP from mrp");
    // SqlDataReader sdr;
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(com.CommandText, con);
    con.Open();
    da.Fill(ds,"mrp");
    ddlmrp.DataSource = ds.Tables[0];
    ddlmrp.DataTextField = ds.Tables[0].Columns["MRP"].ColumnName;
    ddlmrp.DataValueField = ds.Tables[0].Columns["MRP"].ColumnName;
    ddlmrp.DataBind();
    //ddlmrp.SelectedValue = 0;

    //ddlmrp.Items.Insert(0, new ListItem("--Select--", "0"));
   }
}

protected void ddlmrp_SelectedIndexChanged(object sender, EventArgs e)
{
  SqlConnection con = new SqlConnection("Data Source=MICKEY\\SQLEXPRESS;Initial Catalog=rr;Integrated Security=True");
  SqlCommand com = new SqlCommand("select ITEM from mrp where mrp = '"+ddlmrp.SelectedValue.ToString()+"'", con);
  // SqlDataReader sdr= new SqlDataReader();
  DataSet ds = new DataSet();
  SqlDataAdapter da = new SqlDataAdapter(com);
  con.Open();
  if (ds.Tables.Contains("mrp"))
  {
    ds.Tables[0].Reset();
  }
  da.Fill(ds, "mrp");
  ddlitems.DataSource = ds.Tables[0];
  ddlitems.DataTextField = ds.Tables[0].Columns["ITEM"].ColumnName;
  ddlitems.DataValueField = ds.Tables[0].Columns["ITEM"].ColumnName;
  ddlitems.DataBind();

  //TextBox1.Text = sdr["select RATE FROM mrp where ITEM ='"+ddlitems.SelectedValue.ToString()+"'",con].ToString();

  con.Close();
}

推荐答案

对项目下拉列表使用选择更改事件,并在项目下拉列表更改时获取所选项目ID,使用这个id用sql传递来获取相应项目的费率。

Use selection changed event for item dropdownlist, and get the selected item id when item dropdown list changing, use this id to pass with sql to get the rate of the corresponding item.
protected void ddlitems_SelectedIndexChanged(object sender, EventArgs e)
{
  ListItem objItem = ddlitems.SelectedItem;
  string selectedItemId = objItem.Text;
  SqlConnection con = new SqlConnection("Data Source=MICKEY\\SQLEXPRESS;Initial Catalog=rr;Integrated Security=True");
  SqlCommand com = new SqlCommand("select RATE from mrp where item = '" +  selectedItemId + "'", con);
  DataTable PriceTable = new DataTable();
  SqlDataAdapter da = new SqlDataAdapter(com);
  con.Open();
  da.Fill(PriceTable);
  con.Close();

  TextBox1.Text = PriceTable.Rows[0][0].ToString().Trim();
}


这篇关于从下拉列表中填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆