I am some what of a newbie to ASP.NET 2.0 but no stranger to .Net. I am stumped on implementing a template dropdownlist when attempting to bind to an ArrayList object the DropDownList to a object via code behind.
I am trying to create a global method in a class that allows a listControl to be passed in, have its listItem properties filled and then return the object to the calling page with a populated list.
Here is my class (globalList.cs):
public static class globalList
{
static globalList()
{
}
public static void bindListControlToGlobalSelectionList(ListControl listControl)
{
listControl.Items.Add(new ListItem("No", "0"));
listControl.Items.Add(new ListItem("Yes", "1"));
}
}
The drive here is to be able to manipulate the List (DropDownList) object in the DetailsView object using only the codebehind.
The problem is that this works absolutely fine in a standard web form even when using the codebehind but I cannot get it to function while the List (DropDownList) object is in the <InsertITemTemplate> ir tge <EditItemTemplate> of the DetailView.
Here is the code that works (A simple web form and a simple method calling script in the code behind):
I've listed the code above for brevity but basically I cannot get the template to add the listitems to the dropdownlist at all in the edit or insert templates.
I have looked at a lot of near similar inquiries to this an none are using an ArrayList via class object to bind the List control like I am trying to do.
Is this impossible to do using a DetailsView control and should I be using a FormView?
Can someone please provide an answer or some direction on this? Some full blown example code would be more than welcome.
Again, I understand how to bind the list using a datasource control as well as a db datasource locally but this was an aim at a more object oriented approach.
The key here is that the at Page_Load (before postback), only the controls in the template of DetailsViews for the default mode are instanciated. I expect if you ran your code with the default mode set to edit, it would find the control. Assuming the default
mode is ReadOnly, the controls in the EditItemTemplate are instanciated after the mode is changed to edit and the DetailsView is databound.
Two options you could try:
1. Try to find the control in the DetailsView OnDataBound event if the mode is edit
2. Populate the data in one of the DropDownList's events (e.g. load)
Hope that helps.
Aaron
Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
I'd probably go with the OnLoad on the DropDownList, which will fire no more than once per post. The DetailsView OnItemCreated can fire several times. For example, on any post that changes the mode (edit, update, cancel), it's fired once before ProcessPostData
with the original mode, then again after the mode is changed with the newnew mode.
Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
I'm in the situation. I think calling this code in ItemCreated is much preferable. Also if you call this even everytime you request edit to then use OnModeChange event.
I'll test this and post back the results of my test.
The problem has to do with when the DropDownList is created. When ModeChanging is fired, it's before the transition to edit mode, the DetailsView is still in ReadOnly mode, and controls in the EditItemTemplates have not been created. Even ModeChanged,
which is after the transition to Edit mode, is before the controls in the EditItemTemplates are created.
To really see all this in action, I set up a simple test page with a DetailsView with methods for all of these various events that checks for the existence of the ItemTemplate or EditItem template controls and writes a status to the Trace, so you can see
the whole sequence.
Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
which was solved by reading, and re-reading, this thread. Thank you to everyone who participated here. In researching my problem I found this thread has been valuable to a number of other posts as well.
ngenius
Member
5 Points
8 Posts
Advanced DropDownList in DetailsView binding to ArrayList
Dec 19, 2006 02:04 AM|LINK
I am some what of a newbie to ASP.NET 2.0 but no stranger to .Net. I am stumped on implementing a template dropdownlist when attempting to bind to an ArrayList object the DropDownList to a object via code behind.
I am trying to create a global method in a class that allows a listControl to be passed in, have its listItem properties filled and then return the object to the calling page with a populated list.
Here is my class (globalList.cs):
The drive here is to be able to manipulate the List (DropDownList) object in the DetailsView object using only the codebehind.
The problem is that this works absolutely fine in a standard web form even when using the codebehind but I cannot get it to function while the List (DropDownList) object is in the <InsertITemTemplate> ir tge <EditItemTemplate> of the DetailView.
Here is the code that works (A simple web form and a simple method calling script in the code behind):
.....And that works beautifully. Which is why I DO NOT understand why I can't get it to work in the DetailsView scenario as broken down below:
ANd in the codebehind...
I've listed the code above for brevity but basically I cannot get the template to add the listitems to the dropdownlist at all in the edit or insert templates.
I have looked at a lot of near similar inquiries to this an none are using an ArrayList via class object to bind the List control like I am trying to do.
Is this impossible to do using a DetailsView control and should I be using a FormView?
Can someone please provide an answer or some direction on this? Some full blown example code would be more than welcome.
Again, I understand how to bind the list using a datasource control as well as a db datasource locally but this was an aim at a more object oriented approach.
Thanks for the help.
ngenius
DetailsView DropDownList advanced arraylist
agolden
Star
7893 Points
1060 Posts
Re: Advanced DropDownList in DetailsView binding to ArrayList
Dec 19, 2006 02:52 AM|LINK
The key here is that the at Page_Load (before postback), only the controls in the template of DetailsViews for the default mode are instanciated. I expect if you ran your code with the default mode set to edit, it would find the control. Assuming the default mode is ReadOnly, the controls in the EditItemTemplate are instanciated after the mode is changed to edit and the DetailsView is databound.
Two options you could try:
1. Try to find the control in the DetailsView OnDataBound event if the mode is edit
2. Populate the data in one of the DropDownList's events (e.g. load)
Hope that helps.
Aaron
rexlin
Star
9413 Points
1751 Posts
Re: Advanced DropDownList in DetailsView binding to ArrayList
Dec 19, 2006 03:02 AM|LINK
Hi, ngenius :
Is it the main problem that you can not access the ddl in the insert and edit template?
If so, may this helps:
foreach (DetailsViewRow dr in this.DetailsView1.Rows)
{
if (dr.RowType == DataControlRowType.DataRow)
{
if (dr.RowState == DataControlRowState.Edit || dr.RowState == DataControlRowState.Insert)
{
DropDownList DDL = (DropDownList)dr.FindControl("DropDownListID");
globalList.bindListControlToGlobalSelectionList(DDL);
}
}
}
Best Regards,
__________________________________________________
Sincerely,
Rex Lin
Microsoft Online Community Support
This posting is provided "AS IS" with on warranties, and confers no rights.
ngenius
Member
5 Points
8 Posts
Re: Advanced DropDownList in DetailsView binding to ArrayList
Dec 19, 2006 02:11 PM|LINK
Thanks gang. Both responses provided some good insight into the issue.
I guess now my main question is exactly which event is the best event to place the code in?
Should I be using the ItemCreate Event of the DetailsView control, the or the OnLoad event of the DropDownList?
Is there a performance issue with using one of the above or any other variation?
Thanks in advance.
agolden
Star
7893 Points
1060 Posts
Re: Advanced DropDownList in DetailsView binding to ArrayList
Dec 20, 2006 01:24 AM|LINK
bhav27
Member
79 Points
107 Posts
Re: Advanced DropDownList in DetailsView binding to ArrayList
Jan 18, 2007 08:15 AM|LINK
I'm in the situation. I think calling this code in ItemCreated is much preferable. Also if you call this even everytime you request edit to then use OnModeChange event.
I'll test this and post back the results of my test.
Thanks guys.
bhav27
Member
79 Points
107 Posts
Re: Advanced DropDownList in DetailsView binding to ArrayList
Jan 18, 2007 09:44 AM|LINK
You can also use ModeChanging event like this
protected void dtlSample_ModeChanging(object sender, DetailsViewModeEventArgs e) { if (e.NewMode == DetailsViewMode.Edit) { DropDownList ddlGetAge = (DropDownList)dtlSample.FindControl("ddlAge"); LoadDropdownbox(ddlGetAge); } }But the only problem is(DropDownList)dtlSample.FindControl("ddlAge"); return null. Does anyone know the reason why DetailsView cannot find controls within it?agolden
Star
7893 Points
1060 Posts
Re: Advanced DropDownList in DetailsView binding to ArrayList
Jan 18, 2007 01:32 PM|LINK
The problem has to do with when the DropDownList is created. When ModeChanging is fired, it's before the transition to edit mode, the DetailsView is still in ReadOnly mode, and controls in the EditItemTemplates have not been created. Even ModeChanged, which is after the transition to Edit mode, is before the controls in the EditItemTemplates are created.
To really see all this in action, I set up a simple test page with a DetailsView with methods for all of these various events that checks for the existence of the ItemTemplate or EditItem template controls and writes a status to the Trace, so you can see the whole sequence.
<%@ Page Language="C#" Trace="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void DetailsView1_DataBound(object sender, EventArgs e) { Label StartedLabel = (Label)DetailsView1.FindControl("StartedLabel"); TextBox StartTextbox = (TextBox)DetailsView1.FindControl("StartTextbox"); string message = ""; if (StartedLabel != null) message += "label: " + StartedLabel.Text; if (StartTextbox != null) message += "textbox: " + StartTextbox.Text; Trace.Warn("DetailsView1_DataBound", message); } protected void Page_Load(object sender, EventArgs e) { Label StartedLabel = (Label)DetailsView1.FindControl("StartedLabel"); TextBox StartTextbox = (TextBox)DetailsView1.FindControl("StartTextbox"); string message = ""; if (StartedLabel != null) message += "label: " + StartedLabel.Text; if (StartTextbox != null) message += "textbox: " + StartTextbox.Text; Trace.Warn("Page_Load", message); } protected void DetailsView1_ModeChanged(object sender, EventArgs e) { Label StartedLabel = (Label)DetailsView1.FindControl("StartedLabel"); TextBox StartTextbox = (TextBox)DetailsView1.FindControl("StartTextbox"); string message = ""; if (StartedLabel != null) message += "label: " + StartedLabel.Text; if (StartTextbox != null) message += "textbox: " + StartTextbox.Text; Trace.Warn("DetailsView1_ModeChanged", message); } protected void StartTextbox_Load(object sender, EventArgs e) { TextBox StartTextbox = (TextBox)sender; string message = ""; if (StartTextbox != null) message += "textbox: " + StartTextbox.Text; Trace.Warn("StartTextbox_Load", message); } protected void DetailsView1_ItemCreated(object sender, EventArgs e) { Label StartedLabel = (Label)DetailsView1.FindControl("StartedLabel"); TextBox StartTextbox = (TextBox)DetailsView1.FindControl("StartTextbox"); string message = ""; if (StartedLabel != null) message += "label: " + StartedLabel.Text; if (StartTextbox != null) message += "textbox: " + StartTextbox.Text; Trace.Warn("DetailsView1_ItemCreated", message); } protected void DetailsView1_Load(object sender, EventArgs e) { Label StartedLabel = (Label)DetailsView1.FindControl("StartedLabel"); TextBox StartTextbox = (TextBox)DetailsView1.FindControl("StartTextbox"); string message = ""; if (StartedLabel != null) message += "label: " + StartedLabel.Text; if (StartTextbox != null) message += "textbox: " + StartTextbox.Text; Trace.Warn("DetailsView1_Load", message); } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <style type="text/css"> body { font: 1em Verdana; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT [ID], [Name], [Started] FROM [DateTest2]" UpdateCommand="UPDATE DateTest2 SET Name=@Name, Started=@Started WHERE ID=@ID" InsertCommand="INSERT INTO DateTest2 (Name, Started) VALUES (@Name, @Started)"> <UpdateParameters> <asp:Parameter Name="Name" /> <asp:Parameter Name="Started" /> <asp:Parameter Name="ID" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="Name" /> <asp:Parameter Name="Started" /> </InsertParameters> </asp:SqlDataSource> <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False" DataSourceID="SqlDataSource1" Height="50px" DataKeyNames="ID" DefaultMode="ReadOnly" AutoGenerateEditButton="true" AutoGenerateInsertButton="true" Width="500px" PagerSettings-Mode="NextPreviousFirstLast" OnDataBound="DetailsView1_DataBound" OnModeChanged="DetailsView1_ModeChanged" OnItemCreated="DetailsView1_ItemCreated" OnLoad="DetailsView1_Load"> <Fields> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:TemplateField HeaderText="Started" SortExpression="Started"> <EditItemTemplate> <asp:TextBox ID="StartTextbox" runat="server" Text='<%# Bind("Started") %>' OnLoad="StartTextbox_Load"></asp:TextBox> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox ID="StartTextbox" runat="server" Text='<%# Bind("Started") %>' OnLoad="StartTextbox_Load"></asp:TextBox> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="StartedLabel" runat="server" Text='<%# Bind("Started") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Fields> </asp:DetailsView> </div> </form> </body> </html>se_gordon
Member
17 Points
48 Posts
Re: Advanced DropDownList in DetailsView binding to ArrayList
Apr 19, 2007 02:13 AM|LINK
I had an analogous problem
http://forums.asp.net/1672073/ShowThread.aspx#1672073
which was solved by reading, and re-reading, this thread. Thank you to everyone who participated here. In researching my problem I found this thread has been valuable to a number of other posts as well.