Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing Controls on ListView Edit Command
    primarykey
    data
    text
    <p>In my <code>ListView</code> I have an <code>ItemTemplate</code> and <code>EditItemTemplate</code> which look something like this, respectively.</p> <p><img src="https://i.stack.imgur.com/hew0h.png" alt="enter image description here"> -------> <img src="https://i.stack.imgur.com/FkNdq.png" alt="enter image description here"></p> <p>When I click the "Edit" button, and it switches to the <code>EditItemTemplate</code> view on the right, I want to prefill the <code>Textbox</code> and select the corresponding <code>option</code> in the <code>DropDownList</code>. How can I do this?</p> <p>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 :(</p> <pre class="lang-vb prettyprint-override"><code>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) </code></pre> <p><strong>UPDATE - RAWR!!</strong></p> <p>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 <em>NullReferenceException</em> :(</p> <pre class="lang-vb prettyprint-override"><code>'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 &gt; -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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload