zoukankan      html  css  js  c++  java
  • 十天学会ASP.Net——(2)

    2.Web控件

    1)WebControl基类属性

    参考http://msdn.microsoft.com/zh-cn/library/7zt8s89c

    2)Form控件(很简单)

    应用:实现如下效果

    clip_image002[4]

    <form id="form1" runat="server">

    <div>

    班级:<br />

    <asp:RadioButton ID="RadioButton1" runat="server" GroupName="class"

    Text="地信091" />

    <asp:RadioButton ID="RadioButton2" runat="server" GroupName="class"

    Text="地信092" />

    <br />

    性别:<br />

    <asp:RadioButtonList ID="RadioButtonList1" runat="server" Height="16px"

    Width="64px">

    <asp:ListItem Value="女生"></asp:ListItem>

    <asp:ListItem Value="男生"></asp:ListItem>

    </asp:RadioButtonList>

    <br />

    爱好:<br />

    <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"

    onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">

    <asp:ListItem Value="音乐"></asp:ListItem>

    <asp:ListItem Value="舞蹈"></asp:ListItem>

    <asp:ListItem Value="跑步"></asp:ListItem>

    </asp:CheckBoxList>

    <br />

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="确定" />

    </div>

    </form>

    说明:单选选项组用RadioButtonList中的ListItem会背认为是一组的,所以只有一个会被选中。多选组用:CheckBoxList同样可以在后台访问到Item[]属性。这样做的好处有:你不用去实现单选按钮一个选中而另外一个必须不被选择的逻辑。而直接使用RadioButton的话,他们可以被同时选择但是可以通过设置它们的CheckedChanged方法,在其中取消对方的选择来完成手动实现单选功能:

    clip_image004[4]

    这是上面的例子中我的实现:

    Response.Write("你是 " +

    (RadioButton1.Checked ? "地信091" : "") +

    (RadioButton2.Checked ? "地信092" : "") +

    " 的 " +

    (RadioButtonList1.Items[0].Selected ? "女生" : "") +

    (RadioButtonList1.Items[1].Selected ? "男生" : "") +

    "<br/>你的爱好有:" +

    (CheckBoxList1.Items[0].Selected ? "音乐" : "") +

    (CheckBoxList1.Items[0].Selected && (CheckBoxList1.Items[1].Selected || CheckBoxList1.Items[2].Selected) ? "、" : "") +

    (CheckBoxList1.Items[1].Selected ? "舞蹈" : "") +

    (CheckBoxList1.Items[1].Selected && CheckBoxList1.Items[2].Selected ? "、" : "") +

    (CheckBoxList1.Items[2].Selected ? "跑步" : "")

    );

    3)View控件(需完善)

    在页面上加入一个MultiView控件,然后再其中加入几个View。 然后再View中设置切换的按钮,代码中加入MultiView1.ActiveViewIndex = x;以切换页面。

    clip_image002[8]

    所产生的效果是View按顺序的展现。

    clip_image002[6]clip_image004[6]

    4)文件上传

    //设置允许文件上传的类型
    
    string[] allowExtensions = { ".jpg", ".gif", ".txt", ".ppt", ".ppt" };
    
    //取得网站根目录路径
    
    string path = HttpContext.Current.Request.MapPath("~/");
    
    //新建fileUpdate目录
    
    System.IO.Directory.CreateDirectory(path + "\\fileUpload");
    
    string newLocation = path + "\\fileUpload\\";
    
    string fileNameWithOutExtension = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.FileName).ToLower();
    
    //当然,这个去除文件后缀名的操作也可以通过string类的IndexOf()方法和Substring()方法手动编写。
    
    //取得上传的文件类型的扩展名,转换为小写字母
    
    string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
    
    //通过文件扩展名判断文件类型是否允许
    
    bool isPermitted = false;
    
    foreach (string aa in allowExtensions)
    
    {
    
    if (aa.Equals(fileExtension)) isPermitted = true;
    
    }
    
    //规定文件上传大小为50MB若超过则报错
    
    if (!isPermitted || FileUpload1.PostedFile.ContentLength >= 50*1024)
    
    {
    
    Response.Write("文件大小超限或者上传类型错误");
    
    return;
    
    }
    
    /*为了防止上传的文件名出线重名,第一种方法是往文件名后面加上当前的日期时间或者是利用全球唯一标识符等。*/
    
    //Guid是不重复的字符(全球唯一标识符)
    
    string guid = Guid.NewGuid().ToString();
    
    DateTime now = DateTime.Now;
    
    string nameTime = now.ToString("yyyyMMddHHmmssfff");
    
    FileUpload1.SaveAs(newLocation + fileNameWithOutExtension + nameTime + fileExtension);
    
    //或者FileUpload1.SaveAs(newLocation + fileNameWithOutExtension + guid + fileExtension);
    
    //缓存一下已上传的文件名
    
    ViewState["UpLoads"] += FileUpload1.PostedFile.FileName + "<br>";
    
    //将显示所有你上传过的文件的名字。
    
    Response.Write(ViewState["UpLoads"]);
  • 相关阅读:
    【积累总结】JS基础日常总结
    【积累总结】CSS日常总结
    【学习】JavaScript单线程与异步
    【学习指南】淘宝首页性能优化实践
    【学习】JS中的跨域
    007_stdc_C语言基础
    006_stdc_C语言基础
    005_stdc_C语言基础
    004_stdc_C语言基础
    003_stdc_linux基本常用命令_C语言基础
  • 原文地址:https://www.cnblogs.com/shenerguang/p/2511031.html
Copyright © 2011-2022 走看看