第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

DropDownList與TextBox互動(dòng)

標(biāo)簽:
JavaScript

昨晚有位网友来电求助,说有一个DropDownList,由于选项过多,想在DropDownList前放一个TextBox,让用户输入文本,DropDownList会选中相应的选项。

如果用户选择DropDownList选择一个选择,会把它的值显示于这个TextBox之上。

Insus.NET做了两个演示。也供大家参考,Demo1,普通方法实现,执行时会有点异常,还待去解决:


 

Demo1.aspx:

5acf07330001405b00110016.jpgView Code <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo1.aspx.cs" Inherits="Demo1" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        <hr />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Text="AAA" Value="aaa"></asp:ListItem>
            <asp:ListItem Text="BBB" Value="bbb"></asp:ListItem>
            <asp:ListItem Text="CCC" Value="ccc"></asp:ListItem>
        </asp:DropDownList>
    </form>
</body>
</html>

 

Demo1.aspx.cs:

5acf07330001405b00110016.jpgView Code using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Demo1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.TextBox1.Text)) return;
        this.DropDownList1.ClearSelection();
        this.DropDownList1.Items.FindByText(this.TextBox1.Text).Selected = true;
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.DropDownList1.SelectedIndex == -1) return;
        this.TextBox1.Text = this.DropDownList1.SelectedItem.Text;
    }
}

 

 Demo2,Insus.NET使用了适配器(Adapter)来将DropDownList重写TextBox的Text属性。


 

适配器DropDownListAdapter的类:

5acf07330001405b00110016.jpgDropDownListAdapter using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for DropDownListAdapter
/// </summary>
namespace Insus.NET
{
    public class DropDownListAdapter : TextBox
    {
        DropDownList _DropDownList;

        public DropDownListAdapter(DropDownList dropDownList)
        {
            this._DropDownList = dropDownList;
        }

        public override string Text
        {
            get
            {
                return _DropDownList.SelectedItem.Text;
            }
            set
            {
                this._DropDownList.ClearSelection();
                foreach (ListItem li in this._DropDownList.Items)
                {
                    if (li.Text == value)
                        this._DropDownList.Items.FindByText(value).Selected = true;
                    continue;
                }
            }
        }
    }
}

 

Demo2.aspx:

5acf07330001405b00110016.jpgView Code <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo2.aspx.cs" Inherits="Demo2" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>        
        <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        <hr />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Text="AAA" Value="aaa"></asp:ListItem>
            <asp:ListItem Text="BBB" Value="bbb"></asp:ListItem>
            <asp:ListItem Text="CCC" Value="ccc"></asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

 

Demo2.aspx.cs:

5acf07330001405b00110016.jpgView Code using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

public partial class Demo2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        DropDownListAdapter obj = new DropDownListAdapter(this.DropDownList1);
        obj.Text = this.TextBox1.Text;
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownListAdapter obj = new DropDownListAdapter(this.DropDownList1);
        this.TextBox1.Text = obj.Text;
    }
}

 

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消