坐标地址

看清楚-----那个飞的风筝才是我

 

Data Binding in ASP.NET 2.0


http://rrrtam.spaces.live.com/blog/cns!DFB6FCE7308E794E!142.entry?_c11_blogpart_blogpart=blogview&_c=blogpart

 

ASP.NET 2.0 学习笔记(4)

  • Data Binding in ASP.NET 2.0
这个是我们在ASP.NET 1.1时候的写法
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand(“SELECT * FROM Customers”, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
 
现在ASP.NET 2.0有了SQLDATASOURCE 这个控件,双击然后配置好,看到后台代码是:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>
 
当我们在双引号后面加个DataSourceMode,那么便会出现两个选项,一个是DataSet,一个是
DataReader, DataReader是只向前读的,对于操作数据的话,我们还是用回DataSet。
另外还有一个属性是ConflictDetection,有OverwriteChanges和CompareAllValues这两个选项。
前面一个是LAST IN WINS这种模式,后面的在多个用户同时对一个DATA进行操作就会有潜在的危险。
 
  • 储存连接信息

我们以前在ASP.NET 1.1的时候,多数都是储存这些到WEB.CONFIG里面去,但有很多缺点,例如找错误困难之类。在ASP.NET 2.0多了一个ConnectionStringSettings方法,可以方便我们操作。
<%@ Page Language=”C#” %>
<script runat=”server”>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Create a new ConnectionStringSettings object and populate it
ConnectionStringSettings conn = new ConnectionStringSettings();
conn.ConnectionString =
“Server=localhost;User ID=sa;Password=password; “ +
“Database=Northwind;Persist Security Info=True”;
conn.Name = “AppConnectionString1”;
conn.ProviderName = “System.Data.SqlClient”;
// Add the new connection string to the web.config
ConfigurationManager.ConnectionStrings.Add(conn);
}
}
</script>
 
要在程序里面改变的话,可以用
System.Data.SqlClient.SqlConnectionStringBuilder builder = new
    System.Data.SqlClient.SqlConnectionStringBuilder();
// Change the connection string properties
builder.DataSource = “localhost”;
builder.InitialCatalog = “Northwind1”;
builder.UserID = “sa”;
builder.Password = “password”;
builder.PersistSecurityInfo = true;
// Save the connection string back to the web.config
ConfigurationManager.ConnectionStrings[“AppConnectionString1”].ConnectionString =
builder.ConnectionString;
 
使用GRIDVIEW里面自带的EDIT修改数据更新,可以用
<script runat=”server”>
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
if (e.Exception != null)
{
this.lblErrorMessage.Text = e.Exception.Message;
}
}
</script>
来捕捉错误。
 
删除行时捕捉错误:
<script runat=”server”>
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
if (e.Exception != null)
{
this.lblErrorMessage.Text = e.Exception.Message;
e.ExceptionHandled = true;
}
}
protected void SqlDataSource1_Deleted(object sender,
SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
this.lblErrorMessage.Text = e.Exception.Message;
e.ExceptionHandled = true;
}
}
</script>
 
使用GRIDVIEW和DETAILSVIEW混合起来使用也起到很好效果,譬如我点击GRIDVIEW里面其中一项,而DETAILSVIEW显示其详细信息。
 
FORMVIEW可以自己定义显示的格式,譬如每一页显示出来的样子好像信封那样,右上角是XX,中间是XXXX这样子。

posted on 2007-02-12 11:16  Augur  阅读(500)  评论(0编辑  收藏  举报

导航