Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7184003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:06:41+00:00 2026-05-28T18:06:41+00:00

If only there were an easier way of traversing ASP.NET controls in the codebehind.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:06:42+00:00Added an answer on May 28, 2026 at 6:06 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easier way to grab only one element out of a match
This problem has been getting at me for a while now. Is there an
Is there any easier way to set to process only those input fields within
Is there easier way to build querys in doctrine then this. At this point
Is there an easier way than foreach($_POST as $x=>$y){ $arr[$x] = $this->input->get_post($y, TRUE); }
I know this is not the only question out there, but I still couldn't
What's the easiest way to perform an FQL query from an ASP.NET application? Do
I just wanted to ask is there an easier way to get records from
I've finished my macro! But is there an easier way in runnning it? I
Is there a way to subclass the ArrayList class to only allow objects of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.