62,046
社区成员
发帖
与我相关
我的任务
分享
<a href="NewsEditUI.aspx?action=addnew">发布新闻</a>
<asp:ListView ID="lvNews" runat="server" DataKeyNames="Id"
DataSourceID="odsNews">
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
OnClientClick="return confirm('真的要删除吗?')" Text="删除" />
<a href='NewsEditUI.aspx?action=edit&id=<%# Eval("Id") %>>'>编辑</a>
</td>
<td>
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
</td>
<td>
<asp:Label ID="CreateDateLabel" runat="server"
Text='<%# Eval("CreateDate") %>' />
</td>
</tr>
</ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="标题"></asp:Label></td>
<td>
<asp:TextBox ID="txtTitle" runat="server" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtTitle" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="正文"></asp:Label></td>
<td>
<asp:TextBox ID="txtMsg" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtMsg" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="保存" onclick="btnSave_Click"
style="height: 21px" /></td>
<td></td>
</tr>
</table>
public partial class NewsEditUI : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string action = Request["action"];
if (action=="addnew")
{
}
else if (action=="edit")
{
int id = Convert.ToInt32(Request["id"]);
var newsRow = new T_NewsTableAdapter().GetDataById(id).Single();
txtTitle.Text = newsRow.Title;
txtMsg.Text = newsRow.Msg;
}
else
{
throw new Exception("action错误:"+action);
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (!IsValid)
{
return;
}
string action = Request["action"];
if (action == "addnew")
{
new T_NewsTableAdapter().Insert(txtTitle.Text,txtMsg.Text,DateTime.Now);
}
else if (action == "edit")
{
int id = Convert.ToInt32(Request["id"]);
var newsRow = new T_NewsTableAdapter().GetDataById(id).Single();
newsRow.Title = txtTitle.Text;
newsRow.Msg = txtMsg.Text;
new T_NewsTableAdapter().Update(newsRow);
}
else
{
throw new Exception("action错误:" + action);
}
}