VS2003 安装部署1

来源:互联网 发布:手机获取电话号码软件 编辑:程序博客网 时间:2024/04/30 02:31

(一)建立简单的ASP.NET"应用程序"项目WebDBAccess

1.web.config文件里添加保存数据库连接字符串的key,部署的时候将初始化它.

<appSettings>
    
<add key="DBConnString" value="" />
</appSettings>

2. 创建index.aspx文件 窗体顶端


<%@ Page language="c#" Codebehind="index.aspx.cs" AutoEventWireup="false" Inherits="WebSetup.index" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

     <HEAD>

         <title>首页</title>

         <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

         <meta name="CODE_LANGUAGE" Content="C#">

         <meta name="vs_defaultClientScript" content="JavaScript">

         <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/

ie5">

     </HEAD>

     <body MS_POSITIONING="GridLayout">

         <form id="Form1" method="post" runat="server">

              <FONT face="宋体">

                   <br>

                   <asp:DataGrid id="DataGrid1" AllowPaging="True" PageSize="5" runat="server"

 Width="100%">

                       <PagerStyle Mode="NumericPages"></PagerStyle>

                   </asp:DataGrid>

                   <br>

                   <p align="center">

                   <P></P>

              </FONT>

         </form>

     </body>

</HTML>窗体底端

 

index.aspx.cs里绑定数据   

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Configuration;

using System.Data.SqlClient;

namespace WebSetup

{

     /// <summary>

     /// WebForm1 的摘要说明。

     /// </summary>

     public class index : System.Web.UI.Page

     {

         protected System.Web.UI.WebControls.DataGrid DataGrid1;

         private readonly SqlConnection conn=new

SqlConnection(ConfigurationSettings.AppSettings["DBConnString"].ToString());

         //private readonly SqlConnection conn=new SqlConnection("Data Source=(local);Initial

Catalog=WebDBAccess;User ID=sa;Password=sa");

         DataSet ds = new DataSet();

         SqlDataAdapter da;

         string cmdText="select * from users order by id desc";

         private void Page_Load(object sender, System.EventArgs e)

         {

              // 在此处放置用户代码以初始化页面

              InitData();

         }

         #region Web 窗体设计器生成的代码

         override protected void OnInit(EventArgs e)

         {

              //

              // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

              //

              InitializeComponent();

              base.OnInit(e);

         }

        

         /// <summary>

         /// 设计器支持所需的方法 - 不要使用代码编辑器修改

         /// 此方法的内容。

         /// </summary>

         private void InitializeComponent()

         {   

              this.DataGrid1.PageIndexChanged += new

System.Web.UI.WebControls.DataGridPageChangedEventHandler

(this.DataGrid1_PageIndexChanged);

              this.Load += new System.EventHandler(this.Page_Load);

         }

         #endregion

         private void InitData()

         {

              da = new SqlDataAdapter(cmdText, conn);

              da.Fill(ds, "users");

              DataGrid1.DataSource = ds.Tables["users"].DefaultView;

              DataGrid1.DataBind();

         }

         private void DataGrid1_PageIndexChanged(object source,

System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

         {

              this.DataGrid1.CurrentPageIndex=e.NewPageIndex;

              InitData();

         }

     }

}