我如何使用asp.net中的下拉列表 [英] how can i use drop down list in asp.net

查看:75
本文介绍了我如何使用asp.net中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过下拉选择访问记录,并在标签和文本框中显示从下拉列表中选择的记录.

I want to access record by drop down selection and display record on label and text box as record selected from drop down list.

推荐答案

您好Shivesh,

您所有的代码都是正确的.但是你忘记了一件事.将dd1.DataBind()放在刚要关闭连接时的位置.
这样的东西


Hi Shivesh,

All of your code is correct. But you have forgotten one thing. Put dd1.DataBind(), just befor when you are closing the connection.
Something like this


ddl1.DataSource = datareader; 
ddl1.DataValueField = "product_id"; 
ddl1.DataTextField = "product_id"; 
dd1.DataBind();   //<------
cmd.Connection.Close(); 
cmd.Connection.Dispose(); 




如果可以解决您的问题,请提供投票.

阿努拉格
@cheers @




Please provide Vote if this would solve your problem.

Anurag
@cheers@



SHIVESH

我对您的要求不满意,我的假设如下,请确认:

1 .您是否想要一个包含(1 ... n)之类的记录数量的下拉列表?

2 .您还需要Dropdown中来自数据库的其他内容,例如
在数据库中,您有一些唯一的ID字段,例如(UserID,ProductID,ID)?

3 .从下拉菜单中选择任何项目时,您将不得不根据选择显示记录?


如有任何疑问,请让我知道.

如果对您有帮助,请提供投票.

谢谢,
Imdadhusen
Hi,
SHIVESH

I am not cleared about your requirement, my assumptions are follows please confirm:

1. Do you want a Dropdown list which contains number of records like (1...n)?

2. You want something else in Dropdown which is coming from database like
in database you have some unique id field like (UserID, ProductID, ID)?

3. when selecting any items from Dropdown, you will have to display records based on selection?


Please do let me know, if you have any doubt.

Please provide Vote if this would be helpful to you.

Thanks,
Imdadhusen


您好Shivesh,

请执行以下步骤:
我假设您有一个包含以下字段的表:
表名称:客户
字段:CustomerID
客户名称
购买的商品
ItemQuantity
ItemPrice


1个绑定下拉列表与客户ID
Hi Shivesh,

Do the following steps:
I am assuming you have one table with following fields:
Table Name: Customer
Fields: CustomerID
CustomerName
ItemPurchased
ItemQuantity
ItemPrice


1 Bind dropdown with customer id
strSQL = "SELECT  CustomerID,CustomerName FROM Customer";
            dt = db.GetDataTable(strSQL);
            drpCustomer.DataSource = dt;
            drpCustomer.DataTextField = "CustomerName";
            drpCustomer.DataValueField = "CustomerID";



2 设置下拉菜单 autopostback = true 并在 drpCustomer_SelectedIndexChanged 事件旁边编写代码



2 set dropdown autopostback=true and write the code in side the drpCustomer_SelectedIndexChanged event

strSQL = "SELECT CustomerID,CustomerName,ItemPurchased,ItemQuantity,ItemPrice FROM Customer WHERE CustomerID=" + drpCustomerID.SelectedItem.Value;
            DataTable dt = db.GetDataTable(strSQL);
            if(dt != null && dt.Rows.Count > 0)
            {
                lblCustomerID.Text = Convert.ToString(dt.Rows[0]["CustomerID"]);
                txtCustomer.Text = Convert.ToString(dt.Rows[0]["CustomerName"]);
                txtItemPurchased.Text = Convert.ToString(dt.Rows[0]["ItemPurchased"]);
                txtItemQuantity.Text = Convert.ToString(dt.Rows[0]["ItemQuantity"]);
                txtItemPrice.Text = Convert.ToString(dt.Rows[0]["ItemPrice"]);
            }



3获取数据表



3 Getting datatable

public DataTable GetDataTable(string strCommandText)
    {
        try
        {            
            SqlConnection SConnection = new SqlConnection(strDBConnection);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(strCommandText,SConnection);
            DataSet ds = new DataSet();
            sqlDataAdapter.Fill(ds);
            return ds.Tables[0];
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }



4.在web.config
中定义的数据库连接



4. Database connection defined in web.config

strDBConnection = Convert.ToString(ConfigurationManager.ConnectionStrings["ServerConnection"]);



5.在web.config
中定义连接字符串



5. define connection string in web.config

<connectionStrings>
    <add name="LocalConnection" connectionString="Password=1;Persist Security Info=True;pooling = false;User ID=sa;Initial Catalog=Northwind;Data Source=IMDADS" providerName="System.Data.SqlClient"/>
  </connectionStrings>




如有任何疑问,请让我知道.

如果对您有帮助,请提供投票.

谢谢,
Imdadhusen




Please do let me know, if you have any doubt.

Please provide Vote if this would be helpful to you.

Thanks,
Imdadhusen


这篇关于我如何使用asp.net中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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