--后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
namespace _11复习
{
public partial class Web1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataLoad();
}
}
private void DataLoad()
{
string strcon = @"Data Source=PC-20130306VXLI;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=yqq";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//cmd.CommandText = "Pro_GetNewsByTitle";
//cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.AddWithValue("@newstitle", "%" + txttitle.Text + "%");
cmd.CommandText = "Pro_GetNewsByTitle";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@newstitle", "%" + txttitle.Text + "%");
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
#region 将数据拼接成
StringBuilder sb1 = new StringBuilder();
string newstitle = string.Empty;
string newscontent = string.Empty;
string createtime = string.Empty;
sb1.Append("<table border=2>");
foreach (DataRow row in dt.Rows)
{
sb1.Append("<tr>");
newstitle = row["NewsTitle"].ToString();
sb1.Append("<td>" + newstitle + "</td>");
newscontent = row["NewsContent"].ToString();
sb1.Append("<td>" + newscontent + "</td>");
createtime = row["CreateTime"].ToString();
sb1.Append("<td>" + createtime + "</td>");
sb1.Append("</tr>");
}
sb1.Append("</table>");
divResult.InnerHtml = sb1.ToString();
#endregion
}
protected void Button1_Click(object sender, EventArgs e)
{
DataLoad();
}
}
}
--前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Web1.aspx.cs" Inherits="_11复习.Web1" %>
<!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>
<asp:TextBox ID="txttitle" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="搜索" οnclick="Button1_Click" />
<div id="divResult" runat="server">
</div>
</div>
</form>
</body>
</html>