我有一个大学项目来建立新闻网站。在我的主页中,我有一些链接,我试图通过这个 URL 传递查询字符串
<a href="Content.aspx?Id=2&NewsId=6">
MyContent.aspx 文件
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Content.aspx.cs" Inherits="Content" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%# Eval ("NewsDetails") %>
</ItemTemplate>
</asp:Repeater>
</asp:Content>
文件隐藏代码
public partial class Content : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int _Id = Convert.ToInt32(Request.QueryString["Id"]);
int _NewsDetails = Convert.ToInt32(Request.QueryString["NewsDetails"]);
int _NewsId = Convert.ToInt32(Request.QueryString["NewsId"]);
//open your database connection
SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\MySite\\Pandheri\\App_Data\\Padheri.mdf;Integrated Security=True;User Instance=True");
conn.Open();
SqlCommand _cmd = new SqlCommand ("Select [NewsDetails],[Id] FROM Article",conn);
DataSet ds = new DataSet();
SqlDataAdapter adptr = new SqlDataAdapter(_cmd);
adptr.Fill(ds);
//close your database connection
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
当我运行该页面时,我从 NewsDetails 表中获取所有数据。
我的数据库。
{新闻表
Id,NewsCategory
文章表
Id,NewsTitle,NewsDetails,NewsId}