十天学会ASP.Net——(2)
2.Web控件
1)WebControl基類屬性
參考http://msdn.microsoft.com/zh-cn/library/7zt8s89c
2)Form控件(很簡單)
應用:實現如下效果
<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" οnclick="Button1_Click" Text="確定" />
</div>
</form>
說明:單選選項組用RadioButtonList中的ListItem會背認為是一組的,所以只有一個會被選中。多選組用:CheckBoxList同樣可以在后臺訪問到Item[]屬性。這樣做的好處有:你不用去實現單選按鈕一個選中而另外一個必須不被選擇的邏輯。而直接使用RadioButton的話,他們可以被同時選擇但是可以通過設置它們的CheckedChanged方法,在其中取消對方的選擇來完成手動實現單選功能:
這是上面的例子中我的實現:
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;以切換頁面。
所產生的效果是View按順序的展現。
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"]);轉載于:https://www.cnblogs.com/shenerguang/archive/2012/05/21/2511031.html
總結
以上是生活随笔為你收集整理的十天学会ASP.Net——(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 核能力将定手机浏览器HTML5之争成败
- 下一篇: 关于偏微分