如何将数据绑定到下拉列表 [英] How to bind data into dropdownlist

查看:61
本文介绍了如何将数据绑定到下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我使用一个下拉列表框(用户ID)和两个文本框(名字和姓氏).

然后选择Userid,然后使用Sql server将数据绑定到名字和姓氏.

谢谢.

Hi
I use one drop downlist box(user id) and two textbox(First Name and Last Name).

And I Select Userid And Then bind the data To First Name And Last Name Using Sql server.

Thanking You.

推荐答案

阿伦,


Hi Arun,


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
    string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    string str;
    SqlCommand com;
    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.AutoPostBack = true;
        SqlConnection con = new SqlConnection(strConnString);

        if (!IsPostBack)
        {
            DropDownList1.Items.Add("Choose userid");
            con.Open();
            str = "select * from Contacts";
            com = new SqlCommand(str, con);
            SqlDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                DropDownList1.Items.Add(reader["userid"].ToString());
            }
            reader.Close();
            con.Close();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(strConnString);
        con.Open();
        str = "select * from Contacts where userid=''" + DropDownList1.SelectedItem.Text + "''";
        com = new SqlCommand(str, con);
        SqlDataReader reader = com.ExecuteReader();
        while (reader.Read())
        {
            TextBox1.Text=reader["Fname"].ToString();
            TextBox2.Text = reader["Lname"].ToString(); 
        }
        reader.Close();
        con.Close();
    }
}


这篇关于如何将数据绑定到下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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