● ObjectDataSource使用:

來源:互聯網
上載者:User
用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"--資料添加完成後觸發的事件。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.