문제

바닥 글 로우에 삽입 템플릿을 만든 그리드 뷰가 있습니다.

비즈니스 객체에 묶인 ObjectDatasource가 있습니다.

해고되지 않는 이벤트 핸들러가 있습니다.

ObjectDatasource에서 .insert를 호출하면 프로그램이 오류가 발생합니다. 내가받는 오류는 값이없고 값 사전이 비어 있지 않은지 확인해야한다는 것입니다.

사전을 매개 변수로 삽입하는 방법은 보이지 않습니다. 나는 ObjectDatasourceView를 잡고 삽입 방법을 사용하는 것에 대한 언급을 보았지만이를 수행하는 방법에 대한 언급은 없으며 MSDN은 액세스 할 수 없다고 주장합니다.

성찰은 여기로가는 길입니까? GridView에 인서트 행을 갖는 더 좋은 방법이 있습니까? 내 단계에서 분명한 것을 놓치고 있습니까?

아래는 코드입니다.
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 메소드 :

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;
    }
도움이 되었습니까?

해결책

DataObjectTyPename 세트가 있기 때문에 예제는 일관성이 없지만 LEASESDS_INSERTING 메소드는 그렇지 않은 것처럼 매개 변수를 추가합니다.

DataObjectTyPename이 필요하지 않은 경우 제거 할 수 있으며이를 수행 할 수있는 더 좋은 기회가 있어야합니다.

DataObjectTyPename이 필요하거나 선호하는 경우 (내가하는 것처럼) 이것을 작동시키는 방법이 있지만 예쁘지는 않습니다.

LEASESGRID_ROWCOMMAND 메소드에서 이와 같은 코드를 사용하여보기를 얻을 수 있습니다.

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

보기를 얻은 후에는 데이터 객체에 작성 해야하는 값이 포함 된 사전을 구축하여 결국 삽입 방법에 전달됩니다. 이에 대한 코드는 Leasesds_inserting에있는 것과 매우 유사 해 보이지만 E.inputparameters 대신 자신의 사전에 삽입 할 것입니다.

사전 준비가되면 다음과 같은 것으로보기에 삽입을 호출 할 수 있습니다.

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

그렇게해야합니다.

View의 삽입 메소드가 사전을 변환하는 작업을 수행하기 때문에 Leasesds_inserting 메소드가 필요하지 않습니다.

해당 데이터 객체에서 속성을 더 많이 처리 해야하는 경우 Leasesds_inserting에 전달 된 입력 파라 메테르 사전에 볼 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top