一天精通asp.net的学习经验小结

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.Text = PreviousPage.Title;
还可以将PreviousPage cast成更详细的页面子类。
15、取QueryString的方法:
Request.QueryString["recordID"]
16、Server.UrlEncode(lstItems.SelectedItem.Text)
17、Multiview控件用来实现动态界面,Multiview里嵌套多个view控件,每个view控件里可以方式其他控件。通过控制Multiview控件的ActiveViewIndex属性来控制不同View的显示。
18、Wizard控件比Multiview控件更方面,更像一个TabControl
19、动态图片:
在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 页面导航
创建SiteMap文件,修改SiteMap文件增加节点。
在页面上增加一个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 设定起始页:在aspx上点右键,选择“Set as startpage”
24 程序中数据库连接字符串的设置
(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)、程序中读取这个连接字符串的方法:
System.Configuration.Configuration rootWebConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
string connString =
rootWebConfig.ConnectionStrings.ConnectionStrings["DBConnectionString"].ConnectionString;
24 制作简单的CRUD页面的步骤:
(1)拖放一个SqlDataSource组件上来,设定好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)拖放一个GridView组件上来,设定其DataSourceID属性为dsList。修改AllowPaging、AllowSorting、AutoGenerateDeleteButton、AutoGenerateEditButton属性为True。设定AutoGeneratedColumns属性为false。设定DataKeyNames属性为FId(这样哪怕隐藏了FId字段,Edit、delete功能也能正常执行了)
(4)修改GridView的Columns属性,在弹出的对话框中点击【RefreshSchema】链接,这样在BoundField下就显示了FId、FName、FPassword三个字段,将FUserName和FPassword字段Add进来。
这样不用一行代码,有删、改功能的页面就做好了。下面来做“增”的功能。
(5)选择GridView组件,在智能提示中选择EditTemplete、然后选择“EmptyTemplete”,拖放一个FormView组件到EmptyTemplete中,选中Formview组件,在智能提示中设定DataSource为dsList。
(6)新建一个【新增】按钮,编辑其Click事件代码为:
GridView1.DataSourceID = "";
GridView1.DataBind();
(7)设定FormView的ItemInserted事件代码为:
RefreshList();
RefreshList()函数定义如下:
GridView1.DataSourceID = "dsList";
GridView1.DataBind();
这样“增”的功能就做好了,不过还是有缺憾,那就是显示出了不归我们管的FId字段,并且字段名、按钮都是英文的。
(8)选中,FormView组件,然后点击EditTemplete,选中InsertTemplete,这样就可以删除不需要的FId字段了,并且可以修改控件布局以及界面的语言文字。
(9)这样的话Insert界面中的“Cancel取消”按钮还是不能用,编辑FormView1的ItemCommand事件,编写如下的代码:
if (e.CommandName == "Cancel")
{
RefreshList();
}
25 上面实现CRUD的方法有两个缺陷:
(1)需要编写一个EmptyTemplete
(2)很难对Edit的控件做定制
因此我们还是用ListUI和EditUI分置的方法来解决。步骤:
制作ListUI:
(1)使用datasource、GridView,不过DataSource只要配置SelectQuery、DeleteQuery即可。
(2)GridView不自动生成Edit按钮。
(3)GridView生成一个ButtonField,标题为“编辑”,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)拖一个DataSouce控件,按常规配置InsertCommand和UpdateCommand,配置SelectCommand为“SELECT * FROM [T_PSI_User] where 1<>1”,配置UpdateCommand为“”
(2)拖一个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实现基础资料选择器,比如在商品编辑中的选择计量单位:
(1)拖一个针对T_MeasureUnit表的DataSource,比如名字为dsMeasureUnit。
(2)拖一个商品的Datasource,比如dsMerchan。
(3)拖一个FormView上来,并且设定其DataSource为dsMerchan
(4)将一个DropDownList放到FormView中,因为只有这样才能设定DropDownList本身的绑定。
(5)选中DropDownList,在智能提示中选择“ConfigDateSource”,在这里配置上dsMeasureUnit。
(6)选中DropDownList,在智能提示中选择“EditDataBindings”,然后设定绑定到dsMerchan的FMeasureUnitId字段。

(0)

相关推荐

  • ASP.NET的HtmlForm控件学习及Post与Get的区别概述

    一.前言 1.了解HTTP(hypertext transport protocol)超文本传输协议 它是一种分布式,协作式,超媒体系统应用之间的通信协议.是万维网(world wide web)交换信息的基础.它以HTML文档从web服务器传到web浏览器的方式进行的,如下图解: HTTP工作在TCP/IP协议体系中的TCP协议上.我们可以引出TCP/IP协议层级模型,如下图: HTTP请求的方法如下:(1).OPTIONS:返回服务器针对特定资源所支持的HTTP请求方法.也可以利用向Web服

  • asp.net Linq to Xml学习笔记

    加上之前学习过Linq to Entity,因此学习起来也比较随心应手. 以下是项目中某个底层的代码,记下做个备忘,如果能给新手学习Linq to Xml带来帮助,那就再好不过了 XML文件的格式: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <configuration> <OPsystemConfig> <MemberCenter> <DomainNam

  • asp.net 学习之路 项目整体框架简单的搭建

    最近刚学了些关于asp.net mvc方面的知识,于是了要拿个小项目来练练手,提高下自己的code能力跟思维能力.在此之前做东西都很简单,直接用动软那一套生成代码,生成一个简单的三层架构作为项目整体的框架,数据库访问层用的是ado.net.这么做了感觉挺麻烦,如果要项目要换数据库,要给数据库增加表或者给表增加某个字段,或者不使用ado.net用个orm框架来访问数据库等等,这样整体项目该动起来就提别的麻烦,为了解决这一些问题我们需要重新思考怎么搭建. 关于数据库访问层 数据库访问驱动层--大家都

  • ASP.NET的事件模型(很适合学习的文章)

    在Default.aspx的页面中第一行是一条页面指令: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AspxEventsModel._Default" %> 其中CodeBehind属性指定代码隐藏页面的名称,Inherits指定所属的命名空间和类,AutoEventWireup属性是可以

  • asp.net Coolite 学习交流

    Coolite Toolkit 简介 Coolite Toolkit 是一个支持ASP.NET AJAX的Web控件. Coolite Toolkit是基于跨浏览器的ExtJS 库开发而来的,并且简化了开发步骤,并且包含有丰富的Ajax运用. Coolite Toolkit和ExtJS 都是开源的. 官方主页:http://coolite.com下载地址:http://coolite.com/downloadSamples: http://examples.coolite.com 而且我在cnb

  • 写给初学asp.net的新人们 新手学习经验

    关于书本 新人们常常会说我看了多少多少的书,看过某某人写的书,仿佛书看了就会做了.其实不然,很多新人在面试的时候夸夸其谈,说啥啥都知道一点,到真正做的时候,啥都不会.归根到底是没有经验,技术这玩意儿经验非常重要,很多东西如果不是你碰到了,你从书上一辈子都找不到答案. 我看书的习惯一般都是先把整个书翻翻,了解一下书的基本内容,然后在实际项目中遇到相应的问题再去翻书,当然这是因为我对技术已经有一定的了解,不需要通读整本书.初学者应该找一些适合自己的书,通读一遍,其中的例子要认真做,一定要自己去写代码

  • ASP.NET预备知识学习笔记

    .NET FrameWork框架 是一套应用程序开发框架,主要目的提供一个开发模型. 主要的两个组件:     公共语言运行时(Common Language Runtime)(CLR): 提供内存管理.线程管理和远程处理等核心服务,并且还强制实施严格的安全类型,提高代码的安全性和可靠想. .NET  Framework类库: 与CLR紧密集成,可以使用它开发多种应用程序和服务.主要包括控制台应用程序.Windows窗体应用程序.WindowsPresentationFoundation(WPF

  • ASP.NET MVC学习笔记

    网上关于ASP.NET MVC的系列教程有好几个,所以就不从头开始介绍了,结尾处给大家推荐了几个链接,需要的话可以从头系统的看看. 1.ASP.NET MVC介绍及与ASP.NET WebForm的区别 刚开始为了搞清楚ASP.NET MVC到底值不值得用,翻来覆去想了一个多礼拜,看了好多资料和评论,最后决定还是值得一用.MVC不是一个简单的设计模式,更像一种架构模式,或者一种思想,刚开始一听MVC想到的就是模板引擎,NVelocity,StringTempleate等,但感觉如果只是为了用模板

  • Asp.net回调技术Callback学习笔记

    .aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xht

  • ASP.NET学习路线(详细)

    在此就向打算系统学习ASP.NET技术的初学者谈谈我的建议. 如果你已经有较多的面向对象开发经验,跳过以下这两步: 第一步 掌握一门.NET面向对象语言,C#或VB.NET. 我强烈反对在没系统学过一门面向对象(OO)语言的前提下去学ASP.NET. ASP.NET是一个全面向对象的技术,不懂OO,那绝对学不下去! 第二步 对.NET Framework类库有一定的了解 可以通过开发Windows Form应用程序来学习.NET Framework.ASP.NET是建构在.NET Framewo

  • asp.net自定义控件代码学习笔记

    效果:在放这个控件的页面: XML/HTML 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test6.aspx.cs" Inherits="test6" %> <%@Register TagPrefix="custom" Namespace="myComponents" %>

随机推荐