FindControl now works as intended see post #5 in this thread for the solution.
There are two unresolved issues, see post #6 in this thread:
1) Editing the first item still results in FindControl returning null instead of the correct control. This doesn't happen for all other entries (2nd and onwards, including the last item).
2) After inserting an item or canceling the insert the dropdownlist in the InsertItemTemplate shows nothing anymore because DataBind() is called again as the if(!Page.IsPostBack) doesn't properly work somehow
More detailed code and explanation in the posts below.
[EDIT: I remove code and text from the first post as it has become obsolete by the progress of this thread]
findcontrol
When this post is helpful to you please mark it as answer, so other people can quickly find the solution to a problem as well :-) Thank You!
Second suggestion: Check if you ddl in the Edit tempalte isn't inside another templated control, this would require you to do a FindControl in the tempalted control first and then, a FindControl in the tempalted control looking for the ddl.
Let me know how it goes.
Gabriel Bogéa (http://www.gbogea.com)
-----------------
Please 'Mark as Answer' the post(s) that helped you
ListView5.FindControl doesn't work at all, neither for InsertItem nor for EditItem Templates. Both dropdowns are directly in InsertItem and EditItem respectively and while the dropdown in the InsertItem is found with no probs it just won't work with the
EditItem. I am browsing around for days trying to find a solution now and just found a lot of other people having problems with dropdownlists in ListView as well but no solution in sight so far :(
When this post is helpful to you please mark it as answer, so other people can quickly find the solution to a problem as well :-) Thank You!
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Finding the control in PreRender seems to work, although that doesn't really help, because I need the control OnItemUpdating, here is a little more detailed explanation of what I am trying to do.
I created a dropdownlist from a datasource programmatically (code below)
FillDDLProjectManager is executed OnInit of the dropdownlist in ListView5 in the InsertTemplate and in ListView5select in the EditTemplate. The if condition for the DataBind() is there so the data isn't bound again when the DropDown posts back on changing a value because we only want to update the selected value and not list the data again which would show the selectable data twice in the dropdown.
Now ListView5 has this code in OnItemInserting="..."
Ok, been happy too early :( there are still quite a few problems left:
1) When editing the first item I still get the object reference not set to an instance of an object error
2) After inserting one item or clicking Cancel on the InsertItemTemplate the dropdown items vanish although inserting an item doesn't even reload the page (see the !Page.IsPostBack) in the code above) 3) When I click edit there's always the 1st item in the dropdown pre-selected but I want the actual value set in the database to be preselected. [SOLVED: Post #7 in this thread]
I really didn't think a simple dropdownlist will cause such headaches. But this single problem is throwing me totally off schedule because everything works except the dropdownlists in listviews :(
This is the code for the dropdownlists and listviews btw (sorry for posting so much code, but maybe we can finally solve this problem with the full code):
protected void HydroconsultDBUpdateHandler(object sender, SqlDataSourceStatusEventArgs e) { switch (e.AffectedRows) { default: if (UpdatePanelListView5.Visible == true) { Feedback.Text = "Projekt wurde hinzugefügt bzw. aktualisiert." + " (" + DateTime.Now + ")"; UpdatePanelListView5select.Update(); } else Feedback.Text = "Der Eintrag wurde hinzugefügt bzw. aktualisiert." + " (" + DateTime.Now + ")"; break; case 0: Feedback.Text = "Es konnte leider keine Änderung vorgenommen werden. Setz dich bitte mit dem Administrator in Verbindung." + " (" + DateTime.Now + ")"; if (e.Exception != null) /* We want to avoid showing Server Error Pages to the user so we output the error text to a label */ { Feedback.Text = e.Exception.Message; e.ExceptionHandled = true; } break; }
UpdatePanelStatus.Update(); }
protected void HydroconsultDBDeleteHandler(object sender, SqlDataSourceStatusEventArgs e) { switch (e.AffectedRows) { default: if (UpdatePanelListView5.Visible == true) { Feedback.Text = "Projekt wurde gelöscht." + " (" + DateTime.Now + ")"; UpdatePanelListView5select.Update(); } else Feedback.Text = "Der Eintrag wurde gelöscht." + " (" + DateTime.Now + ")"; break; case 0: Feedback.Text = "Dieser Eintrag kann nicht gelöscht werden. Setz dich bitte mit dem Administrator in Verbindung." + " (" + DateTime.Now + ")"; if (e.Exception != null) /* We want to avoid showing Server Error Pages to the user so we output the error text to a label */ { Feedback.Text = e.Exception.Message; e.ExceptionHandled = true; } break; }
UpdatePanelStatus.Update(); }
When this post is helpful to you please mark it as answer, so other people can quickly find the solution to a problem as well :-) Thank You!
This is most likely too late for your needs, but perhaps it will help someone else down the road. For item #2, try the following by simply adding a check for non null values in the drop down list prior to assigning a value for that particular variable:
ChristophS
Member
26 Points
29 Posts
ListView.EditItem.FindControl always returning null on 1st item and lots of ListView DropDownList...
Jun 15, 2008 04:04 PM|LINK
FindControl now works as intended see post #5 in this thread for the solution.
There are two unresolved issues, see post #6 in this thread:
1) Editing the first item still results in FindControl returning null instead of the correct control. This doesn't happen for all other entries (2nd and onwards, including the last item).
2) After inserting an item or canceling the insert the dropdownlist in the InsertItemTemplate shows nothing anymore because DataBind() is called again as the if(!Page.IsPostBack) doesn't properly work somehow
More detailed code and explanation in the posts below.
[EDIT: I remove code and text from the first post as it has become obsolete by the progress of this thread]
findcontrol
gbogea
Contributor
4019 Points
576 Posts
Re: FindControl always returning null
Jun 16, 2008 02:35 PM|LINK
It seems weird indeed. First suggestion: try to use the FindControl like this:
DropDownList ddl = (DropDownList)ListView5.FindControl("ddlProjectManager");
This will allow the same code for both templates.
Second suggestion: Check if you ddl in the Edit tempalte isn't inside another templated control, this would require you to do a FindControl in the tempalted control first and then, a FindControl in the tempalted control looking for the ddl.
Let me know how it goes.
-----------------
Please 'Mark as Answer' the post(s) that helped you
ChristophS
Member
26 Points
29 Posts
Re: FindControl always returning null
Jun 17, 2008 05:31 AM|LINK
ListView5.FindControl doesn't work at all, neither for InsertItem nor for EditItem Templates. Both dropdowns are directly in InsertItem and EditItem respectively and while the dropdown in the InsertItem is found with no probs it just won't work with the EditItem. I am browsing around for days trying to find a solution now and just found a lot of other people having problems with dropdownlists in ListView as well but no solution in sight so far :(
Samu Zhang -...
All-Star
62162 Points
6100 Posts
Re: FindControl always returning null
Jun 17, 2008 06:46 AM|LINK
Hi ChristophS ,
In general , it should work.
I think you put your code in the wrong place. You can write it in PreRender event.
protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e) { } protected void ListView1_PreRender(object sender, EventArgs e) { if (this.ListView1.EditIndex != -1) { TextBox box = this.ListView1.Items[this.ListView1.EditIndex].FindControl("nameTextBox") as TextBox; } }Samu Zhang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
ChristophS
Member
26 Points
29 Posts
Re: FindControl always returning null
Jun 17, 2008 10:36 AM|LINK
Finding the control in PreRender seems to work, although that doesn't really help, because I need the control OnItemUpdating, here is a little more detailed explanation of what I am trying to do.
I created a dropdownlist from a datasource programmatically (code below)
ChristophS
Member
26 Points
29 Posts
Re: FindControl always returning null
Jun 17, 2008 12:32 PM|LINK
Ok, been happy too early :( there are still quite a few problems left:
1) When editing the first item I still get the object reference not set to an instance of an object error
2) After inserting one item or clicking Cancel on the InsertItemTemplate the dropdown items vanish although inserting an item doesn't even reload the page (see the !Page.IsPostBack) in the code above)
3) When I click edit there's always the 1st item in the dropdown pre-selected but I want the actual value set in the database to be preselected.[SOLVED: Post #7 in this thread]I really didn't think a simple dropdownlist will cause such headaches. But this single problem is throwing me totally off schedule because everything works except the dropdownlists in listviews :(
This is the code for the dropdownlists and listviews btw (sorry for posting so much code, but maybe we can finally solve this problem with the full code):
And the SQL Source Handlers:
ChristophS
Member
26 Points
29 Posts
Re: ListView.EditItem.FindControl always returning null on 1st item and lots of ListView DropDown...
Jun 17, 2008 03:32 PM|LINK
This solves problem #3 from the above post:
protected void ListView5selected_ItemDataBound(object sender, ListViewItemEventArgs e) { if (ListView5select.EditIndex >= 0) { ListViewDataItem dataItem = (ListViewDataItem)e.Item; if (dataItem.DisplayIndex == ListView5select.EditIndex) { System.Data.Common.DbDataRecord rowView = (System.Data.Common.DbDataRecord)dataItem.DataItem; String selection = rowView[3].ToString(); DropDownList ddl = (DropDownList)dataItem.FindControl("ddlProjectManager"); ListItem item = ddl.Items.FindByValue(selection); ddl.SelectedIndex = ddl.Items.IndexOf(item); } } }Started with this sample code and modified it to my needs: http://msdn.microsoft.com/de-de/library/system.web.ui.webcontrols.listview.editindex.aspxdoberman8u
Member
4 Points
2 Posts
Re: FindControl always returning null
Aug 30, 2008 01:12 PM|LINK
This is most likely too late for your needs, but perhaps it will help someone else down the road. For item #2, try the following by simply adding a check for non null values in the drop down list prior to assigning a value for that particular variable:
protected void ListView5_ItemInserting(object sender, ListViewInsertEventArgs e){
if (Page.IsValid)
{
DropDownList ddl = (DropDownList)ListView5.InsertItem.FindControl("ddlProjectManager");
if (ddlProjectManager != null)
{
e.Values["EmployeeID"] = ddl.SelectedValue;
e.Cancel = false;
}
else
e.Cancel = true;
}
protected void ListView5select_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
if (Page.IsValid)
{
DropDownList ddl = (DropDownList)ListView5select.EditItem.FindControl("ddlProjectManager");
if (ddlProjectManager != null)
{
e.NewValues["EmployeeID"] = ddl.SelectedValue;
e.Cancel = false;
}
else
e.Cancel = true;
}
adefwebserver
Contributor
5908 Points
1205 Posts
MVP
Re: FindControl always returning null
Nov 05, 2008 05:52 AM|LINK
This worked for me:
I can bind a Dropdown that is in an Edit Template by using code such as this:
<asp:DropDownList ID="ddlPlannedMonth" SelectedValue='<%# Bind("PlannedMonth) %>' runat="server"> <asp:ListItem Value="1/1/2008">Jan - 2008</asp:ListItem> <asp:ListItem Value="2/1/2008">Feb - 2008</asp:ListItem> </asp:DropDownList>However, to actually update the proper value when a person changes the dropdown selection, I had to wire-up to the ItemUpdating event on the ListView.
Also note that I have a ListView wired-up to a LinqDataSource control.
Open Light Group
View Model (MVVM) Series | ADefWebserver.com
felixchen55
Member
2 Points
1 Post
Re: FindControl always returning null
May 11, 2009 02:07 PM|LINK
Cool, it also worked while wiring-up to a SqlDataSource control.
I also bound ListItems to SqlDataSource. Still worked. [:)]
[:D]