一样的写法,你可以将你的代码写到aspx页面中,或者用codebehind,将代码写到一个cs文件中
比如,上面,新建一个asp.net的应用程序,在aspx页面中添加一个DataGrid(名为MyList),添加一个Web的按钮。
在codebehind中
using System.Data;
using System.Data.SqlClient;在aspx页中双击按钮,在按钮的click事件中,加入          
SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");
          SqlDataAdapter myCommand = new SqlDataAdapter("select * from Titles where type='" + Category.SelectedItem.Value + "'", myConnection);          DataSet ds = new DataSet();
          myCommand.Fill(ds, "Titles");          MyList.DataSource = ds.Tables["Titles"].DefaultView;
          MyList.DataBind();就差不多了你可以先看看QuickStart,msdn

解决方案 »

  1.   

    QuickStart中的例子没有采用CodeBehind,最好不要这样写!
    应该象ameng_2002(wader)说的那样!
      

  2.   

    使用。NET加快了常规代码的写入,提高了代码编写的效率(比较记事本),而实质效果两者很接近!
    如:你的例子代码将由一个文件(.aspx) 变化为保存于同一目录下的三个文件(*.aspx *.aspx.cs 和 *.aspx.resx)
    三者由.net IDE自动产生(创建一个新项目、添加一个新页面)
    自动打开 .aspx 页面的“设计”界面(可以从左下脚的标签按钮“HTML”处切换致HTML 代码界面)
    双击.aspx 页面的“设计”界面可以打开脚本界面——即:CodeBehind 文件内容(编程语言代码页)你的例子代码在记事本中是三合一的,在.NET IDE 中则为:
    HTML 代码界面:
    <%@ Page language="c#" Codebehind="*.aspx.cs" AutoEventWireup="false" Inherits="*.Articles.Articles" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>Articles</title>
    <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body >
    <form id="*" method="post" runat="server">
                         <asp:Button id ="Button1" runat="server"></asp:button>
                      </form>
             </body>
             </html>
    切换到.aspx 页面的“设计”界面可以看到最终效果。
                 选中Button 控件按钮,在属性窗口(IDE 的特点)的“事件”标签中选择所想编写的事件类型,输入新的响应函数的名字(不必有括号),回车会自动进入CodeBehind 页(脚本界面)的指定编写位置,比如:你代码中的
                      private void SubmitBtn_Click(Object sender, EventArgs e)
                      {
                            //这里
                       }
    脚本界面:
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;namespace *//项目名:可以自定义
    {
    /// <summary>
    /// *的摘要说明。
    /// </summary>
    public class *: System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button Button1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
                               ……//相关初始化代码:可以不写!
                       }
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    //this.Button1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.SubmitBtn_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion                  private void SubmitBtn_Click(Object sender, EventArgs e)
                      {
              SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");
              SqlDataAdapter myCommand = new SqlDataAdapter("select * from Titles where type='" + Category.SelectedItem.Value + "'", myConnection);          DataSet ds = new DataSet();
              myCommand.Fill(ds, "Titles");          MyList.DataSource = ds.Tables["Titles"].DefaultView;
              MyList.DataBind();
           }
       }
    }
      

  3.   

    要不使用codebehind,得自己手动关联事件
    如<asp:button onclick="myfunction">
    服务器端函数写成如下形式,还要加上参数
    <script language=c# runat=server>
    void myfunction(Object sender, EventArgs e)
    </script>ps:现在在老家,还没装vs,代码格式可能有些地方不规范,但原理就是这样吧:)
      

  4.   

    你不用CodeBehind 技术,就你这种情情况,最好用Dreamweaver Mx写比较适合你.我就是用Dreamweaver Mx的,它的界面友好,很适合我们.