用FormView+ObjectDataSource組合 添加和刪除資料
<asp:FormView ID="FormView1" DefaultMode="Insert" style="line-height:24px;" runat="server" DataSourceID="ObjectDataSource1">
<InsertItemTemplate>
聯 系 人:<asp:TextBox ID="contact" Text='<%# Bind("contact") %>' runat="server" /><span class="red12"> *</span><br />
<asp:Button ID="Button1" runat="server" CommandName="insert" Text="提交" />
<div>
</div>
</InsertItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="Model.HouseRent" TypeName="BLL.HouseRent" OnInserted="ObjectDataSource1_Inserted" InsertMethod="Add">
<InsertParameters>
<asp:Parameter Name="model" Type="Object" />
</InsertParameters>
</asp:ObjectDataSource>
aspx.cs:
public partial class HouseRentAdd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
int val = (int)e.ReturnValue;
if (val == -1)//重複記錄
Response.Redirect("Message.html?message=9&auto=back", true);
else if (val == 0)
Response.Redirect("Message.html?message=0&url=" + BLL.WebUtility.escape(Request.RawUrl) + "&auto=no", true);
else if(val > 0)//添加成功
Response.Redirect("Message.html?message=10&listUrl=HouseRentList.aspx&addUrl=" + BLL.WebUtility.escape(Request.RawUrl) + "&editUrl=" + BLL.WebUtility.escape("HouseRentUpdate.aspx?id=" + val.ToString()), true);
}
}
/// <summary>
///
/// </summary>
/// <param name="model"></param>
/// <returns>0=添加失敗,大於0=添加成功,-1=記錄已存在</returns>
public int Add(Model.HouseRent model)
{
//id = 0;
if (model.area_text.Trim() == "") model.area_text = model.area_value.ToString();
if (model.price_text.Trim() == "") model.price_text = model.price_value.ToString();
//判斷是否有重複記錄
if (dal.IsHaseNoContact(model)) return -1;
//判斷電話或手機號碼是否重複出現過三次
model.is_pass_tel = true;
if (dal.IsTelHasThree(model.tel, model.mobile)) model.is_pass_tel = false;
//id = dal.Add(model);
return dal.Add(model);
//return 1;
}
protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
int val = (int)e.ReturnValue;
if (val == -1)//重複記錄
Response.Redirect("Message.html?message=9&auto=back", true);
else if (val == 0)
Response.Redirect("Message.html?message=0&url=" + BLL.WebUtility.escape(Request.RawUrl) + "&auto=no", true);
else if(val > 0)//添加成功
Response.Redirect("Message.html?message=10&listUrl=HouseRentList.aspx&addUrl=" + BLL.WebUtility.escape(Request.RawUrl) + "&editUrl=" + BLL.WebUtility.escape("HouseRentUpdate.aspx?id=" + val.ToString()), true);
}
odel.HouseRent 實體類
BLL.HouseRent 邏輯類
DataObjectTypeName="Model.HouseRent"--指定資料實體
TypeName="BLL.HouseRent"--邏輯層類
InsertMethod="Add"--邏輯層的添加資料方法
OnInserted="ObjectDataSource1_Inserted"--資料添加完成後觸發的事件。