小弟有一个关于asp.net的DropDownList的问题。 我的DropDownList绑定好数据库的资料。在前台运行时想选中DropDownList内的任一个值旁边的textbook就显出出被选择的值。 小弟知道用按钮事件是能完成的,但是现在要求不用按钮事件,直接点DropDownList选中其中一个值旁边的textbook就显示出该值。希望各位高手能指点一二

解决方案 »

  1.   

    DropDownList 本身就有个事件,选中时触发
      

  2.   


    SelectedIndexChanged事件
    设置AutoPostBack="true" 
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" 
                onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem Value="请选择" Text="请选择"></asp:ListItem>
                <asp:ListItem Value="A" Text="A"></asp:ListItem>
                <asp:ListItem Value="B" Text="B"></asp:ListItem>
            </asp:DropDownList>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                this.TextBox1.Text = this.DropDownList1.SelectedValue;
            }
      

  3.   

    AutoPostBack此属性变成true;SelectedValue
      

  4.   

    HTML里 AutoPostBack此属性变成true;CS里 设置SelectedIndexChanged事件
       protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textbox1 = DropDownList1.SelectedItem.Text;
        }
      

  5.   

    如果只是这个功能的话  建议用JS<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetDDLText.aspx.cs" Inherits="Html_GetDDLText" %><!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>
        <script type="text/javascript">
            function GetDDLText() {
                var ddl = document.getElementById('ddlTest');
                document.getElementById('txtTest').value = ddl.options[ddl.selectedIndex].text
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <div><asp:dropdownlist ID="ddlTest" runat="server" onchange="GetDDLText()">
            <asp:ListItem>北京</asp:ListItem>
            <asp:ListItem>上海</asp:ListItem>
            <asp:ListItem>天津</asp:ListItem>
            </asp:dropdownlist>
            <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
        </div>
        </div>
        </form>
    </body>
    </html>