472,986 Members | 2,881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 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 2295
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.