473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Setting values of Controls used multiple times on a page

Hello,

I have created a custom dropdownlist that is used multiple times within
a single page. When trying to set the values of the controls with the
page in which they are being used, they all are set to the value of the
last control. I found this link:
http://www.codecomments.com/archive3...-1-366113.html which I think
explains the situation but I'm not sure how to implement the solution
in my case since I am not using a .ascx control, but a programmatically
created control.

++++++++++++++++++
Following is an abbreviated example of what I'm trying to do:

In the .ascx page
<%@register tagprefix=custom %>
<custom:mydropdownlist></custom:mydropdownlist>

In the .ascx.vb page

Private Sub LoadInfoFromSession()
mydropdownlist1.selectedvalue = 1
mydropdownlist2.selectedvalue = 2
mydropdownlist3.selectedvalue = 3
End Sub

Private Sub Page_PreRender()
LoadInfoFromSession()
'Note it doesn't work in Page_Load at all
End Sub

..vb File that contains dropdownlist
Public Class MyDropDownList
Inherits dropdownlist
End Class
+++++++++++++++++++++++++++

When the control containing the dropdownlists is loaded, they are all
set to 3. I have tried using the INamingContainer, Page.Databinding,
and a bunch of other optons. None have worked.

If I use a system dropdownlist, I have no problems setting the
individual values.

Any help is greatly appreciated.

Thanks,

James

Nov 19 '05 #1
9 2318
I think I did the same thing as you do. The problem is that controls
provides by .NET library save their property data in the ViewState and upon
each roundtrip the previous property data will be retrieved from postback
data. Whereas your customer control ( Assume that is your Programmically
Created Control) does not do that. Upon each round trip they are removed and
recreated on your webpage, if they are created dynamically.

There are couple of solutions depends on how you create your controls
1. Store property data of customer control explicitly in ViewState or
Session. The problem with Session is that if you put two customer controls
on one page, they will share the session data.
2. If you are using non-native data type, you have to overwrite postback
event handler to store and retrieve viewstate data.
3. It seems if you add your customer control or user control on the .aspx
page using VS graphic designer, (create them statically), the compiler will
handle the ViewState processing for your customer control.

It took some try and error session for me to be pretty good at controls. The
following link give a good description of the background.
http://msdn.microsoft.com/library/de...nLifecycle.asp
<ja*************@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hello,

I have created a custom dropdownlist that is used multiple times within
a single page. When trying to set the values of the controls with the
page in which they are being used, they all are set to the value of the
last control. I found this link:
http://www.codecomments.com/archive3...-1-366113.html which I think
explains the situation but I'm not sure how to implement the solution
in my case since I am not using a .ascx control, but a programmatically
created control.

++++++++++++++++++
Following is an abbreviated example of what I'm trying to do:

In the .ascx page
<%@register tagprefix=custom %>
<custom:mydropdownlist></custom:mydropdownlist>

In the .ascx.vb page

Private Sub LoadInfoFromSession()
mydropdownlist1.selectedvalue = 1
mydropdownlist2.selectedvalue = 2
mydropdownlist3.selectedvalue = 3
End Sub

Private Sub Page_PreRender()
LoadInfoFromSession()
'Note it doesn't work in Page_Load at all
End Sub

.vb File that contains dropdownlist
Public Class MyDropDownList
Inherits dropdownlist
End Class
+++++++++++++++++++++++++++

When the control containing the dropdownlists is loaded, they are all
set to 3. I have tried using the INamingContainer, Page.Databinding,
and a bunch of other optons. None have worked.

If I use a system dropdownlist, I have no problems setting the
individual values.

Any help is greatly appreciated.

Thanks,

James

Nov 19 '05 #2
Hey C.F,

Thanks for the response. I've started playing around with the page
lifecycle and found that no matter where I set the value within the
control (even in PreRender) all of the values are set the same. If
there are controls in order A-B-C, all of the controls will always have
the value of C. I did find that PreRender was the last function where
it could be set, but say if I set control A in preRender after control
C was set earlier in the page lifecycle it would still have the value
of A.

What I am trying to avoid is having to set a controls values within the
page that contains it. Maybe the answer is an event handler in the
prerender of the page that fires a reset in the control. It seems me
to be a bit clunky, but I will try that since I just thought of it.

Thanks again for your thoughts as you've inspired me to try things
another way.

James

Nov 19 '05 #3
I tried setting the values in the PAGE prerender as opposed to the
control prerender and got the same result. All of the ddl values are
set to the value of the last dropdownlist that was set. Very
frustrating for one custom control.

Nov 19 '05 #4
Yeah, it can be frustrating sometime, but there are lots of joy when you get
it right. Can you post some code section for us to look at? Thanks.

<ja*************@gmail.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Hey C.F,

Thanks for the response. I've started playing around with the page
lifecycle and found that no matter where I set the value within the
control (even in PreRender) all of the values are set the same. If
there are controls in order A-B-C, all of the controls will always have
the value of C. I did find that PreRender was the last function where
it could be set, but say if I set control A in preRender after control
C was set earlier in the page lifecycle it would still have the value
of A.

What I am trying to avoid is having to set a controls values within the
page that contains it. Maybe the answer is an event handler in the
prerender of the page that fires a reset in the control. It seems me
to be a bit clunky, but I will try that since I just thought of it.

Thanks again for your thoughts as you've inspired me to try things
another way.

James

Nov 19 '05 #5
I'm not sure how much code to post because it would be a lot....here's
another description:
Here's an object heirarchy

webpage.aspx
placeholder
myform.ascx
mycustomdropdownlist

inside myform.ascx I am trying to set the value of mycustomdropdownlist
from session. I am just wondering why the custom dropdownlist can't be
set, but a regular dropdownlist works just fine...

I also figured out that the value is correct when you look at the
object inside the myform.ascx: ie

Public Class MyForm
Protected withevents CustomDDL

Private Sub LoadFromSession()
customddl.selectedvalue = 1

httpcontext.current.response.write(customddl.selec tedvalue) 'gives the
correct value
End Sub
End Class

But the when you look inside the customDDL's onPreRender the value is
wrong:

Public Class CustomDDL
Inherits dropdownlist
Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
HttpContext.Current.Response.Write(Me.SelectedItem .Text &
"<br>") 'this is the wrong value
End Sub

End Class

I hope this helps to clarify what I'm trying to accomplish....

Nov 19 '05 #6
Basically it seems at though the default render method is what is
killing me. It is where the values set in prerender are being
overwritten in the custom objects. I've spent quite a bit of time
seeing where the values get changed. The values always get changed
AFTER prerender. All of the microsoft documentation seems to say this
shouldn't happen, but it does. At this point, unless, I find a better
solution, I am not going to use the custom object and use the default
dropdownlist which works just fine. I can set instances indivdually
from session so my back button will work correctly. Admittedly, I am a
little disappointed what was seemingly straight forward turned into
hours of time reviewing the page life cycle, only to find, at least
according to my current findings that custom controls are easy for
submit and reading but not so easy for resetting.

Not being able to use the custom object means I'll have to rewrite my
object using a class that will set the values for me while retaining
the original class unaltered. So much for inheritance simplifiying
things. I really wish that when an object was inherited it really took
on all of the attributes of the it's parent was was treated by the
system as such especially when key function were NOT overridden.

Nov 19 '05 #7
If you set the value of dropdownlist from session, do you have seperate
session variables for each ddl? The session variables are shared among your
pages. Is it why all ddl end up with the same value?

Here is what I did with DDL and I did not have your problem before. In the
customized DDL, I will overwrite CreateChildControls(). The following
function is a ddl that shows the last few years.

namespace PCUpload.Control

{

/// <summary>

/// Summary description for DDLYear.

/// </summary>

[DefaultProperty("Text"),

ToolboxData("<{0}:DDLYear runat=server></{0}:DDLYear>")]

public class DDLYear : System.Web.UI.WebControls.DropDownList

{

[Bindable(true),

Category("Appearance"),

DefaultValue("")]

protected override void CreateChildControls()

{

// get the year of last month and add other years

int year = DateTime.Now.AddMonths(-1).Year;

this.Items.Clear();

this.Items.Add(new ListItem("All", "0"));

this.Items.Add(new ListItem(Convert.ToString(year-4),
Convert.ToString(year-4)));

this.Items.Add(new ListItem(Convert.ToString(year-3),
Convert.ToString(year-3)));

this.Items.Add(new ListItem(Convert.ToString(year-2),
Convert.ToString(year-2)));

this.Items.Add(new ListItem(Convert.ToString(year-1),
Convert.ToString(year-1)));

this.Items.Add(new ListItem(Convert.ToString(year),
Convert.ToString(year)));

int i = Convert.ToInt16(PCUpload.Util.MySession.Year);

this.SelectedIndex = -1;

this.SelectedIndex = (i==0)? 0 : i-year+5; // convert to index of
dropdownlist

}

}

}
in the user control that uses the ddl, I used the @Register to add the ddl
statically. Upon roundtrip the DDL will remember its own selectedIndex
value. Amazingly
..NET handled it for you. Hope this will help.

<ja*************@gmail.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
I'm not sure how much code to post because it would be a lot....here's
another description:
Here's an object heirarchy

webpage.aspx
placeholder
myform.ascx
mycustomdropdownlist

inside myform.ascx I am trying to set the value of mycustomdropdownlist
from session. I am just wondering why the custom dropdownlist can't be
set, but a regular dropdownlist works just fine...

I also figured out that the value is correct when you look at the
object inside the myform.ascx: ie

Public Class MyForm
Protected withevents CustomDDL

Private Sub LoadFromSession()
customddl.selectedvalue = 1

httpcontext.current.response.write(customddl.selec tedvalue) 'gives the
correct value
End Sub
End Class

But the when you look inside the customDDL's onPreRender the value is
wrong:

Public Class CustomDDL
Inherits dropdownlist
Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
HttpContext.Current.Response.Write(Me.SelectedItem .Text &
"<br>") 'this is the wrong value
End Sub

End Class

I hope this helps to clarify what I'm trying to accomplish....


Nov 19 '05 #8
Thanks for the response. I really appreciate the dialog.

Yes the session variables are different for each ddl. They are pulled
from a single object in session that has properties for each of the
ddls.
From your example it looks as if you are setting the selected index

within the create child controls. My custom dll adds the list items in
the OnLoad function. Does this make a difference?

Also, when I am setting the selected value from session, it is
happening within the .ascx file that houses the dropdownlist thus:
Public Class MyASCX
Private Sub LoadInfoFromSession()
myddl.selectedvalue = foo.value
End Sub
End Class

I will try moving the add items piece to the createchildcontrols method
and see if that fixes the problem.

Thanks again,

James

Nov 19 '05 #9
Ok, I've figured out what the problem was. The custom dropdownlist was
populated by an arraylist from memory. I needed to create a new, fresh
copy of the arraylist for each dropdownlist. So I create a function
that did this and everything worked fine.

Thanks for all of your help,

James

Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
4
by: Gary Hasler | last post by:
I have a full page of thumbnails linked to home plan pages. I'm adding a mouseover/mouseout effect to the table cells, plus a onclick event to permanently set the cell background to indicate the...
2
by: John Hargrove | last post by:
I'm having trouble setting decimal place values in the results field of a sample management database I'm building for an environmental testing laboratory. The degree of sensitivity varies among...
1
by: .Net Newbie | last post by:
I am relatively new to .Net and have been coding an intranet site for my employer for a couple of months. I am currently stuck coding in a text-editor called EditPlus without access to the VS.Net...
3
by: Shikari Shambu | last post by:
Hi All, I have a situation where multiple applications are sharing some pages/ controls. So, I have a separate common project that has the common pages/ controls. My question is how do I...
0
by: .Net Newbie | last post by:
I am relatively new to .Net and have been coding an intranet site for my employer for a couple of months. I am currently stuck coding in a text-editor called EditPlus without access to the VS.Net...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
8
by: =?Utf-8?B?U2hhd24gUmFtaXJleg==?= | last post by:
I am coming at the world from the standpoint of a veteran ASP Classic developer. I am not clear on the advantages of using the ASP.NET control over standard HTML control. With the ASP.NET...
0
by: erkbiz | last post by:
Hello, this is my first post, I hope this is the right forum to ask my question. I am dynamically loading a user control multiple times. I have a gridview and have inserted empty rows. Into these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.