与 ListView (NamingContainer) 项控件交互

发布于 2024-12-14 18:58:28 字数 5867 浏览 6 评论 0原文

如果有一种更简单的方法来遍历代码隐藏中的 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

我想要完成的是让我的组合框具有已在 DDL 中选择的单击行的值以及输入的文本文本框。我认为问题在于我无法通过 中的控件启动的命令在 部分中的控件上 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("")< /code> 等等,或者什么?!

我的意思是给我一个该死的休息微软!把你的东西放在一起!!你让世界各地的开发者的生活变得非常悲惨!不仅对于 IE,而且对于非常不一致的 .NET 框架,每个控件都有不同的成员结构,因为实现方式不同。废话!!!我决定在推出新网站后制作一套内容广泛的教程和指南,用于探索 .NET 框架以及某些控件如何转换为 html 等。恕我直言,这是 API 的一个主要缺点。作为一名新开发人员,很难了解幕后发生的事情。我的目标是让那些具有更多 html 和传统编程背景的人更清楚这一点。我学到了一件事,我对框架有着严重的又爱又恨的关系。

If only there were an easier way of traversing ASP.NET controls in the codebehind. This has been the bane of my existence as an interning .NET developer. I would like some help identifying the proper member of the ListView controls. I've deleted all the presentation code in the markup to make it easier to look, since it isn't relevant anyway. Here's the situation:

Markup

<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>

Codebehind (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

What I want to accomplish is for my Combobox to have the value of the clicked row already selected in the DDL AND the text entered into the TextBox. I think the issue lies with my inability to FindControl on a control in the <EditItemTemplate> section from a Command initiated by a control in the <ItemTemplate> section. Here's what I want it to look like. The first image is item mode, the second is edit mode.

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

It's not shown in my codebehind block above, but I'm using the following inside my "Edit" command block to try to identify the structure and how to grab my Combobox controls to act on them, but to no avail :(

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

I don't know whether to use lv.Items(0).FindControl(""), lv.Items(0).Parent.FindControl(""), lv.Parent.FindControl(""), lv.FindControl(""), etc, or WHAT?!

I mean GIMME A FREAKING BREAK MICROSOFT!!! Get your stuff together!! You're making developers' lives everywhere freaking MISERABLE!! Not only with IE, but with a very inconsistent .NET framework where every control has a different member structure as is implemented differently. FCOL!!! I've decided to make an extensive set of tutorials and guides for exploring the .NET framework and how certain controls translate to html, and so on, once I roll out my new website. This is a major shortcoming in the API imho. As a new developer, it is extremely difficult to tell what is going on behind the scenes. I aim to make that a bit more evident to those with more of an html and traditional programming background. I've learned one thing, I have a serious love/hate relationship with frameworks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱桃奶球 2024-12-21 18:58:29

如果我没理解错的话,我认为这就是 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

If I'm understanding you correctly, I think that's what Bind is for.

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

EDIT

I think this is what you're after:

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
醉生梦死 2024-12-21 18:58:29

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

到这里查看我的答案:)

I re-framed my asking of this question months later in hopes of simplifying it and increasingly the likelihood of receiving adequate assistance. I have posted the answer to both of these questions, thanks to some guidance from the referenced question.

Go here to see my answer :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文