asp.net动态添加下拉列表框_asp.net

asp.net动态添加下拉列表框_超链接_02

.aspx代码如下:

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

<!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>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>页面转向</h3>
<p><span class ="spMemo">通过静态的超链接转向:</span>
<a href ="http://www.51zxw.net" title="我要自学网">我要自学网</a>
</p>

<p ><span class="spMemo">动态创建的HyperLink超链接:</span> </p>
<p class ="pLine"> <span >名称:</span> <asp:TextBox ID="txt_name" runat="server"></asp:TextBox> </p>
<p class ="pLine"> <span >网址:</span> <asp:TextBox ID="txt_url" runat="server" Width="300"></asp:TextBox> </p>
<p class="pLine"> <asp:Button ID="btn_addlink" runat="server" Text="添加新链接"
/>
<asp:TextBox ID="txt_links" runat="server" Height="25px" Width="329px" Visible="false" ></asp:TextBox></p>
<asp:Panel ID="pnl_links" runat="server">
</asp:Panel>



</div>
</form>
</body>
</html>


.aspx.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Sample_01 : System.Web.UI.Page
{


//自定义函数,添加HyperLink
protected void AddLink(string nameurl) {
string[] _link = nameurl.Replace("\u0081", "").Split('\u0080');
HyperLink hl= new HyperLink();
hl.Text = _link[0];
hl.NavigateUrl = _link[1];
pnl_links.Controls.Add(hl);

}



protected void Page_Load(object sender, EventArgs e)
{
//这段程序,和上一节课最后的练习,构建checkbox的思路相同

//动态创建超链接,从中转存储的TXT中读取
if (txt_links.Text != "") {
//读取字符串,拆分成数组,每个成员包括名称和网址
string[] arrS_links = txt_links.Text.Split('\u0081');
foreach (string s in arrS_links) {
if (s.Length > 0) {//为什么要大于0才构建?因为拆分的时候会多一个数组成员为空字符串
//交给子函数
AddLink(s);
}
}
}

//判断是否有新值要添加:
string _name, _url;
_name = txt_name.Text; _url = txt_url.Text;

if (_name != "" && _url != "") {
string _addlinktxt = _name + "\u0080" + _url + "\u0081";
AddLink(_addlinktxt);
txt_links.Text += _addlinktxt;

//清空文本框
txt_name.Text = txt_url.Text = "";
}



}




}


效果如下:

asp.net动态添加下拉列表框_超链接_03