Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 22nd, 2005, 03:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default User Controls - Access Correct Poperties

Hi All,
I am trying to use a user control in one of my pages.
I have not been able to access the variable that are set during the execution on the Control.

Basically the contol allows a user to select a row from a datagrid.
The Primary key of that record is recorded in a private variable.

I then access the private variable with an Accessor.
This works fine with any default values in variables, but not when the value has changed through the execution of the controls purpose, only default values show.

I have been guessing that this is somehow related to the control not maintaining state but I am not sure what to do.

I have contructed a simple User Control to simplify the issue but still have the same problem.

Can saomeone please take a look at my logic and let me know where I am going wrong.

The simplified user control code is as below
Code:
    public class TestControl : System.Web.UI.UserControl
    {
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected System.Web.UI.WebControls.TextBox TextBox2;
        protected System.Web.UI.WebControls.Button Button1;
        public string Concat = string.Empty;
        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
        }
        private void Button1_Click(object sender, System.EventArgs e)
        {
            Concat = TextBox1.Text + " " + TextBox2.Text;
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///        Required method for Designer support - do not modify
        ///        the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

    }
including generated code

The code for the web page to access this is
Code:
        protected UserControls.TestControl TestControl1;
        private void Page_Load(object sender, System.EventArgs e)
        {
            Response.Write("CONCAT = " + TestControl1.Concat);
        }
and I have an instance of my TestControl in the aspx page.

I understand that being in the page load does not allow any values the first time round, but when I add values and Concatinate them with the control, it still doesn't show.

I hope Ive been clear enough with my problem here.
Any assistance would be greatly appreciated.

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old February 22nd, 2005, 09:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

http://p2p.wrox.com/topic.asp?TOPIC_ID=15969

 
Old February 23rd, 2005, 01:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks Stu,
I've read through the topic and have set up delegates on my test control.
All went well so I'll try it where I had intended it.

One thing though.

There still doesn't seem to be great access to the controls properties.
I can retreive control property after the delegated even has fired, but the next time the containing page postbacks.
The controls property is back to its default status even though the control itself maintains it state.

Is this the correct behaviour or am I still doing something wrong?
It seems a little strange that I should have to maintain the state of a variable in the containing page.

Thanks again for you assistance,
Ive deffinately made headway on this but quite understand the factors effecting my situation.


======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old February 23rd, 2005, 09:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

G'day mate. Wish I was visiting Australia and/or New Zealand today!

Here's where I used it:
http://www.as.westga.edu/default.aspx?tabid=6

The "Dates to Remember - Spring 2005" is on the parent page and everything in the box directly below that is a user control. If you click the drop down and change the semester the event will cause the semester you clicked on to be sent to the parent. I followed this page http://www.odetocode.com/code/94.aspx to get it to work.

 
Old February 23rd, 2005, 08:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks for the link.
I had used delegates top get it working but this event bubbling looks a lot cleaner.
I'll give that a try also.
My control is used in a datagrid which makes things a little messy with the delegates.

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old February 24th, 2005, 03:30 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

you had,
public string Concat = string.Empty;
in your control,if you want to keep the state of this variable,there are two ways,(generally for keeping the state of public properties),
1-in ASP.NET page by using session,viewstate,...(in consuming page)
2-in your control(i.e make a hidden webcontrol..)

_____________
Mehdi.
software student.
 
Old February 24th, 2005, 04:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks Mehdi,
I ended up using the ViewState.
I had trouble when I tried the hidden web control in my user control, but I'll look into it again as that would be prefered.

I've just finished doing another control to be used in a datagrid with using the event bubling method Stu mentioned above.

The code is a cleaner than using delegates as shown at http://aspnet.4guysfromrolla.com/articles/031704-1.aspx .
But there it really just looks like a shorthand way of doing the same thing.

Is there any technical difference to Event Bubbling and Delegates?


======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old February 24th, 2005, 06:58 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

in event bubbling
1-you give the user an event
2-you can send the index through your own eventargs(as a parameter for the event consumer,somewhat like 4guys)
3-you can send the index like method 2 I told by using a private label and a public property(not as a parameter)
in delegates(4guys)
1-you give the user a delegate
2-you send the index through a private label as a parameter for your consuming function,
Code:
_intCurrentIndex = (CInt(Me.LblIndex.Text) - 1)
....
aObj(0) = _intCurrentIndex
_delUpdateIndex.DynamicInvoke(aObj)
I think here a delegate could be sufficient(it depends on your requirement).

_____________
Mehdi.
software student.
 
Old February 24th, 2005, 08:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks again.
I have a lot to learn about this.

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================





Similar Threads
Thread Thread Starter Forum Replies Last Post
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
how can i access controls on parent page from user marvz ASP.NET 2.0 Basics 0 September 5th, 2005 11:18 PM
Select the correct version of Access 97/2000 setyna VB Databases Basics 2 August 16th, 2004 03:54 AM
User Controls Duncan Pro VB.NET 2002/2003 2 December 1st, 2003 05:41 AM
User Controls Duncan Pro VB.NET 2002/2003 1 October 27th, 2003 12:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.