用label显示新闻内容,怎么写。我刚开始学,实在是基础差。。。
看了里面的好多帖子,研究了半天,自己也改了好多次,就是还不行。
只好再麻烦大家给我讲讲了。
我在之前的页中已经写好了按news_no跳转连接到shownewsPAGE的代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="news_no" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="news_no" HeaderText="news_no" ReadOnly="True"
SortExpression="news_no" />
<asp:BoundField DataField="news_name" HeaderText="news_name"
SortExpression="news_name" />
<asp:HyperLinkField DataNavigateUrlFields="news_no"
DataNavigateUrlFormatString="USER\shownews.aspx?news_no={0}"
DataTextField="news_no" DataTextFormatString="查看{0}的详细信息" HeaderText="选择" />
</Columns>
</asp:GridView>
这个可以运行,点击查看后直接就到了shownews
可是到了要具体显示news的页面后,
我写了个后台代码如下:(我用了两个label)
public partial class USER_shownews : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string news_no = Request.QueryString["news_no"];
string strConnection = "server=pc0113kzq\\Sqlexpress;database=2012;uid=2012;pwd=000000;";
SqlConnection Connection = new SqlConnection(strConnection);
SqlCommand command = new SqlCommand(strSQL, Connection);
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.CommandText = "select * from news where news_no='"+news_no+"'";
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
this.Label1.Text = sdr["news_name"].ToString();
this.Label2.Text = sdr["news_infor"].ToString();
}
sdr.Close();
}
}
}
数据库学校没有好好学,现在很乱。。。可是又到了交作业的时候。。