当前位置:有风信息港IT学院编程技术.net → 编程实例 asp.net传值简单例程

编程实例 asp.net传值简单例程

减小字体 增大字体 作者:有风IT学院  来源:有风信息港  发布时间:2008-1-13 9:07:34
<a target=编程实例 asp.net传值简单例程" onload="return imgzoom(this,550);" onclick="javascript:window.open(this.src);" style="cursor: pointer;"/>

<a target=编程实例 asp.net传值简单例程" onload="return imgzoom(this,550);" onclick="javascript:window.open(this.src);" style="cursor: pointer;"/>
下面是传值例程代码:
listnews.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class listnews : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string pid = "接收到的id值为";
pid += Request["id"];
pid+="<br />";
Response.Write(pid);
string connstr="Data Source=TIAN;Initial Catalog=news;Persist Security Info=True;User ID=sa;";
SqlConnection conn = new SqlConnection(connstr);
string cmdstr = "select * from comment where(id='"+Request["id"]+"')";
SqlCommand mycmd = new SqlCommand(cmdstr, conn);
SqlDataReader mydr = null;
conn.Open();
mydr=mycmd.ExecuteReader();
mydr.Read();
string res = "";
res += "接收到的id号是:";
res += mydr["id"];
res+="<br />";
res += "接受到的文章内容是:";
res += mydr["text"];
Response.Write(res);
}
}


listnews.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="listnews.aspx.cs" Inherits="listnews" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>


</div>
</form>
</body>
</html>

Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Src="bottom.ascx" TagName="bottom" TagPrefix="uc2" %>

<%@ Register Src="top.ascx" TagName="top" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
 <br />
<table border=1>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">

<ItemTemplate>
<tr>
<td>
<a href="listnews.aspx?id=<%# Eval("id") %>"><%# Eval("text") %></a>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:newsConnectionString3 %>"
SelectCommand="SELECT [text], [id] FROM [comment]"></asp:SqlDataSource>
   <br />
<br />
<br />

</body>
</html>

Default.aspx.cs


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}

}