从下拉列表中自动将所选项目中的标签中的值归档 [英] automaticaly filing values in label on seleting item from drop down list

查看:125
本文介绍了从下拉列表中自动将所选项目中的标签中的值归档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库中选择填充下拉列表

然后从下拉列表中选择项目,我想在标签中显示两个值

在哪里放置该代码,以便标签自动适合从下拉列表中选择的项目.

这是代码.我试图将代码放入dropdownlist和页面加载中,但未在标签中显示数据.它仅在按钮事件中起作用.但我希望它从下拉列表中选择项目自动运行

i am seleting the filling dropdownlist from database

and then on the slecting the item from dropdown list i want to show the two values in the label

where to put that code so that labels fit automatically on selecting items from dropdown list..

here is the code. i tried to put the code in dropdownlist , and page load but not showing data in label. it only works in button event. but i want it to run autmatically on select the item from dropdownlist

string query = "select * from Supplier where SupplierName='" + DropDownList1.Text + "' ";
      DataSet ds = obj.fillgrid(query);
      DataTable dt = ds.Tables[0];

      foreach (DataRow dr in dt.Rows)
      {
          //lblcustomerID.Text = dr["SupplierName"].ToString();

          lblcell.Text = dr["SupplierCell"].ToString();
          lbladdress.Text = dr["SupplierAddress"].ToString();
          //lblcompany.Text = dr["Company"].ToString();

      }

推荐答案

您可以使用DropDownList控件的SelectedIndexChanged事件.
在Page_Load
中执行 1)将DropDownList的AutoPostBack属性设置为true
1)加载DropDownList项目.
2)选择默认值为DDL,为所选的DDL项目设置文本标签.

做SelectedIndexChanged事件
1)将发送方(第一个)参数强制转换为DropDownList.使用发件人上的SelectedIndex访问选定的项目.
或做(发送者为DropDownList).Text.
2)将选定的项目文本传递给sql并检索值,然后填充标签.

注意:还有其他更好的解决方案也可用于ex.使用ajax.
You can use SelectedIndexChanged event of the DropDownList control.
Do in Page_Load
1)Set AutoPostBack property to true for DropDownList
1) Load the DropDownList items.
2) Select default value to DDL, set text label for the selected DDL item.

Do SelectedIndexChanged event
1) cast the sender (first) parameter to DropDownList. Access the item being selected using SelectedIndex on sender.
Or do (sender as DropDownList).Text.
2) pass the selected item text to sql and retrieve the values then populate the labels.

NOTE: there are other better solutions are also available for ex. using ajax.


这篇关于从下拉列表中自动将所选项目中的标签中的值归档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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