从ObjectDataSource插入业务层对象(无表单)

时间:2013-04-26 13:23:02

标签: c# asp.net objectdatasource

这是我的标记:

<div class="postit">
    <ul>
        <li>
            <asp:Label ID="lblPostBaslik" runat="server" Text="Baslik :" CssClass="postit-label"></asp:Label>
            <asp:TextBox ID="txtBaslik" runat="server" CssClass="postit-txtbox" ></asp:TextBox>
        </li>
        <li>
            <asp:Label ID="lblPostEtiket" runat="server" Text="Etiket :" CssClass="postit-label"></asp:Label>
            <asp:TextBox ID="txtEtiket" runat="server" CssClass="postit-txtbox" ></asp:TextBox>
            <li class="aciklama">
                <asp:Label ID="lblAciklama" runat="server" Text="Açiklama :" CssClass="postit-label"></asp:Label>
                <asp:TextBox ID="txtAciklama" runat="server" CssClass="postit-txtbox-aciklama"  TextMode="MultiLine" ></asp:TextBox>
            </li>
            <div class="postitbuttons" id="postitbuttons">
                <asp:Button ID="btnKaydet" runat="server" Text="Kaydet"  CssClass="submit button large" onclick="btnKaydet_Click"  />
                <asp:Button ID="btnVazgec" runat="server" Text="Vazgeç" CssClass="submit button large"/>
                <%--<asp:LinkButton ID="btnKaydet" runat="server" CssClass="submit button large"  CommandName="Insert">Kaydet</asp:LinkButton>
                <asp:LinkButton ID="btnKapat" runat="server" CssClass="submit button large" CommandName="Cancel">Vazgeç</asp:LinkButton>--%>
            </div>
        </li>
    </ul>
</div>
<asp:ObjectDataSource ID="ods_postit" runat="server"  DataObjectTypeName="Post" TypeName="yonet" InsertMethod="PostEkle" >
    <InsertParameters>
        <asp:ControlParameter ControlID="txtBaslik" PropertyName="Text" Name="Post_Baslik" Type="String" />
        <asp:ControlParameter ControlID="txtEtiket" PropertyName="Text" Name="Post_Etiket" Type="String" />
        <asp:ControlParameter ControlID="txtEtiket" PropertyName="Text" Name="Post_Icerik" Type="String" />
    </InsertParameters>
</asp:ObjectDataSource> 

当我点击btnKaydet时,我收到以下错误:

  

“ObjectDataSource'odss_postit'没有要插入的值。请检查'values'字典是否包含值。”

有什么问题?它可以是这样的:

<asp:ObjectDataSource ID="ods_postit" runat="server"  DataObjectTypeName="Post" TypeName="yonet" InsertMethod="PostEkle" >

我尝试断点,但它不会触发databaseworks.cs和yonet.cs。

我不能使用这个OOP代码吗?我不想使用像(formview等)这样的表格。

这是代码隐藏和其他类:

protected void btnKaydet_Click(object sender, EventArgs e)
{
    ods_postit.Insert();
}

Post.cs

public class Post
{
    //Post Private Özellikleri
    private int _post_id;
    private Guid _post_user_id;
    private string _post_baslik;
    private string _post_etiket;
    private string _post_icerik;
    //Post Public Özellikleri
    public int Post_ID
    {
        get { return _post_id; }
        set { _post_id = value; }
    }
    public Guid Post_User_ID
    {
        get { return _post_user_id; }
        set { _post_user_id = value; }
    }
    public string Post_Baslik
    {
        get { return _post_baslik; }
        set { _post_baslik = value; }
    }
    public string Post_Etiket
    {
        get { return _post_etiket; }
        set { _post_etiket = value; }
    }
    public string Post_Icerik
    {
        get { return _post_icerik; }
        set { _post_icerik = value; }
    }
}

databaseworks.cs

public static void PostEkle(Post post)
{  
    using (SqlConnection baglanti = new SqlConnection(dbconnect()))
    {
        SqlCommand komut = new SqlCommand("sp_post_ekle", baglanti);
        komut.CommandType = CommandType.StoredProcedure;
        komut.Parameters.AddWithValue("post_id", post.Post_ID);
        komut.Parameters.AddWithValue("post_user_id",Membership.GetUser().ProviderUserKey);
        komut.Parameters.AddWithValue("post_baslik", post.Post_Baslik);
        komut.Parameters.AddWithValue("post_etiket","#" + post.Post_Etiket);
        komut.Parameters.AddWithValue("post_icerik", post.Post_Icerik);
        baglanti.Open();
        komut.ExecuteNonQuery();
        baglanti.Close();
    }
}

yonet.cs

public static void PostEkle(Post post)
{
    databaseworks.PostEkle(post);
}

1 个答案:

答案 0 :(得分:1)

FormView或DetailsView处理UI控件和数据源之间的集成,以及您没有指定的Bind语句的所有绑定(我认为即使您使用ControlParameter也很重要;要么,或者控件参数可以工作但是你需要FormView控件。

此外,ODS在你的场景中没有获得任何东西;您可以完全转储ObjectDataSource,将对象中的数据推送到业务层并将数据写入命令。