Change value of a textbox based on the selected item from a Dropdownlist in asp.net using C#?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a created a mini web app, the goal is to have the price displayed on a textbox. I was already able to populate the dropdownlist with the product names what I want to happen is there would be a textbox that would display the price of the current selected
item from the dropdownlist. I was already able to display the price on pageload but when I select a different item from the dropdownlist the price doesnt not change. The first items price is display on pageload.
<img src="http://social.microsoft.com/Forums/getfile/594/" alt="

codes are displayed below.



Populate the DDL using my own class:

<pre> public DropDownList populateProducts2(DropDownList ddl)
{
string select = "Select ProductName From ProductInfo";
adapter = new SqlDataAdapter(select, conString);
dt = new DataTable();
adapter.Fill(dt);
/*for (int i = 0; i < dt.Rows.Count; i++)
{
string productName = dt.Rows[0].ToString();
//string lastname = dt.Rows[1].ToString();
ddl.Items.Add(productName);

}*/
ddl.DataSource = dt;
ddl.DataTextField = "ProductName";
ddl.DataBind();

return ddl;
}[/code]


Page Load

<pre>protected void Page_Load(object sender, EventArgs e)
{
//sql.getOrder(GridView1);

sql.populateCustomer(DropDownList1);
sql.populateProducts2(DropDownList2);
textProdPrice.Text = sql.getProductPrice(DropDownList2.SelectedValue);




}[/code]
<br/>
To get the price


<pre> public string getProductPrice(string ProductName)
{
string select = "Select Price From ProductInfo where ProductName = "+ProductName+"";
adapter = new SqlDataAdapter(select, conString);
dt = new DataTable();
adapter.Fill(dt);
//for (int i = 0; i < dt.Rows.Count; i++)
//{
//string productName = dt.Rows[0].ToString();
//string lastname = dt.Rows[1].ToString();
//ddl.Items.Add(productName);

//}
string price = dt.Rows[0][0].ToString();
return price;


}[/code]
<hr class="sig Randel Ramirez

View the full article
 
Back
Top