2011-11-09 6 views
1

コードビハインド内でASP.NETコントロールを簡単にトラバースする方法があれば。これはインターンシップ.NET開発者としての私の存在の痛手でした。私は、ListViewコントロールの適切なメンバーを特定するための助けをしたいと思います。とにかく関連性がないので、見やすくするために、マークアップ内のすべてのプレゼンテーションコードを削除しました。ここでは状況があります:私のコンボボックスがクリックされた行の値がすでに選択持っているために、私が達成したい何ListView(NamingContainer)の項目コントロールとのやりとり

マークアップ

<asp:ListView ID="NewProduct" runat="server" DataSourceID="NewProductSDS" DataKeyNames="ID"> 
    <ItemTemplate> 
     <asp:Table ID="NewProductTable" runat="server"> 
      <asp:TableRow> 
       <asp:TableCell> 
        <asp:LinkButton ID="editProductName" runat="server" CommandName="Edit" /> 
       </asp:TableCell> 
       <!-- I want this value to be transferred to my edit combobox --> 
       <asp:TableCell ID="NewProductName" runat="server"> 
        <%# Eval("Product").ToString.Trim()%> 
       </asp:TableCell> 
      </asp:TableRow> 
     </asp:Table> 
    </ItemTemplate> 
    <EditItemTemplate> 
     <asp:Table ID="NewProductTable" runat="server"> 
      <asp:TableRow> 
       <asp:TableCell> 
        <asp:LinkButton ID="updateProductName" runat="server" CommandName="Rename" /> 
        <asp:LinkButton ID="cancelProductName" runat="server" CommandName="Cancel" /> 
        <!-- Autocomplete Combobox, NOTE: The DDL is not displayed --> 
        <asp:DropDownList ID="NewProductName_ddl" runat="server" DataSourceID="productLineSDS" DataTextField="Product" DataValueField="ID"></asp:DropDownList> 
        <asp:TextBox ID="NewProductName_cb" runat="server"></asp:TextBox> 
        <button id="NewProductName_btn" type="button"></button> 
       </asp:TableCell> 
      </asp:TableRow> 
     </asp:Table> 
    </EditItemTemplate> 
</asp:ListView> 

分離コード(VB)

Protected Sub ItemClick(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles NewProduct.ItemCommand 
    Dim lv As ListView = DirectCast(sender, ListView) 
    Dim i As Integer = e.Item.DisplayIndex 
    'Session State Attempt 
    Session.Add("NewProductKey", lv.DataKeys(i).Value) 
    'URL State Attempt 
    NewProductKey = lv.DataKeys(i).Value 

    If e.CommandName = "Edit" Then 
     Session.Add("NewProductKey", lv.DataKeys(i).Value) 
     Try 
      'This DDL is in the <EditItemTemplate> section. 
      ' Need to set "selected" to value from "NewProductName" table cell 
      ' For some reason I can't "FindControl" on this one. 
      Dim ddl As DropDownList = DirectCast(lv.Items(0).FindControl("NewProductName_ddl"), DropDownList) 
      Dim tb As TextBox = DirectCast(lv.Items(0).FindControl("NewProductName_cb"), TextBox) 
      tb.Text = "test" 'BROKEN, can't even set the text. How can I ensure this control exists at this time? 
      'This TableCell is in the <ItemTemplate> section. I can get this 
      ' value back just fine. 
      Dim pn As TableCell = DirectCast(lv.Items(0).FindControl("NewProductName"), TableCell) 
      ddl.SelectedValue = CInt(Session.Item("NewProductKey")) 
      ddl.Text = ddl.SelectedValue 
     Catch ex As Exception 
     End Try 
     'Wireup the Combobox using some custom Javascript. 
     Page.ClientScript.RegisterStartupScript([GetType], "", "cbsetup(""#NewProductName_cb"", ""#NewProductName_ddl"");", True) 
    ElseIf e.CommandName = "Rename" Then 
     Session.Add("NewProductKey", lv.DataKeys(i).Value) 
     'Update the Product Name with the new value as entered in the TextBox control. 
     Try 
      Dim ddl As DropDownList = DirectCast(lv.Items(0).FindControl("NewProductName_ddl"), DropDownList) 
      Dim tb As TextBox = DirectCast(lv.Items(0).FindControl("NewProductName_cb"), TextBox) 
      Dim pKey As String = NewProductKey.ToString 
      Dim pName As String = tb.Text 'Should take the value from the "NewProductName" TableCell 
      Using connection As New SqlConnection(myConnectionString) 
       'Query using pName and pKey works perfectly when run from SQL Server. 
       ' The issue I'm having is capturing the values from the controls. 
       Dim updateQuery As New SqlCommand(RenameProductQueryStr, connection) 
       connection.Open() 
       updateQuery.ExecuteNonQuery() 
       connection.Close() 
      End Using 
     Catch ex As Exception 
     End Try 
    End If 
End Sub 

ですDDLとTextBoxにテキストが入力されます。私は問題がセクションのコントロールによって開始されたコマンドからの<EditItemTemplate>セクションのコントロールのFindControlへの私の無能にあると思う。ここで私はそれが見えるようにしたいです。最初のイメージはアイテムモードで、2つ目は編集モードです。

enter image description hereは------->enter image description here

これは、上記の私の分離コードブロックに示されていないが、私は構造を特定しようとする私の「編集」コマンドブロック内で、以下のものを使用して、どのようにしています私のコンボボックスは、それらに基づいて行動するコントロールつかむが、無駄:(

For Each item As Control In lv.Items 
    debugLabel.Text += ", Items: " + item.ToString + "<br />" 
Next 

に私が使用するかどうかわからないlv.Items(0).FindControl("")lv.Items(0).Parent.FindControl("")lv.Parent.FindControl("")lv.FindControl("")など、またはWHAT ?!

私はを意味しています。GIMME A FREAKING BREAK MICROSOFT !!!一緒にあなたのものを手に入れよう!!開発者の暮らしをいたる所に夢中にさせています! IEだけでなく、すべてのコントロールが異なるメンバー構造を持ち、異なる実装をしている.NETフレームワークが非常に矛盾しています。 FCOL !!!私は新しいフレームワークを公開したら、.NETフレームワークを調べるためのチュートリアルとガイドの広範なセットと、特定のコントロールをhtmlなどに変換する方法を決定しました。これはAPI imhoの大きな欠点です。新しい開発者として、舞台裏で何が起こっているのかを知ることは非常に困難です。私は、HTMLと伝統的なプログラミングのバックグラウンドを持つ人たちに、そのことをもう少し明白にすることを目指しています。私は一つのことを学んだ、私はフレームワークと深刻な愛情/憎しみの関係を持っている。

答えて

0

:EDIT

<ItemTemplate> 
    <asp:DropDownList ID="DropDownList1" runat="server" 
     SelectedValue='<%# Bind("SomeValue") %>'> 
    </asp:DropDownList> 
</ItemTemplate> 
<EditItemTemplate> 
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SomeValue") %>' ... /> 
</EditItemTemplate> 

私はこれは後に、あなたがしているものだと思います適切な援助を受ける可能性。参照された質問からのガイダンスのおかげで、これらの質問の両方への回答を投稿しました。

Go here to see my answer

0

私が正しく理解しているとすれば、それはBindのためだと思います。私はそれを単純化し、ますますの期待して数ヶ月後に、この質問の私の提示を枠の再

For Each item As ListViewItem In lv.Items 
    Dim ddl As DropDownList = DirectCast(item.FindControl("NewProductName_ddl"), DropDownList) 
    If ddl IsNot Nothing Then 
     'your code 
    End If 
Next 
+0

画像をいくつか追加しました。私の 'ItemTemplate'にコンボボックスを表示させるのは意味がありません。 Btw、私はあなたのマークアップでインラインプログラムロジックを使用するのは一般的に良い習慣ではないことを読んだ。ビューはそんなに痛みがあるので、この場合はそうではありませんか? – Chiramisu

+0

避けられればインラインコードブロックを使用するのはお勧めできませんが、データバインドされたコントロールのコンテキストでは完全に問題ありません。 –

+0

Hmmm ...私は本当にこれが私がこのケースで探している答えだとは思わない。または少なくとも私は接続を見ることができません。とにかく、私は自分のテンプレートをそのまま保ちたい、つまり、私は 'ItemTemplate'セクションに私のコンボボックスコントロールを置いてほしくないと思っています。犯行はありません。他のアイデア?またはそれ以上の説明?あなたが好きなら、私たちは再びチャットすることができます。私に知らせて、もう一度感謝します;) – Chiramisu

関連する問題