通过本文主要向大家介绍了一天精通asp.net的学习经验小结等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
1、Validator
2、IsPostBack
3、AutoPostBack。控件离开焦点的时候自动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这个属性,可选值还有edit、delete等可选值,当按钮被点击的时候将会执行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中取,因为取到的是null
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)
通过向导创建一个UserControl,然后就可以任意编辑这个UserControl,而且还可以为UserControl增加属性、事件。使用的时候只要将控件直接从SolutionExplorer拖到页面上就可以。
8、继承控件
(1)通过向导创建一个WebCustomControl。
(2)定义自己应用界面。需要重载从Control类继承来的CreateChildControls方法,并在其中生成界面控件。如果用户定义的控件会在一个页面中反复使用,最好implements System.Web.UI.INamingContainer,它会为该控件创建一个唯一的命名空间。
(3)定义自己控件的消息处理函数。自己定义的控件含有两种类型的消息,一是包含的子控件所产生的消息,二是自定义的控件消息。
9、向工程中添加“Global Application Class”就可以添加Global.asax,在这里可以监听Application、Session的生命周期。
10、(1)Response.Redirect("newpage.aspx");客户端转发
(2)Server.Transfer("newpage.aspx");服务器端转发
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本质论
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,这样表单就会提交到WebForm1.aspx了,然后在WebForm1.aspx中还可以取到前一页中所有的值:
TextBox1
2、IsPostBack
3、AutoPostBack。控件离开焦点的时候自动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这个属性,可选值还有edit、delete等可选值,当按钮被点击的时候将会执行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中取,因为取到的是null
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)
通过向导创建一个UserControl,然后就可以任意编辑这个UserControl,而且还可以为UserControl增加属性、事件。使用的时候只要将控件直接从SolutionExplorer拖到页面上就可以。
8、继承控件
(1)通过向导创建一个WebCustomControl。
(2)定义自己应用界面。需要重载从Control类继承来的CreateChildControls方法,并在其中生成界面控件。如果用户定义的控件会在一个页面中反复使用,最好implements System.Web.UI.INamingContainer,它会为该控件创建一个唯一的命名空间。
(3)定义自己控件的消息处理函数。自己定义的控件含有两种类型的消息,一是包含的子控件所产生的消息,二是自定义的控件消息。
9、向工程中添加“Global Application Class”就可以添加Global.asax,在这里可以监听Application、Session的生命周期。
10、(1)Response.Redirect("newpage.aspx");客户端转发
(2)Server.Transfer("newpage.aspx");服务器端转发
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本质论
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,这样表单就会提交到WebForm1.aspx了,然后在WebForm1.aspx中还可以取到前一页中所有的值:
TextBox1