ObjectDataSource控件的GridView插入失败W /空值字典 [英] ObjectDataSource Gridview Insert Fails W/ Empty Values Dictionary

查看:105
本文介绍了ObjectDataSource控件的GridView插入失败W /空值字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我所尾行中创建一个模板插入一个GridView。

我有一个绑定到业务对象一个ObjectDataSource。

我永远也不会被解雇的OnInserting事件处理程序。

有一次我打电话.Insert在ObjectDataSource控件的程序遇到一个错误。我收到的错误是,有没有价值,我应该检查,以确保这些值字典是不是空的。

我不明白的方式借助字典作为参数传递给插入。我见过抓住ObjectDataSourceView,并使用它的方法插入提及,但我不明白的是如何做到这一点任何提及和MSDN声称你没有访问。

时的反射来这里走的方式?是否有一个GridView具有插入行的更好的办法?我失去了在我这里的东西步骤明显?

下面是code:

ObjectDataSource控件:

 < ASP:ObjectDataSource控件ID =LeasesDS=服务器OnInserting =LeasesDS_Inserting
    DataObjectTypeName =CLS.BusObjects.LeaseObjDeleteMethod =删除
    InsertMethod =插入OldValues​​ParameterFormatString =original_ {0}
    SelectMethod =GetLeasesByCustomerID的TypeName =CLS.BusObjects.LeaseObj
    UpdateMethod =更新>
    < SelectParameters>
        < ASP:参数名称=的customerIDTYPE =的Int32/>
    < / SelectParameters>
    < InsertParameters>
        < ASP:参数名称=客户ID类型=的Int32/>
        < ASP:参数名称=而purchaseDate类型=日期时间/>
        < ASP:参数名称=AutoYearTYPE =的Int32/>
        < ASP:参数名称=使类型=字符串/>
        < ASP:参数名称=模型类型=字符串/>
        < ASP:参数名称=LeaseEndDate类型=日期时间/>
    < / InsertParameters>
< / ASP:ObjectDataSource控件>



codeBehind方法:

 保护无效LeasesGrid_RowCommand(对象发件人,GridViewCommandEventArgs E)
    {
        如果(e.CommandName ==插入&放大器;&安培; Page.IsValid)
        {
            LeasesDS.Insert();
        }
    }
    保护无效LeasesDS_Inserting(对象发件人,ObjectDataSourceMethodEventArgs E)
    {
        DropDownList的GridCustomersList =(DropDownList的)LeasesGrid.FooterRow.FindControl(GridCustomersList);
        文本框而purchaseDate =(文本框)LeasesGrid.FooterRow.FindControl(而purchaseDate);
        文本框AutoYear =(文本框)LeasesGrid.FooterRow.FindControl(AutoYear);
        文本框制作=(文本框)LeasesGrid.FooterRow.FindControl(做);
        文本框模型=(文本框)LeasesGrid.FooterRow.FindControl(模式);
        文本框LeaseEndDate =(文本框)LeasesGrid.FooterRow.FindControl(LeaseEndDate);
        e.InputParameters [客户ID] = Convert.ToInt32(GridCustomersList.SelectedValue);
        约会时间?而purchaseDate = NULL;
        如果(string.IsNullOrEmpty(PurchaseDate.Text)!)而purchaseDate = Convert.ToDateTime(PurchaseDate.Text);
        e.InputParameters [而purchaseDate] =而purchaseDate;
        诠释? autoYear = NULL;
        如果(string.IsNullOrEmpty(AutoYear.Text)!)autoYear = Convert.ToInt32(AutoYear.Text);
        e.InputParameters [AutoYear] = autoYear;
        字符串化妆= NULL;
        如果使= Make.Text(string.IsNullOrEmpty(Make.Text)!);
        e.InputParameters [让] =令;
        弦模型= NULL;
        如果模型= Model.Text(string.IsNullOrEmpty(Model.Text)!);
        e.InputParameters [模型] =模型;
        约会时间? leaseEndDate = NULL;
        如果(string.IsNullOrEmpty(LeaseEndDate.Text)!)leaseEndDate = Convert.ToDateTime(LeaseEndDate.Text);
        e.InputParameters [LeaseEndDate] = leaseEndDate;
    }


解决方案

您的例子是不相符的,因为你有DataObjectTypeName集,但你LeasesDS_Inserting方法是添加参数,如果它不是。

如果您不需要DataObjectTypeName,你可以删除它,那么你应该有更好的机会得到这个工作。

如果您需要DataObjectTypeName或只是preFER它(像我一样),还有一种方式来得到这个工作,但它不是pretty。

在您LeasesGrid_RowCommand方法,你可以用code这样得到的观点:

  DS的IDataSource =(的IDataSource)LeasesDS;
观点的DataSourceView = ds.GetView(LeasesGrid.DataMember);

获取视图后,您可以构建一个包含需要得到写入,最终将在得到传递给你的方法插入数据对象的值的字典。在code表示会看起来非常相似,你有什么LeasesDS_Inserting,但你会被插入到自己的字典,而不是e.InputParameters的。

一旦你有你的字典准备好了,你可以在视图上调用插入像这样的东西:

  view.Insert(字典,代表{返回false;});

这应该这样做。

由于视图的插入方法将做转换你给它变成了数据字典的工作对象,它的将在传递给你的方法插入你不会需要LeasesDS_Inserting方法了。

如果你需要做的是数据对象的属性更多的处理,你会看到它在传递给LeasesDS_Inserting的InputParameters字典。

I have a gridview to which I have created an Insert Template in the footer row.

I have an ObjectDataSource which is bound to a business object.

I have an OnInserting event handler which never gets fired.

The program encounters an error once I call .Insert on the ObjectDataSource. The error I receive is that there are no values and that I should check to make sure the values dictionary is not empty.

I do not see a way to insert with a dictionary as a parameter. I have seen mention of grabbing the ObjectDataSourceView and using it's Insert method but I do not see any mention of how to do that and MSDN claims you do not have access.

Is reflection the way to go here? Is there a better way of having an insert row on a gridview? Am I missing something obvious in my steps here?

Below is the code:
ObjectDataSource:

    <asp:ObjectDataSource ID="LeasesDS" runat="server" OnInserting="LeasesDS_Inserting" 
    DataObjectTypeName="CLS.BusObjects.LeaseObj" DeleteMethod="Delete" 
    InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" 
    SelectMethod="GetLeasesByCustomerID" TypeName="CLS.BusObjects.LeaseObj" 
    UpdateMethod="Update">
    <SelectParameters>
        <asp:Parameter Name="customerID" Type="Int32" />
    </SelectParameters>
    <InsertParameters>
        <asp:Parameter Name="CustomerID" Type="Int32" />
        <asp:Parameter Name="PurchaseDate" Type="DateTime" />
        <asp:Parameter Name="AutoYear" Type="Int32" />
        <asp:Parameter Name="Make" Type="String" />
        <asp:Parameter Name="Model" Type="String" />
        <asp:Parameter Name="LeaseEndDate" Type="DateTime" />
    </InsertParameters>
</asp:ObjectDataSource>


CodeBehind Methods:

protected void LeasesGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert" && Page.IsValid)
        {
            LeasesDS.Insert();
        }
    }
    protected void LeasesDS_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        DropDownList GridCustomersList = (DropDownList)LeasesGrid.FooterRow.FindControl("GridCustomersList");
        TextBox PurchaseDate = (TextBox)LeasesGrid.FooterRow.FindControl("PurchaseDate");
        TextBox AutoYear = (TextBox)LeasesGrid.FooterRow.FindControl("AutoYear");
        TextBox Make = (TextBox)LeasesGrid.FooterRow.FindControl("Make");
        TextBox Model = (TextBox)LeasesGrid.FooterRow.FindControl("Model");
        TextBox LeaseEndDate = (TextBox)LeasesGrid.FooterRow.FindControl("LeaseEndDate");
        e.InputParameters["CustomerID"] = Convert.ToInt32(GridCustomersList.SelectedValue);
        DateTime? purchaseDate = null;
        if (!string.IsNullOrEmpty(PurchaseDate.Text)) purchaseDate = Convert.ToDateTime(PurchaseDate.Text);
        e.InputParameters["PurchaseDate"] = purchaseDate;
        int? autoYear = null;
        if (!string.IsNullOrEmpty(AutoYear.Text)) autoYear = Convert.ToInt32(AutoYear.Text);
        e.InputParameters["AutoYear"] = autoYear;
        string make = null;
        if (!string.IsNullOrEmpty(Make.Text)) make = Make.Text;
        e.InputParameters["Make"] = make;
        string model = null;
        if (!string.IsNullOrEmpty(Model.Text)) model = Model.Text;
        e.InputParameters["Model"] = model;
        DateTime? leaseEndDate = null;
        if (!string.IsNullOrEmpty(LeaseEndDate.Text)) leaseEndDate = Convert.ToDateTime(LeaseEndDate.Text);
        e.InputParameters["LeaseEndDate"] = leaseEndDate;
    }

解决方案

Your example isn't consistent since you have DataObjectTypeName set, but your LeasesDS_Inserting method is adding parameters as if it's not.

If you don't need DataObjectTypeName, you can remove it and then you should have a better chance getting this to work.

If you need DataObjectTypeName or just prefer it (as I do), there is a way to get this to work, but it's not pretty.

In your LeasesGrid_RowCommand method, you can use code like this to get the view:

IDataSource ds = (IDataSource)LeasesDS;
DataSourceView view = ds.GetView(LeasesGrid.DataMember);

After getting the view, you can build a dictionary containing the values that need to get written to the data object that will eventually get passed in to your Insert method. The code for that will look very similar to what you have in LeasesDS_Inserting, but you'll be inserting into your own dictionary instead of e.InputParameters.

Once you have your dictionary ready, you can invoke Insert on the view with something like this:

view.Insert(dict, delegate { return false; });

That should do it.

You won't need the LeasesDS_Inserting method any more since the view's Insert method will do the work of converting the dictionary you gave it into the data object that it's going to pass in to your Insert method.

If you need to do more processing of the properties on that data object, you will see it in the InputParameters dictionary passed in to LeasesDS_Inserting.

这篇关于ObjectDataSource控件的GridView插入失败W /空值字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆