2016-03-30 30 views
0

私はユーザーがデータを参照することができます小さなVB webformを書いていますItemTemplateでDataListを利用するMySQL DBで、EditItemTemplateを使用してそのデータを編集します。'System.Web.UI.WebControls.CommandEventArgs'型のオブジェクトをキャストできません 'System.Web.UI.WebControls.DataListCommandEventArgs'の型に

私はアプリの更新部分を除く作業のすべてを持っています。ユーザーが編集内容を更新すると、

'System.Web.UI.WebControls.CommandEventArgs'オブジェクトを「System.Web.UI.WebControls」と入力するとキャストできません。 DataListCommandEventArgs '。

私は、これはオンデマンド=属性セットを持つLinkBut​​tonコントロールが原因であると考えています。私は、コマンドをキャンセルし、セットアップしようとしたとき、この正確なエラーをrecived、そして私がやったことはサブDataList1_CancelCommand(オブジェクトとしてByVal送信者、System.Web.UI.WebControls.DataListCommandEventArgsとしてByVal e)の

保護された変更

ました

サブDataList1_CancelCommand(オブジェクトとしてByVal送信者、System.Web.UI.WebControls.CommandEventArgsとしてByVal e)の

保護

はしかし、私は、updateコマンドで同じことを行うことはできませんそれが設定されてDataListCommandEventArgsを必要とEditItemTemplateから編集フィールドを取得しているよう。以下は

は私のコードです。私は私の更新コマンド機能でこのエラーを回避する方法が必要です。どんな助けも素晴らしいだろう! VB BEHIND

ASPXページ

<form id="SearchForm" runat="server"> 
    <asp:SqlDataSource 
    ID='FarmStandSource' runat='server' 
    ConnectionString='<%$ ConnectionStrings:FSDATA3ConnectionString %>' 
    SelectCommand='SelectFarmStand' 
    SelectCommandType='StoredProcedure'> 
    </asp:SqlDataSource> 
<asp:DataList ID="DataList1" runat="server" ItemStyle-Width="95%" RepeatLayout="Flow" 
     OnEditCommand="DataList1_EditCommand" 
     OnCancelCommand="DataList1_CancelCommand" 
     OnUpdateCommand="DataList1_UpdateCommand" DataKeyField="PKF"> 
<ItemTemplate> 
...Fields... 
</ItemTemplate> 
<EditItemTemplate> 
...Fields... 
</EditItemTemplate> 
</DataList> 
</form> 

CODEは

Protected Sub DataList1_UpdateCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) 

    Dim PKF As Int32 = Convert.ToInt32(DataList1.DataKeys(e.Item.ItemIndex)) 
    Dim FarmBusinessName As TextBox = e.Item.FindControl("FarmBusinessNameEdit") 
    Dim FarmOwners As TextBox = e.Item.FindControl("FarmOwnersEdit") 
    Dim FullFarmAddress As TextBox = e.Item.FindControl("FullFarmAddressEdit") 
    Dim Address1 As TextBox = e.Item.FindControl("Address1Edit") 
    Dim City As TextBox = e.Item.FindControl("CityEdit") 
    Dim State As TextBox = e.Item.FindControl("StateEdit") 
    Dim Zip As TextBox = e.Item.FindControl("ZipEdit") 
    Dim County As TextBox = e.Item.FindControl("CountyEdit") 
    Dim Phone As TextBox = e.Item.FindControl("PhoneEdit") 
    Dim Website As TextBox = e.Item.FindControl("WebsiteEdit") 
    Dim Email As TextBox = e.Item.FindControl("EmailEdit") 
    Dim DatesOpen As TextBox = e.Item.FindControl("DatesOpenEdit") 
    Dim DatesClosed As TextBox = e.Item.FindControl("DatesClosedEdit") 
    Dim StandOpenTime As TextBox = e.Item.FindControl("StandOpenTimeEdit") 
    Dim StandCloseTime As TextBox = e.Item.FindControl("StandCloseTimeEdit") 
    Dim BIO As TextBox = e.Item.FindControl("BIOEdit") 
    Dim OpenEndedResponse As TextBox = e.Item.FindControl("OpenEndedResponseEdit") 
    Dim Vegetables As CheckBox = e.Item.FindControl("VegetablesEdit") 
    Dim Grains As CheckBox = e.Item.FindControl("GrainsEdit") 
    Dim BreadBakedGoods As CheckBox = e.Item.FindControl("BreadBakedGoodsEdit") 
    Dim Eggs As CheckBox = e.Item.FindControl("EggsEdit") 
    Dim MilksRawMilk As CheckBox = e.Item.FindControl("MilksRawMilkEdit") 
    Dim Cheese As CheckBox = e.Item.FindControl("CheeseEdit") 
    Dim CiderApples As CheckBox = e.Item.FindControl("CiderApplesEdit") 
    Dim Honey As CheckBox = e.Item.FindControl("HoneyEdit") 
    Dim WineHardCider As CheckBox = e.Item.FindControl("WineHardCiderEdit") 
    Dim Fruits As CheckBox = e.Item.FindControl("FruitsEdit") 
    Dim Berries As CheckBox = e.Item.FindControl("BerriesEdit") 
    Dim Maples As CheckBox = e.Item.FindControl("MaplesEdit") 
    Dim Chicken As CheckBox = e.Item.FindControl("ChickenEdit") 
    Dim Turkey As CheckBox = e.Item.FindControl("TurkeyEdit") 
    Dim Beef As CheckBox = e.Item.FindControl("BeefEdit") 
    Dim Pork As CheckBox = e.Item.FindControl("PorkEdit") 
    Dim Lamb As CheckBox = e.Item.FindControl("LambEdit") 
    Dim Goat As CheckBox = e.Item.FindControl("GoatEdit") 
    Dim WoolFiber As CheckBox = e.Item.FindControl("WoolFiberEdit") 
    Dim WoodProducts As CheckBox = e.Item.FindControl("WoodProductsEdit") 
    Dim Flowers As CheckBox = e.Item.FindControl("FlowersEdit") 
    Dim CannedBottledGoods As CheckBox = e.Item.FindControl("CannedBottledGoodsEdit") 
    Dim PlantSeedlingsStarts As CheckBox = e.Item.FindControl("PlantSeedlingsStartsEdit") 
    Dim PlantsTrees As CheckBox = e.Item.FindControl("PlantsTreesEdit") 
    Dim Other As TextBox = e.Item.FindControl("OtherEdit") 
    Dim CertifiedOrganic As TextBox = e.Item.FindControl("CertifiedOrganicEdit") 
    Dim OrganicCertifier As TextBox = e.Item.FindControl("OrganicCertifierEdit") 
    Dim OtherFarmsLocal As TextBox = e.Item.FindControl("OtherFarmsLocalEdit") 
    Dim SupplementalCatagories As TextBox = e.Item.FindControl("SupplementalCatagoriesEdit") 
    Dim BeginOperation As TextBox = e.Item.FindControl("BeginOperationEdit") 
    Dim PickYourOwn As CheckBox = e.Item.FindControl("PickYourOwnEdit") 
    Dim EBT As CheckBox = e.Item.FindControl("EBTEdit") 
    Dim Staffed As TextBox = e.Item.FindControl("StaffedEdit") 
    Dim CustomersPeakSeason As TextBox = e.Item.FindControl("CustomersPeakSeasonEdit") 
    Dim Customers2015 As TextBox = e.Item.FindControl("Customers2015Edit") 
    Dim Customers2014 As TextBox = e.Item.FindControl("Customers2014Edit") 
    Dim PercentSelfProduced As TextBox = e.Item.FindControl("PercentSelfProducedEdit") 
    Dim SellOtherFarms As CheckBox = e.Item.FindControl("SellOtherFarmsEdit") 
    Dim LiabilityInsurance As CheckBox = e.Item.FindControl("LiabilityInsuranceEdit") 

    Dim FarmBusinessNameValue = "" 
    If FarmBusinessName.Text.Trim().Length > 0 Then 
     FarmBusinessNameValue = FarmBusinessName.Text.Trim() 
    End If 
    Dim FarmOwnersValue = "" 
    If FarmOwners.Text.Trim().Length > 0 Then 
     FarmOwnersValue = FarmOwners.Text.Trim() 
    End If 
    Dim FullFarmAddressValue = "" 
    If FullFarmAddress.Text.Trim().Length > 0 Then 
     FullFarmAddressValue = FullFarmAddress.Text.Trim() 
    End If 
    Dim Address1Value = "" 
    If Address1.Text.Trim().Length > 0 Then 
     Address1Value = Address1.Text.Trim() 
    End If 
    Dim CityValue = "" 
    If City.Text.Trim().Length > 0 Then 
     CityValue = City.Text.Trim() 
    End If 
    Dim StateValue = "" 
    If State.Text.Trim().Length > 0 Then 
     StateValue = State.Text.Trim() 
    End If 
    Dim ZipValue = "" 
    If Zip.Text.Trim().Length > 0 Then 
     ZipValue = Zip.Text.Trim() 
    End If 
    Dim CountyValue = "" 
    If County.Text.Trim().Length > 0 Then 
     CountyValue = County.Text.Trim() 
    End If 
    Dim PhoneValue = "" 
    If Phone.Text.Trim().Length > 0 Then 
     PhoneValue = Phone.Text.Trim() 
    End If 
    Dim WebsiteValue = "" 
    If Website.Text.Trim().Length > 0 Then 
     WebsiteValue = Website.Text.Trim() 
    End If 
    Dim EmailValue = "" 
    If Email.Text.Trim().Length > 0 Then 
     EmailValue = Email.Text.Trim() 
    End If 
    Dim DatesOpenValue = "" 
    If DatesOpen.Text.Trim().Length > 0 Then 
     DatesOpenValue = DatesOpen.Text.Trim() 
    End If 
    Dim DatesClosedValue = "" 
    If DatesClosed.Text.Trim().Length > 0 Then 
     DatesClosedValue = DatesClosed.Text.Trim() 
    End If 
    Dim StandOpenTimeValue = "" 
    If StandOpenTime.Text.Trim().Length > 0 Then 
     StandOpenTimeValue = StandOpenTime.Text.Trim() 
    End If 
    Dim StandCloseTimeValue = "" 
    If StandCloseTime.Text.Trim().Length > 0 Then 
     StandCloseTimeValue = StandCloseTime.Text.Trim() 
    End If 
    Dim BIOValue = "" 
    If BIO.Text.Trim().Length > 0 Then 
     BIOValue = BIO.Text.Trim() 
    End If 
    Dim OpenEndedResponseValue = "" 
    If OpenEndedResponse.Text.Trim().Length > 0 Then 
     OpenEndedResponseValue = OpenEndedResponse.Text.Trim() 
    End If 
    Dim VegetablesValue = "" 
    If Vegetables.Checked <> "" Then 
     VegetablesValue = Vegetables.Checked 
    End If 
    Dim GrainsValue = "" 
    If Grains.Checked <> "" Then 
     GrainsValue = Grains.Checked 
    End If 
    Dim BreadBakedGoodsValue = "" 
    If BreadBakedGoods.Checked <> "" Then 
     BreadBakedGoodsValue = BreadBakedGoods.Checked 
    End If 
    Dim EggsValue = "" 
    If Eggs.Checked <> "" Then 
     EggsValue = Eggs.Checked 
    End If 
    Dim MilksRawMilkValue = "" 
    If MilksRawMilk.Checked <> "" Then 
     MilksRawMilkValue = MilksRawMilk.Checked 
    End If 
    Dim CheeseValue = "" 
    If Cheese.Checked <> "" Then 
     CheeseValue = Cheese.Checked 
    End If 
    Dim CiderApplesValue = "" 
    If CiderApples.Checked <> "" Then 
     CiderApplesValue = CiderApples.Checked 
    End If 
    Dim HoneyValue = "" 
    If Honey.Checked <> "" Then 
     HoneyValue = Honey.Checked 
    End If 
    Dim WineHardCiderValue = "" 
    If WineHardCider.Checked <> "" Then 
     WineHardCiderValue = WineHardCider.Checked 
    End If 
    Dim FruitsValue = "" 
    If Fruits.Checked <> "" Then 
     FruitsValue = Fruits.Checked 
    End If 
    Dim BerriesValue = "" 
    If Berries.Checked <> "" Then 
     BerriesValue = Berries.Checked 
    End If 
    Dim MaplesValue = "" 
    If Maples.Checked <> "" Then 
     MaplesValue = Maples.Checked 
    End If 
    Dim ChickenValue = "" 
    If Chicken.Checked <> "" Then 
     ChickenValue = Chicken.Checked 
    End If 
    Dim TurkeyValue = "" 
    If Turkey.Checked <> "" Then 
     TurkeyValue = Turkey.Checked 
    End If 
    Dim BeefValue = "" 
    If Beef.Checked <> "" Then 
     BeefValue = Beef.Checked 
    End If 
    Dim PorkValue = "" 
    If Pork.Checked <> "" Then 
     PorkValue = Pork.Checked 
    End If 
    Dim LambValue = "" 
    If Lamb.Checked <> "" Then 
     LambValue = Lamb.Checked 
    End If 
    Dim GoatValue = "" 
    If Goat.Checked <> "" Then 
     GoatValue = Goat.Checked 
    End If 
    Dim WoolFiberValue = "" 
    If WoolFiber.Checked <> "" Then 
     WoolFiberValue = WoolFiber.Checked 
    End If 
    Dim WoodProductsValue = "" 
    If WoodProducts.Checked <> "" Then 
     WoodProductsValue = WoodProducts.Checked 
    End If 
    Dim FlowersValue = "" 
    If Flowers.Checked <> "" Then 
     FlowersValue = Flowers.Checked 
    End If 
    Dim CannedBottledGoodsValue = "" 
    If CannedBottledGoods.Checked <> "" Then 
     CannedBottledGoodsValue = CannedBottledGoods.Checked 
    End If 
    Dim PlantSeedlingsStartsValue = "" 
    If PlantSeedlingsStarts.Checked <> "" Then 
     PlantSeedlingsStartsValue = PlantSeedlingsStarts.Checked 
    End If 
    Dim PlantsTreesValue = "" 
    If PlantsTrees.Checked <> "" Then 
     PlantsTreesValue = PlantsTrees.Checked 
    End If 
    Dim OtherValue = "" 
    If Other.Text.Trim().Length > 0 Then 
     OtherValue = Other.Text.Trim() 
    End If 
    Dim CertifiedOrganicValue = "" 
    If CertifiedOrganic.Text.Trim().Length > 0 Then 
     CertifiedOrganicValue = CertifiedOrganic.Text.Trim() 
    End If 
    Dim OrganicCertifierValue = "" 
    If OrganicCertifier.Text.Trim().Length > 0 Then 
     OrganicCertifierValue = OrganicCertifier.Text.Trim() 
    End If 
    Dim OtherFarmsLocalValue = "" 
    If OtherFarmsLocal.Text.Trim().Length > 0 Then 
     OtherFarmsLocalValue = OtherFarmsLocal.Text.Trim() 
    End If 
    Dim SupplementalCatagoriesValue = "" 
    If SupplementalCatagories.Text.Trim().Length > 0 Then 
     SupplementalCatagoriesValue = SupplementalCatagories.Text.Trim() 
    End If 
    Dim BeginOperationValue = "" 
    If BeginOperation.Text.Trim().Length > 0 Then 
     BeginOperationValue = BeginOperation.Text.Trim() 
    End If 
    Dim PickYuorOwnValue = "" 
    If PickYourOwn.Checked <> "" Then 
     PickYuorOwnValue = PickYourOwn.Checked 
    End If 
    Dim EBTValue = "" 
    If EBT.Checked <> "" Then 
     EBTValue = EBT.Checked 
    End If 
    Dim StaffedValue = "" 
    If Staffed.Text.Trim().Length > 0 Then 
     StaffedValue = Staffed.Text.Trim() 
    End If 
    Dim CustomersPeakSeasonValue = "" 
    If CustomersPeakSeason.Text.Trim().Length > 0 Then 
     CustomersPeakSeasonValue = CustomersPeakSeason.Text.Trim() 
    End If 
    Dim Customers2015Value = "" 
    If Customers2015.Text.Trim().Length > 0 Then 
     Customers2015Value = Customers2015.Text.Trim() 
    End If 
    Dim Customers2014Value = "" 
    If Customers2014.Text.Trim().Length > 0 Then 
     Customers2014Value = Customers2014.Text.Trim() 
    End If 
    Dim PercentSelfProducedValue = "" 
    If PercentSelfProduced.Text.Trim().Length > 0 Then 
     PercentSelfProducedValue = PercentSelfProduced.Text.Trim() 
    End If 
    Dim SellOtherFarmsValue = "" 
    If SellOtherFarms.Checked <> "" Then 
     SellOtherFarmsValue = SellOtherFarms.Checked 
    End If 
    Dim LiabilityInsuranceValue = "" 
    If LiabilityInsurance.Checked <> "" Then 
     LiabilityInsuranceValue = LiabilityInsurance.Checked 
    End If 

    Dim sqlConnection3 As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("FSDATA").ConnectionString) 
    Dim cmd As New SqlCommand 
    cmd.CommandText = "UpdateFarmStand" 
    cmd.CommandType = CommandType.StoredProcedure 
    cmd.Connection = sqlConnection3 

    sqlConnection3.Open() 
    cmd.ExecuteNonQuery() 
    sqlConnection3.Close() 

    DataList1.EditItemIndex = -1 

    hiddenindexfield.Text = "" 

    DataList1.DataBind() 

    SearchPanel.Visible = "True" 

End Sub 

はUPDATE:私は、UpdateCommandのはまったく発射されていないことに気づいたので、私はどこにそれを具体的に発見するcodebreaksを使用しました失敗していた。それはPage_Loadのハンドラで、私のポストバックチェッカーでIF文のネストされたに達した場合、それはドロップですが判明。これは、ユーザーがPostBackの後に編集ビューにいたかどうかを確認し、それに基づいて何かを実行します。 IF文を切り捨て、Select文をPage_Loadで処理するだけでポストバックエラーが発生します。

をPage_Loadハンドラ

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load 


    Dim sqlConnection1 As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("FSDATA").ConnectionString) 
    Dim cmd As New SqlCommand 

    cmd.CommandText = "SelectFarmStand" 
    cmd.CommandType = CommandType.StoredProcedure 
    cmd.Connection = sqlConnection1 

    Dim dt As New DataTable 
    Dim retData As New SqlDataAdapter 

    If Not IsPostBack Then 
     retData.SelectCommand = cmd 
     retData.Fill(dt) 
     DataList1.DataSource = dt 
     DataList1.DataBind() 
    Else 
     If hiddenindexfield.Text <> "" Then 
      DataList1.EditItemIndex = Convert.ToInt32(hiddenindexfield.Text) 
      DisplayPanel.Visible = True '<- FAILS HERE 
     Else 
      retData.SelectCommand = cmd 
      retData.Fill(dt) 
      DataList1.DataSource = dt 
      DataList1.DataBind() 
     End If 
    End If 
End Sub 

答えて

1

DataListCommandEventArgsはCommandEventArgs由来します。したがって、イベント引数が正しいサブタイプのものであれば、それはキャストする必要があります。私は、さまざまな種類のイベントに対して同じハンドラを使用しようとしているというのが問題だと思います。 (あなたが言及したリンクボタンのハンドラが表示されませんでした)。

あなたはそれを行うことができますが、あなたは、引数のサブタイプをテストする必要があります。これを行う1つの方法は、TryCast演算子です。例えば:あなたのハンドラが正しく配線されている場合は

Sub CommandEventHandler(sender as Object, e as CommandEventArgs) 
    Dim e1 as DataListCommandEventArgs = TryCast(e, DataListCommandEventArgs) 
    If e1 IsNot Nothing Then 
     DataListEventAHander(sender, e1) 
     Return 
    End if 
    ' Repeat for other subtypes as needed. 
End Sub 

Sub DataListEventHandler(sender as Object, e as DataListCommandEventArgs) 
    ' Your code for DataListEvents 
End Sub 

あなたはマークアップまたはコードでハンドル句 のいずれかで、直接 正しいハンドラにでき派遣する必要があります。

Sub EventSubTypeHander(sender as Object, e as EventSubTypeArgs) handles object.EventSubType 
    ' Event specific code here. 
End Sub 
+0

あなたの方法を試しましたが、まだエラーが発生しています。私はLinkBut​​ton2のためにOnClickイベントを利用しようと試みていましたが、そこから何かを試しましたが、ハンドラのいずれかが起動される前にエラーを受け取っています。私はいくつかのコードブレークを行い、それが私のPage_Loadハンドラで失敗していることがわかりました。私は、上記の私のコードに小さな説明を加えました。私はどこに失敗したのかを示すコードにコメントを入れました。 – expenguin

+0

おっと!私はTryCastのハンドラを間違って実装しました。私はエラーとバムを修正しました。魅力的なものです!回答が受け入れられました! – expenguin

関連する問題