一天精通asp.net的學(xué)習(xí)經(jīng)驗(yàn)小結(jié)
2、IsPostBack
3、AutoPostBack??丶x開焦點(diǎn)的時(shí)候自動(dòng)Post。
4、repeater控件的使用。:Repeater控件比以前版本的asp.net好用了,只要 Eval就可以了,不用DataBinder.Eval(container.DataItem,"***"):了,只要Eval("Name")就可以,注意不能丟了前面的“#”。
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
嘎嘎嘎
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Name")%>
<%# Eval("Desc")%>
</ItemTemplate>
</asp:Repeater>
protected void Button3_Click(object sender, EventArgs e)
{
List<Person> list = new List<Person>();
list.Add(new Person(){Name="芭芭拉",Desc="白牙唄"});
list.Add(new Person(){Name="奧巴馬",Desc="黑黝黑"});
Repeater1.DataSource = list;
Repeater1.DataBind();
}
5、DataList控件:
(1)行的高亮選中
<asp:DataList ID="DataList1" runat="server" >
<SelectedItemStyle BackColor="#FF6666" />
<ItemTemplate>
<%# Eval("Name")%>
<%# Eval("Desc")%>
<asp:LinkButton ID="LinkButton1" runat="server" Text="選擇" CommandName="select" />
</ItemTemplate>
</asp:DataList>
核心是CommandName這個(gè)屬性,可選值還有edit、delete等可選值,當(dāng)按鈕被點(diǎn)擊的時(shí)候?qū)?huì)執(zhí)行EditCommand、DeleteCommand等事件。
(2)行的在位編輯:
<asp:DataList ID="DataList1" runat="server"
oneditcommand="DataList1_EditCommand">
<SelectedItemStyle BackColor="#FF6666" />
<EditItemTemplate>
<asp:TextBox runat="server" ID="t1" Text='<%# Eval("Name")%>' />
<asp:TextBox runat="server" ID="t2" Text='<%# Eval("Desc")%>' />
<asp:Button runat="server" Text="提交" CommandName="update" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("Name")%>
<%# Eval("Desc")%>
<asp:LinkButton ID="LinkButton1" runat="server" Text="編輯" CommandName="edit" />
</ItemTemplate>
</asp:DataList>
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
ReBind();
}
private void ReBind()
{
List<Person> list = new List<Person>();
list.Add(new Person() { Name = "芭芭拉", Desc = "白牙唄" });
list.Add(new Person() { Name = "奧巴馬", Desc = "黑黝黑" });
Repeater1.DataSource = list;
Repeater1.DataBind();
DataList1.DataSource = list;
DataList1.DataBind();
}
(3)行的在位編輯并且提交修改
<asp:DataList ID="DataList1" runat="server"
oneditcommand="DataList1_EditCommand"
onupdatecommand="DataList1_UpdateCommand">
<SelectedItemStyle BackColor="#FF6666" />
<EditItemTemplate>
<asp:TextBox runat="server" ID="t1" Text='<%# Eval("Name")%>' />
<asp:TextBox runat="server" ID="t2" Text='<%# Eval("Desc")%>' />
<asp:Button runat="server" Text="提交" CommandName="update" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("Name")%>
<%# Eval("Desc")%>
<asp:LinkButton ID="LinkButton1" runat="server" Text="編輯" CommandName="edit" />
</ItemTemplate>
</asp:DataList>
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["PersonList"] == null)
{
List<Person> list = new List<Person>();
list.Add(new Person() { Name = "芭芭拉", Desc = "白牙唄" });
list.Add(new Person() { Name = "奧巴馬", Desc = "黑黝黑" });
Repeater1.DataSource = list;
Repeater1.DataBind();
Session["PersonList"] = list;
}
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
ReBind();
}
private void ReBind()
{
DataList1.DataSource = Session["PersonList"];
DataList1.DataBind();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
TextBox nT1 = e.Item.FindControl("t1") as TextBox;
TextBox nT2 = e.Item.FindControl("t2") as TextBox;
//不要直接從DataList1.DataSource中取,因?yàn)槿〉降氖莕ull
List<Person> list = Session["PersonList"] as List<Person>;
Person curPerson = list[DataList1.EditItemIndex];
curPerson.Name = nT1.Text;
curPerson.Desc = nT2.Text;
DataList1.EditItemIndex = -1;
ReBind();
}
}
6 GridView控件
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand"
onsorting="GridView1_Sorting">
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="DingGou" HeaderText="訂購"
ShowHeader="True" Text="訂購" />
<asp:ButtonField ButtonType="Button" CommandName="TuiDing" HeaderText="退訂"
ShowHeader="True" Text="退訂" />
<asp:BoundField DataField="Name" HeaderText="名稱" SortExpression="Name" />
<asp:BoundField DataField="Desc" HeaderText="描述" SortExpression="Desc" />
</Columns>
</asp:GridView>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DingGou")
{
Debug.WriteLine("第"+e.CommandArgument+"行被訂購");
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
}
7、用戶控件(UserControl)
通過向?qū)?chuàng)建一個(gè)UserControl,然后就可以任意編輯這個(gè)UserControl,而且還可以為UserControl增加屬性、事件。使用的時(shí)候只要將控件直接從SolutionExplorer拖到頁面上就可以。
8、繼承控件
(1)通過向?qū)?chuàng)建一個(gè)WebCustomControl。
(2)定義自己應(yīng)用界面。需要重載從Control類繼承來的CreateChildControls方法,并在其中生成界面控件。如果用戶定義的控件會(huì)在一個(gè)頁面中反復(fù)使用,最好implements System.Web.UI.INamingContainer,它會(huì)為該控件創(chuàng)建一個(gè)唯一的命名空間。
(3)定義自己控件的消息處理函數(shù)。自己定義的控件含有兩種類型的消息,一是包含的子控件所產(chǎn)生的消息,二是自定義的控件消息。
9、向工程中添加“Global Application Class”就可以添加Global.asax,在這里可以監(jiān)聽Application、Session的生命周期。
10、(1)Response.Redirect("newpage.aspx");客戶端轉(zhuǎn)發(fā)
(2)Server.Transfer("newpage.aspx");服務(wù)器端轉(zhuǎn)發(fā)
11、web.config配置
(1) <appSettings>
<add key="FTP" value="127.0.0.1"/>
</appSettings>
this.Title = WebConfigurationManager.AppSettings["FTP"];
(2)
<connectionStrings>
<add name="mydb" connectionString="jdbc:ddd"/>
</connectionStrings>
this.Title = WebConfigurationManager.ConnectionStrings["mydb"].ConnectionString;
12、BulletedList就是<ul><ol>
13、PostBack本質(zhì)論
ASP.NET also adds two additional hidden input fields that are used to pass information
back to the server. This information consists of the ID of the control that raised the event and
any additional information that might be relevant. These fields are initially empty, as shown
here:
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
The __doPostBack() function has the responsibility for setting these values with the
appropriate information about the event and then submitting the form. A slightly simplified
version of the __doPostBack() function is shown here:
<script language="text/javascript">
function __doPostBack(eventTarget, eventArgument) {
var theform = document.Form1;
theform.__EVENTTARGET.value = eventTarget;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
</script>
14、跨頁表單提交
在頁1中指定按鈕的PostBackUrl屬性為WebForm1.aspx,這樣表單就會(huì)提交到WebForm1.aspx了,然后在WebForm1.aspx中還可以取到前一頁中所有的值:
TextBox1.Text = PreviousPage.Title;
還可以將PreviousPage cast成更詳細(xì)的頁面子類。
15、取QueryString的方法:
Request.QueryString["recordID"]
16、Server.UrlEncode(lstItems.SelectedItem.Text)
17、Multiview控件用來實(shí)現(xiàn)動(dòng)態(tài)界面,Multiview里嵌套多個(gè)view控件,每個(gè)view控件里可以方式其他控件。通過控制Multiview控件的ActiveViewIndex屬性來控制不同View的顯示。
18、Wizard控件比Multiview控件更方面,更像一個(gè)TabControl
19、動(dòng)態(tài)圖片:
在pageload的事件中:
Bitmap image = new Bitmap(300, 50);
Graphics g = Graphics.FromImage(image);
Response.ContentType = "image/png";
image.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Gif);
20 頁面導(dǎo)航
創(chuàng)建SiteMap文件,修改SiteMap文件增加節(jié)點(diǎn)。
在頁面上增加一個(gè)SiteMapDataSource,然后只要拖TreeView、Menu、SiteMapPath等控件上來,指定DataSource屬性為SiteMapDataSource就可以了。
21 單值綁定
URL = "Images/picture.jpg";
this.DataBind();
<asp:CheckBox id="chkDynamic" Text="<%# URL %>" runat="server" />
22 下拉列表框綁定
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="value"
DataValueField="key">
</asp:DropDownList>
IDictionary<string, string> dict = new Dictionary<string, string>();
dict["1"] = "aa";
dict["2"] = "bb";
DropDownList1.DataSource = dict;
DropDownList1.DataBind();
23 設(shè)定起始頁:在aspx上點(diǎn)右鍵,選擇“Set as startpage”
24 程序中數(shù)據(jù)庫連接字符串的設(shè)置
(1)、web.config中加入:
<connectionStrings>
<add name="DBConnectionString" connectionString="server=192.168.88.128\SQLEXPRESS1;uid=sa;pwd=123456;database=CRM" providerName="System.Data.SqlClient"/>
</connectionStrings>
(2)、在IDE中拖放DataSource組件以后,在屬性視圖的ConnectionString屬性中選擇DBConnectionString即可。
(3)、程序中讀取這個(gè)連接字符串的方法:
System.Configuration.Configuration rootWebConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
string connString =
rootWebConfig.ConnectionStrings.ConnectionStrings["DBConnectionString"].ConnectionString;
24 制作簡單的CRUD頁面的步驟:
(1)拖放一個(gè)SqlDataSource組件上來,設(shè)定好ConnectionString,命名組件為dsList。
(2)修改SqlDataSource組件的DeleteQuery屬性為:delete from T_PSI_User where FId=@FId
InsertQuery屬性為:INSERT INTO T_PSI_User(FId, FUserName, FPassword) VALUES (NEWID(),@FUserName,@FPassword)
SelectQuery為:select * from T_PSI_User
UpdateQuery為:Update T_PSI_User set FUserName=@FUserName,FPassword=@FPassword where FId=@FId
(3)拖放一個(gè)GridView組件上來,設(shè)定其DataSourceID屬性為dsList。修改AllowPaging、AllowSorting、AutoGenerateDeleteButton、AutoGenerateEditButton屬性為True。設(shè)定AutoGeneratedColumns屬性為false。設(shè)定DataKeyNames屬性為FId(這樣哪怕隱藏了FId字段,Edit、delete功能也能正常執(zhí)行了)
(4)修改GridView的Columns屬性,在彈出的對話框中點(diǎn)擊【RefreshSchema】鏈接,這樣在BoundField下就顯示了FId、FName、FPassword三個(gè)字段,將FUserName和FPassword字段Add進(jìn)來。
這樣不用一行代碼,有刪、改功能的頁面就做好了。下面來做“增”的功能。
(5)選擇GridView組件,在智能提示中選擇EditTemplete、然后選擇“EmptyTemplete”,拖放一個(gè)FormView組件到EmptyTemplete中,選中Formview組件,在智能提示中設(shè)定DataSource為dsList。
(6)新建一個(gè)【新增】按鈕,編輯其Click事件代碼為:
GridView1.DataSourceID = "";
GridView1.DataBind();
(7)設(shè)定FormView的ItemInserted事件代碼為:
RefreshList();
RefreshList()函數(shù)定義如下:
GridView1.DataSourceID = "dsList";
GridView1.DataBind();
這樣“增”的功能就做好了,不過還是有缺憾,那就是顯示出了不歸我們管的FId字段,并且字段名、按鈕都是英文的。
(8)選中,F(xiàn)ormView組件,然后點(diǎn)擊EditTemplete,選中InsertTemplete,這樣就可以刪除不需要的FId字段了,并且可以修改控件布局以及界面的語言文字。
(9)這樣的話Insert界面中的“Cancel取消”按鈕還是不能用,編輯FormView1的ItemCommand事件,編寫如下的代碼:
if (e.CommandName == "Cancel")
{
RefreshList();
}
25 上面實(shí)現(xiàn)CRUD的方法有兩個(gè)缺陷:
(1)需要編寫一個(gè)EmptyTemplete
(2)很難對Edit的控件做定制
因此我們還是用ListUI和EditUI分置的方法來解決。步驟:
制作ListUI:
(1)使用datasource、GridView,不過DataSource只要配置SelectQuery、DeleteQuery即可。
(2)GridView不自動(dòng)生成Edit按鈕。
(3)GridView生成一個(gè)ButtonField,標(biāo)題為“編輯”,CommandName="EditInPage"
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditInPage")
{
int index = Convert.ToInt32(e.CommandArgument);
Guid guid = (Guid)GridView1.DataKeys[index].Value;
Server.Transfer("/Sys/SysUserEdit.aspx?Action=Edit&FId="+guid);
}
}
(4)新增按鈕的Onclick事件:
Server.Transfer("/Sys/SysUserEdit.aspx?Action=Insert");
制作EditUI:
(1)拖一個(gè)DataSouce控件,按常規(guī)配置InsertCommand和UpdateCommand,配置SelectCommand為“SELECT * FROM [T_PSI_User] where 1<>1”,配置UpdateCommand為“”
(2)拖一個(gè)FormView上來,并且修改EditTemplete和InsertTemplte(可以直接將EditTemplete修改后的拷貝到InsertTemplte,注意不要忘了修改Button的CommandName)
(3)代碼;
protected void Page_Load(object sender, EventArgs e)
{
switch (Request["Action"])
{
case "Edit":
dsEdit.SelectCommand = "select * from T_PSI_User where FId=@FId";
dsEdit.SelectParameters.Clear();
dsEdit.SelectParameters.Add("FId", Request["FId"]);
FormView1.ChangeMode(FormViewMode.Edit);
break;
case "Insert":
FormView1.ChangeMode(FormViewMode.Insert);
break;
}
}
protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
GogoList();
}
protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
GogoList();
}
private void GogoList()
{
Server.Transfer("/Sys/SysUserList.aspx");
}
}
}
26、DropDownList實(shí)現(xiàn)基礎(chǔ)資料選擇器,比如在商品編輯中的選擇計(jì)量單位:
(1)拖一個(gè)針對T_MeasureUnit表的DataSource,比如名字為dsMeasureUnit。
(2)拖一個(gè)商品的Datasource,比如dsMerchan。
(3)拖一個(gè)FormView上來,并且設(shè)定其DataSource為dsMerchan
(4)將一個(gè)DropDownList放到FormView中,因?yàn)橹挥羞@樣才能設(shè)定DropDownList本身的綁定。
(5)選中DropDownList,在智能提示中選擇“ConfigDateSource”,在這里配置上dsMeasureUnit。
(6)選中DropDownList,在智能提示中選擇“EditDataBindings”,然后設(shè)定綁定到dsMerchan的FMeasureUnitId字段。
- asp.net自定義控件代碼學(xué)習(xí)筆記
- asp.net Coolite 學(xué)習(xí)交流
- ASP.NET MVC學(xué)習(xí)筆記
- asp.net Linq to Xml學(xué)習(xí)筆記
- 寫給初學(xué)asp.net的新人們 新手學(xué)習(xí)經(jīng)驗(yàn)
- ASP.NET的事件模型(很適合學(xué)習(xí)的文章)
- asp.net 學(xué)習(xí)之路 項(xiàng)目整體框架簡單的搭建
- ASP.NET的HtmlForm控件學(xué)習(xí)及Post與Get的區(qū)別概述
- Asp.net回調(diào)技術(shù)Callback學(xué)習(xí)筆記
- ASP.NET預(yù)備知識(shí)學(xué)習(xí)筆記
- ASP.NET學(xué)習(xí)路線(詳細(xì))
相關(guān)文章
在Asp.Net Core中使用ModelConvention實(shí)現(xiàn)全局過濾器隔離
這篇文章主要介紹了在Asp.Net Core中使用ModelConvention實(shí)現(xiàn)全局過濾器隔離,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Asp.net(C#)實(shí)現(xiàn)驗(yàn)證碼功能代碼
asp.net驗(yàn)證碼的實(shí)現(xiàn)方法2008-10-10ASP.NET 緩存分析和實(shí)踐淺析提高運(yùn)行效率
說到ASP.NET緩存,那就是:盡早緩存;經(jīng)常緩存您應(yīng)該在應(yīng)用程序的每一層都實(shí)現(xiàn)緩存。2010-02-02詳解高效而穩(wěn)定的企業(yè)級.NET Office 組件Spire(.NET組件介紹之二)
這篇文章主要介紹了詳解高效而穩(wěn)定的企業(yè)級.NET Office 組件Spire(.NET組件介紹之二),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。2016-12-12C# XML操作 代碼大全(讀XML,寫XML,更新,刪除節(jié)點(diǎn),與dataset結(jié)合等)
C#操作XML(讀XML,寫XML,更新,刪除節(jié)點(diǎn),與dataset結(jié)合等),以下就是操作XML的所有方法,相信可以滿足很大一部份的使用了。2009-06-06淺談ASP.NETCore統(tǒng)一處理404錯(cuò)誤都有哪些方式
本文主要介紹了ASP.NETCore統(tǒng)一處理404錯(cuò)誤都有哪些方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04淺談AjaxPro.dll,asp.net 前臺(tái)js調(diào)用后臺(tái)方法
這篇文章主要介紹了淺談AjaxPro.dll,asp.net 前臺(tái)js調(diào)用后臺(tái)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07