《ASP.NET开发与应用实践》例题复习用

1.labal控件----显示当前日期

  1. 工具箱–>labal控件–>ID属性改为lblMessage
  2. 切换到代码编辑器,在Page_Load事件输入以下代码:
protected void Page_Load(object sender,EventArgs e)
{
		lblMessage.Text="当前日期为:"+DateTime.Now.ToLongDateString();
}

2.ListBox控件----选择向往的城市

 本案例中,使用2个ListBox分别存放城市例表和被选中的城市名称,通过2个按钮,分别对所选城市在2个ListBox之间进行移动

ListBox控件实例

代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Button控件
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            //遍历左侧ListBox的每一项
            for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
            {
                //取得当前项ListItem
                ListItem listItem = ListBox1.Items[i];
                if (listItem.Selected)//如果被选中
                {
                    //往右侧ListBox中加如该项
                    ListBox1.Items.Remove(listItem);
                    //从左侧ListBox中移除选中的项目
                    ListBox2.Items.Add(listItem);
                }
            }
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
            //遍历左侧ListBox的每一项
            for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
            {
                //取得当前项ListItem
                ListItem listItem2 = ListBox2.Items[i];
                if (listItem2.Selected)//如果被选中
                {
                    //往右侧ListBox中加如该项
                    ListBox2.Items.Remove(listItem2);
                    //从左侧ListBox中移除选中的项目
                    ListBox1.Items.Add(listItem2);
                }
            }
        }
    }
}

源视图

添加工具箱中html的表格Table控件,改的rowspan =“2”(单元格向下打通的栏数),改单元格的属性style的“块”的text-align为center

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Button控件.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        .auto-style1 {
            height: 32px;
        }
        .auto-style2 {
            width: 100%;
            height: 281px;
        }
        .auto-style3 {
            width: 1px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table class="auto-style2">
                <tr >
                    <td rowspan="2" class="auto-style3">
                        <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="249px" SelectionMode="Multiple" Width="371px">
                            <asp:ListItem>广州</asp:ListItem>
                            <asp:ListItem>上海</asp:ListItem>
                            <asp:ListItem>北京</asp:ListItem>
                            <asp:ListItem>杭州</asp:ListItem>
                            <asp:ListItem>郑州</asp:ListItem>
                        </asp:ListBox>
                    </td>
                     <td class="auto-style1" style="text-align: center"> 
                         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="--&gt;" />
                    </td>
                   <td rowspan="2">
                       <asp:ListBox ID="ListBox2" runat="server" Height="248px" SelectionMode="Multiple" Width="329px"></asp:ListBox>
                    </td>
                </tr>
                <tr>
                    
                    <td style="text-align: center">
                        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click1" Text="&lt;--" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

3.DropDownList控件----城市选择

DropDownList控件实例

代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Button控件
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
               Label1.Text ="你选择的城市是:"+ DropDownList1.SelectedItem.Text;
        }
    }
}

FileUpload控件

1. 使用FileUpload控件上传图片文件

在这里插入图片描述

(1)页面界面设计

  1. 在窗体上添加一个FileUpload控件,添加FileUpload控件后,出现一个文本框和一个按钮。需要再添加一个上传文件的Button,ID属性为“btnUpload”,执行将文件上传到服务器缓存的功能。再添加一个Label控件
  2. 在网站项目中添加一个名为UploImage的文件夹,用来存放上传的文件

(2)编写代码
3. 为Button控件添加验证上传文件的代码,如果是图像文件,保存到文件系统中,否则提示文件类型不接受。
4. 具体代码如下

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

namespace Button控件
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            Boolean fileOK = false; //置文件验证fileOK的初始值为false
            string path = Server.MapPath("~/UploadImage/"); //设置文件上传路径
            if(FileUpload1.HasFile)  //如果已经点击浏览并添加文件到FileUpload文本框中
            {
                //获取FileUpload文本框中的文件名,并转为小写字母
                string fileExtentision = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                //设置允许上传图片文件扩展名
                string[] allowdExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                //验证添加的文件是否属于图片文件
                for(int i=0;i<allowdExtensions.Length;i++)
                {
                    if(fileExtentision==allowdExtensions[i])
                    {
                        fileOK = true;
                    }
                }
                //如果验证上传的是图片文件,开始上传到服务器的操作
                if(fileOK)
                {
                    try
                    {
                        //上传到UploadImage目录,并在Label中显示信息
                        FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
                        Label1.Text = "文件已经上传成功!<BR>" + "上传文件名为:" + FileUpload1.FileName + "<BR>文件大小为:" + FileUpload1.PostedFile.ContentLength / 1024 + "KB";
                    }
                    catch(Exception ex)
                    {
                        //如果捕获到错误,提示错误信息。
                        Label1.Text = "文件上传失败!原因是:" + ex.Message;
                    }
               
                }
                else
                {
                    //图个上传的不是图片文件,提示错误信息
                    Label1.Text = "不可接受的文件类型!请上传图片文件!";
                }
            }
        }
    }
}

2.上传大文件

(1)代码改字符数组为:

string allowedExtensions = {".mp3"}

其他不变
(2)为支持大文件的上传,需要在web.config配置文件的配置maxRequestlength和requwstLengthDiskThreshold属性的值
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

素心如月桠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值