【总结】Select->由Select转化过来的服务器控件->DropDownList

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="public_default_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>select->DropDownList</title>
<script language="javascript" type="text/javascript">
function CheckWebControl()
{
    
var object = document.getElementById("ddlWebControl");
    alert(object.value);
    object 
= document.getElementById("ddlHtmltoWebControl");
    alert(object.value);
}

function AlertValue(value)
{
    alert(value);
}

function BtnHtmlControl_onclick() 
{
    
var obj1 = document.getElementById("ddlHtmlControl");
    alert(obj1.value);
//    var obj2 = document.getElementsByName("ddlHtmlControls");
//
    alert(obj2.value);

    
var value= form1.ddlHtmlControls.value;
    alert(value);
}
</script>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:DropDownList ID="ddlWebControl" runat="server" onchange="AlertValue(this.value);">
        
</asp:DropDownList>
        
        
<select id="ddlHtmltoWebControl" runat="server" onchange="AlertValue(this.value);">
        
</select>
       
        
<select id="ddlHtmlControl" name="ddlHtmlControls" onchange="AlertValue(this.value);">
            
<%=HtmlSelect%>
        
</select>
        
<br />
        
<asp:Button ID="btnWebControl" runat="server" Text="btnWebControl" OnClientClick="CheckWebControl();" OnClick="btnWebControl_Click" />
        
<input id="BtnHtmlToWeb" runat="server" type="button" value="BtnHtmlToWeb" onserverclick="BtnHtmlToWeb_ServerClick" onclick="CheckWebControl();" />
        
<input id="BtnHtmlControl" type="button" value="BtnHtmlControl" onclick="BtnHtmlControl_onclick();" />
        
    
</div>
    
</form>
</body>
</html>

后台cs文件

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class public_default_Default : System.Web.UI.Page
{
    
protected string HtmlSelect = "";

    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!Page.IsPostBack)
        {
            BindPage();
        }
    }


    
public void BindPage()
    {
        ListItem li 
= new ListItem("请选择""");
        ddlWebControl.Items.Insert(
0, li);
        li 
= new ListItem("江苏""JS");
        ddlWebControl.Items.Insert(
1, li);
        li 
= new ListItem("南通""NT");
        ddlWebControl.Items.Insert(
2, li);
        li 
= new ListItem("启东""QD");
        ddlWebControl.Items.Insert(
3, li);



        li 
= new ListItem(" 请选择""");
        ddlHtmltoWebControl.Items.Insert(
0, li);
        li 
= new ListItem("江苏""JS");
        ddlHtmltoWebControl.Items.Insert(
1, li);
        li 
= new ListItem("南通""NT");
        ddlHtmltoWebControl.Items.Insert(
2, li);
        li 
= new ListItem("启东""QD");
        ddlHtmltoWebControl.Items.Insert(
3, li);

        
//在后台给HTMLCONTROL控件赋值
        HtmlSelect += "<option value=\"\">请选择</option>";
        HtmlSelect 
+= "<option value=\"JS\">江苏</option>";
        HtmlSelect 
+= "<option value=\"NT\">南通</option>";
        HtmlSelect 
+= "<option value=\"QD\">启东</option>";

    }


    
protected void btnWebControl_Click(object sender, EventArgs e)
    {
        Response.Write(ddlWebControl.SelectedItem.Text);
        Response.Write(
"<br>" + ddlWebControl.SelectedItem.Value);
        Response.Write(
"<br>" + ddlWebControl.SelectedValue);
    }
    
protected void BtnHtmlToWeb_ServerClick(object sender, EventArgs e)
    {
        Response.Write(ddlHtmltoWebControl.Value);
    }
}
posted @ 2007-09-20 16:38  海底的鱼  阅读(867)  评论(0编辑  收藏  举报