473,732 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 programmaticall y
created control.

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

In the .ascx page
<%@register tagprefix=custo m %>
<custom:mydropd ownlist></custom:mydropdo wnlist>

In the .ascx.vb page

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

Private Sub Page_PreRender( )
LoadInfoFromSes sion()
'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 INamingContaine r, Page.Databindin g,
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 2343
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.goo glegroups.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 programmaticall y
created control.

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

In the .ascx page
<%@register tagprefix=custo m %>
<custom:mydropd ownlist></custom:mydropdo wnlist>

In the .ascx.vb page

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

Private Sub Page_PreRender( )
LoadInfoFromSes sion()
'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 INamingContaine r, Page.Databindin g,
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******** *************@l 41g2000cwc.goog legroups.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
mycustomdropdow nlist

inside myform.ascx I am trying to set the value of mycustomdropdow nlist
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.selec tedvalue = 1

httpcontext.cur rent.response.w rite(customddl. selectedvalue) '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(ByV al e As EventArgs)
HttpContext.Cur rent.Response.W rite(Me.Selecte dItem.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 CreateChildCont rols(). The following
function is a ddl that shows the last few years.

namespace PCUpload.Contro l

{

/// <summary>

/// Summary description for DDLYear.

/// </summary>

[DefaultProperty ("Text"),

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

public class DDLYear : System.Web.UI.W ebControls.Drop DownList

{

[Bindable(true),

Category("Appea rance"),

DefaultValue("" )]

protected override void CreateChildCont rols()

{

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

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

this.Items.Clea r();

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

this.Items.Add( new ListItem(Conver t.ToString(year-4),
Convert.ToStrin g(year-4)));

this.Items.Add( new ListItem(Conver t.ToString(year-3),
Convert.ToStrin g(year-3)));

this.Items.Add( new ListItem(Conver t.ToString(year-2),
Convert.ToStrin g(year-2)));

this.Items.Add( new ListItem(Conver t.ToString(year-1),
Convert.ToStrin g(year-1)));

this.Items.Add( new ListItem(Conver t.ToString(year ),
Convert.ToStrin g(year)));

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

this.SelectedIn dex = -1;

this.SelectedIn dex = (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******** *************@l 41g2000cwc.goog legroups.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
mycustomdropdow nlist

inside myform.ascx I am trying to set the value of mycustomdropdow nlist
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.selec tedvalue = 1

httpcontext.cur rent.response.w rite(customddl. selectedvalue) '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(ByV al e As EventArgs)
HttpContext.Cur rent.Response.W rite(Me.Selecte dItem.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 LoadInfoFromSes sion()
myddl.selectedv alue = foo.value
End Sub
End Class

I will try moving the add items piece to the createchildcont rols 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
5015
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
1795
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 user's latest choice. This is in addition to the normal A HREF link, and seems to work fine. I set up anchors (A name="") on the thumbnail page, and the "Back to Plans" link from the home plan page contains a hash (#) to scroll the page down and...
2
3748
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 test methods; consequently, some results are reported to 2 decimal places, some to 3, etc. The Results subform consists of Test Parameter, Result, Report Unit, Analysis Date, Analyst and other fields. The test parameter control is a drop-down...
1
1774
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 IDE, and coding pages with the script directly in each page (not using the codebehind method) as most of my learning material has demonstrated. I started with the IBuySpy Portal (SDK version not VS Version) as my base portal and have coded...
3
2793
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 reference these pages/ controls from my ASP.NET web projects WEbApp1 url http://localhost/app1 C:\Apps\App1
0
1236
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 IDE, and coding pages with the script directly in each page (not using the codebehind method) as most of my learning material has demonstrated. I started with the IBuySpy Portal (SDK version not VS Version) as my base portal and have coded...
1
6502
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" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
8
2348
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 controls the rendered names are changed and I appear to be limited in what javascript events I can fire. I also can not seem to figure out how to setup a onsubmit() event. With standard HTML I can setup any javascript event I want and the names...
0
1246
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 empty rows I am dynamically loading a .ascx user control (it too being a gridview) multiple times each time it loads it renders different data. I want to be able to access the data in the different gridviews is this possible? This snippet is...
0
8773
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9445
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9306
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9234
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.