ASP.NET的ListBox、DropDownList、FileUpload

ListBox
.aspx

<%@ 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>ListBox控件選項的多選和單選操作</title>
    <style type="text/css">
        .style1
        {
            text-align: center;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table style="width: 272px; height: 138px">
            <tr>
                <td class="style1">
                    用戶列表</td>
                <td class="style1">
                    &nbsp;</td>
                <td class="style1">
                    授權</td>
            </tr>
            <tr>
                <td rowspan="4" style="width: 117px" align="left" valign="top">
                    <asp:ListBox ID="lbxSource" runat="server" Height="130px" SelectionMode="Multiple" Width="100px"></asp:ListBox></td>
                <td style="width: 66px">
                    <asp:Button ID="Button1" runat="server" Text="&gt;&gt;" OnClick="Button1_Click" 
                        Width="62px" /></td>
                <td rowspan="4" style="width: 71px" align="left" valign="top">
                    <asp:ListBox ID="lbxDest" runat="server" Height="130px" SelectionMode="Multiple" Width="98px"></asp:ListBox></td>
            </tr>
            <tr>
                <td style="width: 66px; height: 27px">
                    <asp:Button ID="Button2" runat="server" Text="&lt;&lt;" OnClick="Button2_Click" 
                        Width="62px" /></td>
            </tr>
            <tr>
                <td style="width: 66px">
                    <asp:Button ID="Button3" runat="server" Text="&gt;" OnClick="Button3_Click" 
                        Width="62px" /></td>
            </tr>
            <tr>
                <td style="width: 66px; height: 27px">
                    <asp:Button ID="Button4" runat="server" Text="&lt;" OnClick="Button4_Click" 
                        Width="62px" /></td>
            </tr>
        </table>
        <br />
    </form>
</body>
</html>

.aspx.cs

using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lbxSource.Items.Add("王朝");
            lbxSource.Items.Add("馬漢");
            lbxSource.Items.Add("張龍");
            lbxSource.Items.Add("趙虎");            
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        int count = lbxSource.Items.Count;
        for (int i = 0; i < count; i++)
        {
            ListItem Item = lbxSource.Items[0];
            lbxSource.Items.Remove(Item);
            lbxDest.Items.Add(Item);
        }       
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int count = lbxDest.Items.Count;
        for (int i = 0; i < count; i++)
        {
            ListItem Item = lbxDest.Items[0];
            lbxDest.Items.Remove(Item);
            lbxSource.Items.Add(Item);   
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        int count = lbxSource.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem Item = lbxSource.Items[index];
            if (lbxSource.Items[index].Selected == true)
            {
                lbxSource.Items.Remove(Item);
                lbxDest.Items.Add(Item);
            }
            else
                index++;            
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        int count = lbxDest.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem Item = lbxDest.Items[index];
            if (lbxDest.Items[index].Selected == true)
            {
                lbxDest.Items.Remove(Item);
                lbxSource.Items.Add(Item);
            }
            else
                index++;
        }
    }
}

Demo:
在這裏插入圖片描述
DropDownList
.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <style>
        html{
            height:400px;
            background:linear-gradient(15deg, #13547a 0%, #80d0c7 100%);
        }
        form{
            margin-left:30%;
            margin-top:30%;
        }
    </style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        省份:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="click1">
        </asp:DropDownList>
&nbsp; 地區:<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="click2">
        </asp:DropDownList>
    
    </div>        
        <asp:Label ID="Label1" runat="server" Text=" "></asp:Label>
    </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;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    private ArrayList list1= new ArrayList(),list2=new ArrayList();
    private SqlConnection conn;
    private SqlCommand command;
    private SqlDataReader reader;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindObject(list1, DropDownList1, "select province from region ");
            removeRepeat();
        }
    }
    protected void click1(object sender,EventArgs e)
    {
        bindObject(list2, DropDownList2, "select city from region  where province ='" + DropDownList1.SelectedValue.ToString() + "'");
       
    }
    protected void click2(object sender,EventArgs e)
    {
        Label1.Text = ""+DropDownList1.SelectedValue.ToString()+"  "+DropDownList2.SelectedValue.ToString();

    }
    private void removeRepeat()
    {
        for(int i = 0; i < list1.Count; i++)
            for (int j = i+1; j < list1.Count; j++)
            {
                string sentry = (string)list1[i];
                if (string.Equals(sentry, list1[j]) == true)
                {
                    list1.RemoveAt(j);
                    j--;
                }
            }
    }
    private void bindObject(ArrayList l,DropDownList ddList,string query)
    {
        conn = new SqlConnection(@"Server=(localdb)\MSSQLLocalDB;User Id=;Pwd=;DataBase=ZLM");
        if (conn.State == ConnectionState.Closed)
            conn.Open();
        command = new SqlCommand(query, conn);
        reader = command.ExecuteReader();
        while (reader.Read())
            for (int i = 0; i < reader.FieldCount; i++)
                l.Add(reader[i].ToString());
        removeRepeat();
        ddList.DataSource = l;
        ddList.DataBind();
        reader.Close();
        if (conn.State == ConnectionState.Open)
            conn.Close();
    }
}

region表:
在這裏插入圖片描述
Demo:
在這裏插入圖片描述
FileUpload
.aspx

<%@ 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">
    </head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload ID="FileUpload1" runat="server"/>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="文件上傳" />
        <br />
        <asp:Image ID="Image1" runat="server" ImageUrl="~/bg.gif" />
    </div>
    <asp:Label ID="Label1" runat="server" Text=" "></asp:Label>
    </form>
</body>
</html>

aspx.cs

using System;
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;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        bool fileIsValid = false;
        if (this.FileUpload1.HasFile)
        {
            String fileExtension = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();
            String[] restrictExtension = { ".gif", ".jpg", ".bmp", ".png" };
            for (int i = 0; i < restrictExtension.Length; i++)
            {
                if (fileExtension == restrictExtension[i])
                {
                    fileIsValid = true;
                }
            }
            if (fileIsValid == true)
            {
                try
                {
                    this.Image1.ImageUrl = "~/images/" + FileUpload1.FileName;
                    this.FileUpload1.SaveAs(Server.MapPath("~/images/") + FileUpload1.FileName);
                    this.Label1.Text = "文件上傳成功";
                    this.Label1.Text += "<Br/>";
                    this.Label1.Text += "<li>" + "原文件路徑:" + this.FileUpload1.PostedFile.FileName;
                    this.Label1.Text += "<Br/>";
                    this.Label1.Text += "<li>" + "文件大小:" + this.FileUpload1.PostedFile.ContentLength + "字節";
                    this.Label1.Text += "<Br/>";
                    this.Label1.Text += "<li>" + "文件類型:" + this.FileUpload1.PostedFile.ContentType;
                }
                catch(Exception ex)
                {
                    this.Label1.Text = "無法上傳文件"+ex.Message;
                }
            }
            else
            {
                this.Label1.Text = "只能夠上傳後綴爲.gif,.jpg,.bmp,.png的文件";
            }
        }
    }
}

Demo:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章