一样的写法,你可以将你的代码写到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
比如,上面,新建一个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
解决方案 »
- 不显示删除回复显示所有回复显示星级回复显示得分回复 求将一个32位的随机数字符串拆分成由16个2位的字符串组成的数组的方法
- 关于协同办公系统服务器安装问题:服务器的sqlserver和w3wp迅速从50mb增大到200mb
- lblname.Text怎么设置成与Identity.Name同表的另一个列值?
- asp.ne2.0t的页面可以提供编译版本吗
- 用GridView实现孟子的DataGrid跨页多选,大家来看看错在哪里了。
- !!!星星们来看看 用Oledb 读取txt 文件 大哥们 救我
- 图片加水印后在网页上打印出来
- datagrid中用下拉列表问题(用代码从数据集中读数据出错)
- Asp.net中控件gridview 如何将数据横向显示
- girdview1点击选择行,获取的数据显示到gridview2中,在gridview2可以修改并保存,在gridview1中显示修改后的数据
- 一些页面不想让用户通过直接输入地址访问,怎样限制呢?
- 关于SQL的问题,烦闷死我了!
应该象ameng_2002(wader)说的那样!
如:你的例子代码将由一个文件(.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();
}
}
}
如<asp:button onclick="myfunction">
服务器端函数写成如下形式,还要加上参数
<script language=c# runat=server>
void myfunction(Object sender, EventArgs e)
</script>ps:现在在老家,还没装vs,代码格式可能有些地方不规范,但原理就是这样吧:)