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 7182905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:52:12+00:00 2026-05-28T17:52:12+00:00

In my ListView I have an ItemTemplate and EditItemTemplate which look something like this,

  • 0

In my ListView I have an ItemTemplate and EditItemTemplate which look something like this, respectively.

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

When I click the “Edit” button, and it switches to the EditItemTemplate view on the right, I want to prefill the Textbox and select the corresponding option in the DropDownList. How can I do this?

Before you say to use something like the following, please know that I’ve already explored every possible variation I can think of. Sorry to be so demanding, but please be prepared to walk me through this one if you answer. ^.^ I’ve been stuck on this issue for literally months 🙁

Dim lv As ListView = DirectCast(sender, ListView) 'sender is the ListView on the ItemCommand event
Dim ddl As DropDownList = DirectCast(lv.Items(0).FindControl("NewProductName_ddl"), DropDownList)
Dim tb As TextBox = DirectCast(lv.Items(0).FindControl("NewProductName_tb"), TextBox)

UPDATE – RAWR!!

Oh my freaking goodness, SO CLOSE, but no cigar. The following code worked for prefilling when only one item was in the ListView, but when more than one items exist, it throws a NullReferenceException 🙁

'PROBLEM WAS HERE: Compare to the working code in my answer.
Protected Sub NewProduct_ItemDataBound(ByVal sender As ListView, ByVal e As ListViewItemEventArgs) Handles NewProduct.ItemDataBound
    If sender.EditIndex > -1 Then
        Dim ddl As DropDownList = DirectCast(e.Item.FindControl("NewProductName_ddl"), DropDownList)
        Dim tb As TextBox = DirectCast(e.Item.FindControl("NewProductName_cb"), TextBox)

        ddl.Items.FindByValue(sender.DataKeys(sender.EditIndex)("ID").ToString).Selected = True 'Prefills the DropDownList
        tb.Text = sender.DataKeys(sender.EditIndex)("Product").ToString 'Prefills the TextBox
    End If
End Sub
  • 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-28T17:52:13+00:00Added an answer on May 28, 2026 at 5:52 pm

    EUREKA!!

    I am elated beyond imagination!! All caps, nor bold do justice to how happy I am right now 🙂

    First I wanna give props to this question which got me pointed in the right direction. Now onto the answer, which is the most ideal variation I have found of the answer provided in the above link:

    The ItemDataBound event is the key, but it’s important to note that this event will fire for each item that exists in your ListView and for that reason, you must be careful in your approach. Here are two options that worked equally well for me.

    Option 1 – Most elegant; only runs FindControl on the item in question rather than all items.

    Protected Sub NewProduct_ItemDataBound(ByVal sender As ListView, ByVal e As ListViewItemEventArgs) Handles NewProduct.ItemDataBound
        Dim i As Integer = sender.EditIndex
        If i = e.Item.DataItemIndex Then
            Dim ddl As DropDownList = DirectCast(e.Item.FindControl("NewProductName_ddl"), DropDownList)
            Dim tb As TextBox = DirectCast(e.Item.FindControl("NewProductName_cb"), TextBox)
    
            ddl.Items.FindByValue(sender.DataKeys(i)("ID").ToString).Selected = True 'Prefills the DropDownList
            tb.Text = sender.DataKeys(i)("Product").ToString 'Prefills the TextBox
        End If
    End Sub
    

    Option 2 – Based on the referenced question, but with a crucial check to ensure non-null object.

    Protected Sub NewProduct_ItemDataBound(ByVal sender As ListView, ByVal e As ListViewItemEventArgs) Handles NewProduct.ItemDataBound
        Dim i As Integer = sender.EditIndex
        If i > -1 Then
            Dim ddl As DropDownList = DirectCast(e.Item.FindControl("NewProductName_ddl"), DropDownList)
            Dim tb As TextBox = DirectCast(e.Item.FindControl("NewProductName_cb"), TextBox)
    
            If Not IsNothing(ddl) Then
                ddl.Items.FindByValue(sender.DataKeys(i)("ID").ToString).Selected = True 'Prefills the DropDownList
            End If
            If Not IsNothing(tb) Then
                tb.Text = sender.DataKeys(i)("Product").ToString 'Prefills the TextBox
            End If
        End If
    End Sub
    

    I may make improvements to this answer later, but this did the trick for me. 🙂

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

Sidebar

Related Questions

I have a setup, something confusing like this: <ListView> <ItemTemplate> <div id=Content> <asp: TextBox
I have a ListView which layout looks like a Windows Explorer view (icon +
I have a list view like below: <asp:ListView ID=lstTopRanks runat=server> <ItemTemplate> <div class=Amazing-{recordNumber}>{itemdata}</div> </ItemTemplate>
I have a ListView on my checkout page with an ItemTemplate which build up
I have a listview which has an ItemTemplate and EditTemplate. When I switch an
I have a listview like this : <telerik:RadListView ID=rlv_available_sys runat=server ItemPlaceholderID=sys_holder DataKeyNames=process_id OnItemDataBound=rlv_available_sys_ItemDataBound> <ItemTemplate>
I have a listview control on an .aspx page. Inside this list view i
I have got this in my ItemTemplate of the listView: <ItemTemplate> <tr style=background-color: #FFFBD6;color:
I have a ListView with a specific ItemTemplate, which visually displays the contents of
I currently have a LinkButton in the ItemTemplate of a ListView. Each button in

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.