需要用于文本框和标签绑定的下拉代码 [英] need dropdown code for textbox and label binding

查看:50
本文介绍了需要用于文本框和标签绑定的下拉代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个下拉列表,并按数据库选择记录.

我希望其他记录显示在文本框中,并标记属于下拉列表的所有记录.

I am using a dropdown and selecting record by database.

I want other records to display in the text box and label whatever record belongs to dropdown list.

推荐答案

我假设您正在谈论的是Asp.net实现. />
如果是这样,我在这里为您完成了一个示例实现.这显示了一个DropDownList 控件,其中填充了一些虚拟数据.更改DropDownList的选择时,系统会在标签中显示所选项目的数据.设置DropDownList的AutoPostback="true"并为SelectedIndexChanged let设置事件处理程序,您将在CodeBehind中获取所选数据,从而在其他控件中显示详细信息属性.

Aspx:

I assume you are talking about an Asp.net implementation.

If that is so, here I have done a sample implementation for you. This shows a DropDownList control populated with some dummy data. When you change the selection of the DropDownList, system shows the selected item''s data in a Label. Setting the DropDownList''s AutoPostback="true" and setting an Event handler for SelectedIndexChanged lets you get the selected data in the CodeBehind and thus display detail properties in other controls.

The Aspx:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server"

            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <br />
        You selected : <asp:Label ID="txtName" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>



并且,代码背后:



And, the CodeBehind:

using System;
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.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
    class Person
    {
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public Person(int Id, string Name)
        {
            this.Id = Id;
            this.Name = Name;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateData();
        }
    }
    /// <summary>
    /// Binds the DropDown list
    /// </summary>
    private void PopulateData()
    {
        //Populate some dummy data. In reality, these data should be retrieved from database
        List<Person> persons = new List<Person> { new Person(1, "Shubho"), new Person(2, "John"), new Person(3, "Tommy") };
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "Id";
        DropDownList1.DataSource = persons;
        DropDownList1.SelectedIndex = 0;
        DropDownList1.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int Id = Convert.ToInt32(DropDownList1.SelectedValue);
        //Id is the Id of the selected Person in the drop-down list
        Person selectedPerson = GetPerson(Id);
        txtName.Text = selectedPerson.Name;
    }
    /// <summary>
    /// Returns a person by Id
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    private Person GetPerson(int Id)
    {
        List<Person> persons = new List<Person> { new Person(1, "Shubho"), new Person(2, "John"), new Person(3, "Tommy") };
        //Find the person with the ID and return. In reality, this should be retrieved from the databas
        foreach (Person person in persons)
        {
            if (person.Id == Id) return person;
        }
        return null;
    }
}


请尝试以下操作:
Step1
首先,在我的情况下,我们在数据库中创建一个表:-filldata

在此表(填充数据)中,我们添加三列,即学生姓名,城市,州.在stuname的帮助下,我们找到了城市和州

Step2
添加DropDownList(以显示stuname),TextBox(状态)和Label(城市)

Step3
在Page_Load事件上,我们将数据(用户名)填充到DropDownList(DropDownList1)中,如下所示:-
Please try this:
Step1
First we create a table in database in my case I create a table:- filldata

In this table(filldata) we add three columns,stuname,city,state. With the help of stuname we find the city and state

Step2
add DropDownList (to show the stuname),TextBox(State) and Label (City)

Step3
On the Page_Load Event we fill the data(stuname) in DropDownList(DropDownList1) Like this:-
SqlConnection conn1 = new SqlConnection
(ConfigurationManager.ConnectionStrings["conn"].ToString());
 if (!IsPostBack)
        {
           
            conn1.Open();
            SqlCommand cmd1 = new SqlCommand("select * from filldata", conn1);
            SqlDataReader dr1;
            dr1 = cmd1.ExecuteReader();
            while (dr1.Read())
            {
                DropDownList1.Items.Add(dr1["stuname"].ToString());

            }
            conn1.Close();
        }


Step4
现在我们借助DropDownlist在TextBox和Label中显示数据:-


Step4
Now we show the data in TextBox and Label with the help of DropDownlist :-

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        conn1.Open();
        SqlCommand cmd1 = new SqlCommand("select * from filldata where stuname='" + DropDownList1.SelectedItem.Text + "'", conn1);
        SqlDataReader dr1;
        dr1 = cmd1.ExecuteReader();
        if (dr1.Read())
        {
            Label1.Text = dr1["city"].ToString();
            TextBox1.Text = dr1["state"].ToString();
        }
        conn1.Close();
    }


这篇关于需要用于文本框和标签绑定的下拉代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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