通過JavaScript對DropDownList的操作 / 初學

來源:互聯網
上載者:User

1.在用戶端編寫 “OnSelectedIndexChanged” 事件
由於在用戶端不識別 OnSelectedIndexChanged 事件,但是我們可以寫其 onchange 事件,然後為
DropDownList 動態添加屬性(onchange)。

<asp:DropDownList ID="SelectProvince" runat="server" AutoPostBack="True"></asp:DropDownList>this.SelectProvince.Attributes.Add("onchange", "return SelectionChanged();");

2.在用戶端動態綁定 DropDownList 的值

var option = document.createElement("option");
option.text = message[1];
option.value = message[0];
selectCity.options.add(option);

3.通過 javascript 擷取 DropDownList 的值

var dropDownList = document.getElementById("SelectProvince");
var provinceId = dropDownList.options[dropDownList.selectedIndex].value;

3.通過 Ajax 實現省市聯動。
我做了一個 Test ,不多說,看代碼: 
1.Default.aspx

Code

2.Default.aspx.cs

Code

3.Hello.aspx (XMLHttpRequest 的url)

Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Hello.aspx.cs" Inherits="Hello" %>

<!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>Hello</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

4.Hello.aspx.cs

Code
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 Hello : System.Web.UI.Page
{
    string connectionString = ConfigurationManager.ConnectionStrings["DbConn"].ConnectionString;
    string rootID = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["rootId"] != null)
        {
            rootID = Request.QueryString["rootId"].ToString();
            SendCityInfo();
        }
    }

    public void SendCityInfo()
    {
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(rootID);
                cmd.CommandText = "select CityId,CityName from City where rootId = @id";
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        string id = dr["CityId"].ToString();
                        string name = dr["CityName"].ToString();
                        Response.Write(id + " " + name);
                        Response.Write("$");
                    }
                }
            }
        }
    }
}

5.xmlHttp.js

Code

6.web.config

<connectionStrings>
   <add name="DbConn" connectionString="Server=.; database=ProvinceCity; uid=sa; pwd=qingtian"/>
</connectionStrings>

這裡要用到資料庫的串連.如果要用上面的例子,請下載相關資料庫。
資料庫檔案下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.