【问题标题】:Interacting with ListView (NamingContainer) Item Controls与 ListView (NamingContainer) 项控件交互
【发布时间】:2011-11-09 23:24:48
【问题描述】:

如果有更简单的方法在代码隐藏中遍历 ASP.NET 控件就好了。这一直是我作为实习 .NET 开发人员存在的祸根。我需要一些帮助来识别ListView 控件的正确成员。我已经删除了标记中的所有演示代码以使其更易于查看,因为无论如何它都不相关。情况如下:

标记

<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

我想要完成的是让我的 Combobox 具有已在 DDL 中选择的单击行的值以及输入到 TextBox 中的文本。我认为问题在于我无法通过&lt;ItemTemplate&gt; 部分中的控件启动的命令对&lt;EditItemTemplate&gt; 部分中的控件进行FindControl。这就是我想要的样子。第一张图是项目模式,第二张图是编辑模式。

-------->

上面的代码隐藏块中没有显示它,但我在“编辑”命令块中使用以下内容来尝试识别结构以及如何抓取我的 Combobox 控件以对其进行操作,但无济于事:(

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("")等等,还是用什么?!

我的意思是给我一个可怕的突破微软!把你的东西放在一起!!你让开发人员的生活到处都是可怕的悲惨!不仅使用 IE,而且使用非常不一致的 .NET 框架,其中每个控件都有不同的成员结构,因为实现方式不同。整容!!!一旦我推出我的新网站,我决定制作一套广泛的教程和指南来探索 .NET 框架以及某些控件如何转换为 html 等等。这是 API imho 的一个主要缺点。作为一名新开发人员,很难说出幕后发生的事情。我的目标是让那些拥有更多 html 和传统编程背景的人更清楚这一点。我学到了一件事,我对框架有着严重的爱/恨关系。

【问题讨论】:

    标签: asp.net vb.net controls code-behind traversal


    【解决方案1】:

    如果我的理解正确,我认为这就是 Bind 的用途。

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

    编辑

    我想这就是你所追求的:

    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
    

    【讨论】:

    • 我添加了一些图片。在我的ItemTemplate 中显示组合框是没有意义的。顺便说一句,我读到在标记中使用内联程序逻辑通常不是一个好习惯。在这种情况下这不是真的,因为视图是如此痛苦?
    • 如果可以避免的话,不鼓励使用内联代码块,但在数据绑定控件的上下文中完全没问题。
    • 嗯...我真的不认为这是我在这种情况下要寻找的答案。或者至少我看不到连接。无论如何,我希望我的模板保持原样,即我不希望我的 Combobox 控件位于 ItemTemplate 部分。没有冒犯的意思。其他想法?或进一步解释?如果你愿意,我们可以再聊。让我知道,再次感谢 ;)
    • 我不是要你把它们放在那里。我以为这就是你在做的。因此,如果ItemTemplate 中没有任何内容是可编辑的,为什么您不能在EditItemTemplate 中获得正确的值?
    • 对不起,哈哈,也许我不是很清楚。请参考我发布的图片。请注意,第一个显示了我的 ItemTemplate 的元素,其中包含一些 LinkButtons 和另一个带有产品名称的 TableCell。第二张图片显示了我的 EditItemTemplate 的元素,其中包含几个其他按钮和我的 Combobox。单击ItemTemplate 中的“编辑”按钮后,我希望TableCell 的值用于播种EditItemTemplate 的组合框。希望这能更好地解释它。 :)
    【解决方案2】:

    几个月后,我重新提出了这个问题,希望能够简化它并增加获得足够帮助的可能性。感谢参考问题的一些指导,我已经发布了这两个问题的答案。

    Go here to see my answer :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-17
      相关资源
      最近更新 更多