Asked by:
ListView can't find control in edit item template

Question
-
User646364117 posted
My problem is that I have a dropdown list in a ListView. When I go into Edit it is changing from its selected value.
I am trying to get the proper value from the database and rebind the dropdownlist.
I can't find the hidden field that holds the selected value. Get error
Object reference not set to an instance of an object
Dim hfTempID As HiddenField = CType(LvTemplate.EditItem.FindControl("hfTempID"), HiddenField)
My code is
Private Sub LvTemplate_ItemEditing(sender As Object, e As ListViewEditEventArgs) Handles LvTemplate.ItemEditing Dim TempID As Integer Dim catid As Integer Dim hfTempID As HiddenField = CType(LvTemplate.EditItem.FindControl("hfTempID"), HiddenField) TempID = Convert.ToInt16(hfTempID.Value) catid = GetCategoryIDForTemplate(TempID) 'Get selected category from db and populate ddl Dim ddlCategoryEdit As DropDownList = CType(LvTemplate.EditItem.FindControl("ddlCategoryEdit"), DropDownList) ddlCategoryEdit.SelectedValue = catid LvTemplate.EditIndex = e.NewEditIndex BindLvTemplate() BindGvCategoryTemplate() End Sub
My markup is
<asp:ListView ID="LvTemplate" runat="server" InsertItemPosition="LastItem"> <EditItemTemplate> <tr style="width: 100%;"> <td style="border: thin solid #C0C0C0; border-collapse: collapse;"> <asp:TextBox ID="txtTemplateName1" runat="server" Text='<%#Eval("name") %>' /> </td> <td> <asp:DropDownList ID="ddlCategoryEdit" runat="server" AppendDataBoundItems="true"> </asp:DropDownList> <asp:HiddenField ID="hfTempID" runat="server" Value='<%# Eval("TempID") %>'></asp:HiddenField> </td> <td style="border: thin solid #C0C0C0; border-collapse: collapse;"> <asp:TextBox ID="txtSubjectEdit" runat="server" Text='<%#Eval("subject") %>' /> </td> <td style="border: thin solid #C0C0C0; border-collapse: collapse;"> <asp:ImageButton ID="btnUpdate" runat="server" ImageUrl="images/save_20.png" CommandName="Update" CommandArgument='<%# Eval("TempID") %>' /> <asp:ImageButton ID="btnCancelEdit" runat="server" ImageUrl="images/cancel_20.png" CommandName="Cancel" /> </td> </tr> </EditItemTemplate>
Monday, March 9, 2020 1:34 PM
All replies
-
User475983607 posted
The syntax is...
Dim item AS ListViewItem = LvTemplate.Items(e.NewEditIndex) Dim hfTempID As HiddenField = CType(item.FindControl("hfTempID"), HiddenField)
... which can be found in the official ItemEditing handler documentation.
Monday, March 9, 2020 1:43 PM -
User646364117 posted
Hi,
Thanks for responding.
I changed my code to
Private Sub LvTemplate_ItemEditing(sender As Object, e As ListViewEditEventArgs) Handles LvTemplate.ItemEditing Dim item As ListViewItem = LvTemplate.Items(e.NewEditIndex) Dim hfTempID As HiddenField = CType(item.FindControl("hfTempID"), HiddenField) Dim TempID As Integer Dim catid As Integer TempID = Convert.ToInt16(hfTempID.Value) catid = GetCategoryIDForTemplate(TempID) 'Get selected category from db and populate ddl Dim ddlCategoryEdit As DropDownList = CType(item.FindControl("ddlCategoryEdit"), DropDownList) ddlCategoryEdit.SelectedValue = catid LvTemplate.EditIndex = e.NewEditIndex BindLvTemplate() BindGvCategoryTemplate() End Sub
I moved the hidden field hfTempID unto the item template and now that is found.
However, my goal is to set the selected value of ddlCategoryEdit which is in the EditItemTemplate.
Now this control is not being found so I don't get to the line where I set it's value
Monday, March 9, 2020 2:39 PM -
User475983607 posted
As I understand the original issue has been solved. Now you have a new problem?
Is the problem the dropdown is not bound to data? Or maybe the selected items does not exist?
Please take a moment to set a break point and step through your code. Share the actual error message if there is an exception.
Monday, March 9, 2020 3:05 PM -
User646364117 posted
This code solved my problem. I think the key was to bind the listview in ItemEditing and then find the controls
Private Sub LvTemplate_ItemEditing(sender As Object, e As ListViewEditEventArgs) Handles LvTemplate.ItemEditing LvTemplate.EditIndex = e.NewEditIndex BindLvTemplate() BindGvCategoryTemplate() Dim CatID As Integer Dim ddlCategoryEdit As DropDownList = CType(LvTemplate.EditItem.FindControl("ddlCategoryEdit"), DropDownList) Dim hf As HiddenField = CType(LvTemplate.EditItem.FindControl("hfCatID"), HiddenField) CatID = Convert.ToInt16(hf.Value) ddlCategoryEdit.SelectedValue = CatID End Sub
Monday, March 9, 2020 4:14 PM