首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与ListView (NamingContainer)项控件交互

与ListView (NamingContainer)项控件交互
EN

Stack Overflow用户
提问于 2011-11-09 23:24:48
回答 2查看 1.6K关注 0票数 1

如果在代码背后有一种更简单的遍历ASP.NET控件的方法就好了。作为一个实习的.NET开发人员,这一直是我生存的祸根。我想要一些帮助来识别ListView控件的适当成员。为了便于查看,我删除了标记中的所有表示代码,因为它无论如何都不相关。情况如下:

标记

代码语言:javascript
复制
<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)

代码语言:javascript
复制
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中。我认为问题在于我无法从由FindControl节中的控件发起的命令中对<EditItemTemplate>节中的控件进行<ItemTemplate>。这就是我想要的样子。第一个图像是项目模式,第二个图像是编辑模式。

->

它没有显示在上面的代码隐藏块中,但是我在我的"Edit“命令块中使用下面的命令来尝试识别结构和如何抓取Combobox控件来对它们进行操作,但是没有结果:

代码语言:javascript
复制
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框架中,每个控件都有不同的成员结构,实现方式不同。FCOL!一旦我推出了我的新网站,我决定编写一套广泛的教程和指南,用于探索.NET框架,以及某些控件如何转换为html等等。这是API imho中的一个主要缺点。作为一名新开发人员,很难知道幕后发生了什么。我的目标是让那些拥有更多html和传统编程背景的人更清楚地看到这一点。我学到了一件事,我与框架有着严重的爱/恨关系。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-25 17:56:43

几个月后,我重新提出了这个问题,希望能简化它,并越来越有可能得到足够的援助。我已经张贴了这两个问题的答案,感谢参考问题的一些指导。

:)

票数 0
EN

Stack Overflow用户

发布于 2011-11-09 23:33:54

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

代码语言:javascript
复制
<ItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server" 
        SelectedValue='<%# Bind("SomeValue") %>'>
    </asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SomeValue") %>' ... />
</EditItemTemplate>

编辑

我想这就是你想要的:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8073159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档