我想做一个新闻列表的功能
如下:
首先显示新闻列表
增加新闻时弹出一个新窗口
Add.aspx 为界面部分
Add.aspx.cs里包含对新闻增加时的数据库操作程序(注:我不想把数据库操作代码写在Add.aspx文件里)
在数据库操作之后关闭 增加新闻的窗口
同时刷新新闻列表
不知道应该怎么做,主要的问题是在于
1。在数据库操作之后关闭 增加新闻的窗口
2。同时刷新新闻列表
3。数据库操作代码不能写在Add.aspx文件里,所有的操作都放在.cs文件里,.aspx文件只做操作界面
呵呵,希望大家多给点意见
大家交个朋友,来者有分, 呵呵
1,2 在操作完毕之后关闭添加新闻窗口,同时刷新新闻列表,可以用javascript实现:JavaScript:window.opener.reload();window.close();
3 用vs.net作编辑工具,它就提供页面和代码分离。
follow the example below:
1. NewsList.aspx:
<%@ Page language="c#" Codebehind="NewsList.aspx.cs" AutoEventWireup="false" Inherits="WebApplication3.NewsList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<script language="javascript">
function openAddNew()
{
var e = event.srcElement;
var ret = showModalDialog("AddNew.aspx");
//刷新新闻列表
//if (ret != null)
e.form.submit();
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server"><asp:DataGrid id=DataGrid1 runat="server"></asp:DataGrid>
<input type="button" value="Add New" onclick="openAddNew()">
</form>
</body>
</HTML>
2. NewsList.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication3
{
/// <summary>
/// Summary description for NewsList.
/// </summary>
public class NewsList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnAdd;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected DataTable GetTable()
{
if (Session["MyDataTable"] == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("Col1",typeof(string));
dt.Columns.Add("Col2",typeof(string));
for (int i=0; i < 3; i++)
{
DataRow dr = dt.NewRow();
dr["Col1"] = i.ToString();
dr["Col2"] = (i+ 1).ToString();
dt.Rows.Add(dr);
}
Session["MyDataTable"] = dt;
}
return (DataTable)Session["MyDataTable"];
}
private void Page_Load(object sender, System.EventArgs e)
{
DataTable dt = GetTable();
if (dt != null)
{
DataGrid1.DataSource = dt.DefaultView;
DataGrid1.DataBind();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
3. AddNew.aspx:
<%@ Page language="c#" Codebehind="AddNew.aspx.cs" AutoEventWireup="false" Inherits="WebApplication3.AddNew" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>AddNew</title>
<base target="_self">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table>
<tr>
<td>Column 1</td><td><asp:TextBox id=TextBox1 runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Column 2</td><td><asp:TextBox id=TextBox2 runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Button id="btnAdd" runat="server" Text="Add"></asp:Button></td>
<td><input type="button" value="close" onclick="window.close()"></td>
</tr>
</table>
</form>
</body>
</HTML>
4. AddNew.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication3
{
/// <summary>
/// Summary description for AddNew.
/// </summary>
public class AddNew : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button btnAdd;
protected System.Web.UI.WebControls.TextBox TextBox2;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnAdd.Click += new System.EventHandler(this.AddNewItem);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void AddNewItem(object sender, System.EventArgs e)
{
if (Session["MyDataTable"] !=null && (TextBox1.Text != "" || TextBox2.Text != ""))
{
//增加时的数据库操作程序 here
DataTable dt = (DataTable)Session["MyDataTable"];
DataRow dr = dt.NewRow();
dr["Col1"] = TextBox1.Text;
dr["Col2"] = TextBox2.Text;
dt.Rows.Add(dr);
}
}
}
}