js调用DropDownList里的值和在DropDownList服务器端事件里调用js

<%@ 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>DropdownList</title>
    <script language="javascript" type="text/javascript">
    <!--
    function getDDLTextandValue()
    {
    var DDL=document.getElementById("DropDownList1");
    var index=DDL.selectedIndex;//这里selectedIndex中Index的I一定为大写
   
    var Text=DDL.options[index].text;
    var Value=DDL.options[index].value;
   // alert(Value);
  
    var txt=document.getElementById("TextBox1");
    //这里必须为txt.value,而不是txt.text,对用惯了服务器端的TextBox.Text的我来说,这点容易忽略
    txt.value=Value;
    }
    //-->
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        value:<asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
        <br />
        <br />
        选取值:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Value="a">值一</asp:ListItem>
            <asp:ListItem Value="b">值二</asp:ListItem>
            <asp:ListItem Value="c">值三</asp:ListItem>
            <asp:ListItem Value="d">值四</asp:ListItem>
            <asp:ListItem Value="e">值五</asp:ListItem>
        </asp:DropDownList>
    
    </div>
    </form>
</body>
</html>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
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;

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

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>getDDLTextandValue()</script>");
    }
}

posted @ 2008-07-12 17:20  chinaifne  阅读(5191)  评论(0编辑  收藏  举报