public variables


J

James Page

Hi all (again!)

Declaring publically available variables is something I use on a daily basis
but here's somthing I just can't figure out:

I have an aspx page with a details view and within that say a button (btn1)

if I wanted to change the visible value to false from within the btn1_click
event I would use:

dim btn as button = me.detailsview1.findcontrol("btn1")

btn1.visible = false

Simple.
What I never been able to work out is how I reference the button 'publically'
so I could set the various properties from within other events - such as the
page load, btn2_click event etc.

Also is possible to reference the btn1 control from within a public class?

Thanks
 
Ad

Advertisements

G

Gregory A. Beamer

Hi all (again!)

Declaring publically available variables is something I use on a daily
basis but here's somthing I just can't figure out:

I have an aspx page with a details view and within that say a button
(btn1)

if I wanted to change the visible value to false from within the
btn1_click event I would use:

dim btn as button = me.detailsview1.findcontrol("btn1")

btn1.visible = false

Simple.
What I never been able to work out is how I reference the button
'publically' so I could set the various properties from within other
events - such as the page load, btn2_click event etc.

Also is possible to reference the btn1 control from within a public
class?

You are going about this the hard way. When you pull a button control on
the page, and set it as runat="server" (ASP.NET controls have to have
this, period, HTML controls do not), you can access in code directly.

Example of both types of controls:

<!-- ASP.NET button control -->
<asp:Button ID="AspNetButton" runat="server" Text="Button"
OnClick="AspNetButton_Click" />

<!-- HTML button control -->
<input id="Button2" type="button" value="button" runat="server" />

NOTE: If you want an ASP.NET postback for the HTML button, you have to
code it. It will simply submit back to the form otherwise, and you will
have to branch Page_Load (I have blogged Page_Load is for loading pages
before, so this is a BAAAAD thing, IMO).

Since the ASP.NET button is simpler, here is how you can hide it when it
is clicked.

protected void AspNetButton_Click(object sender, EventArgs e)
{
AspNetButton.Visible = false;
}

If you go this route, Intellisense will show you all of the properties
you can set on the button control.

Peace and Grace,
 
J

James Page

Thanks Gregory

The example you gave is what I would use if all i wanted to do was hide the
button when the button was clicked
..
What I'm asking is slightly different.

The page I'm working on has various controls - butttons, fileupload labels
etc.

When a particular button is clicked or other event is fired I have to
hide/unhide or otherwise manipulate control settings on a number of controls
on the page. In order to do this i have to dim the the controls I want to
manipulate in that particular event.

I have to repeat this dim in each control event in order to manipulate
various settings.
Its a lot of typing!

What i'm looking for is to dim these controls outside of all events
something like:

dim lblCtl as label= me.detailsview1.findcontrol("lbl1")

and simply reference them from within a given event like:

btn1_click event
lblCtl.text = "some text"

or

btn2_click event
lblCtl.text = "some other text"

All I'm trying to do is to reference the various controls once and
manipulate them from within various events. Also it would be very handy if
this 'external' refrencing could be acheived from an external class (i
suspect it can't be done!)

Hope this is a little clearer :)
 
G

Gregory A. Beamer

Thanks Gregory

The example you gave is what I would use if all i wanted to do was
hide the button when the button was clicked
.
What I'm asking is slightly different.

The page I'm working on has various controls - butttons, fileupload
labels etc.

WARNING: Much of this post is brainstorming.

My thought is to group the controls that will hide like so:

protected void ToggleControls()
{
Control1.Visible = !Control1.Visible;
Control2.Visible = !Control2.Visible;
//etc.
}

Or group them in a Panel object and hide the entire panel.

This does not work all the time, of course.

Another potential way of doing this (more complex) would be to create
your own "hiding" controls by deriving from the controls you want to
hide. You use an attribute on the new class that shows it can hide, for
example and then you can check that attribute. NOTE that I have not
really thought this architecture through, so some R&D is necessary to
ensure it is sane. You also have to set up the attribute so the controls
can have a "group" to belong to, so you can hide different sets based on
user input.

You can loop through controls, but you have to have some way of easily
identifying the ones that hide (either metadata stored somewhere or
something similar).

For metadata, you can create a custom config that has the names of the
controls that hide. You pull the list and then use the type of discovery
you were using in the original post, but you are searching for a list
you have pulled out of some type of config file (note that this is not
necessarily web.config, it can be truly custom). This method is very
flexible.

For the I need to do X and Y and Z to these controls and only X and Y to
these, you will likely have to set up the rules, either in code or some
form of config, and then generically rip through the controls and find
those that have certain settings.

One other approach, to avoid a lot of typing, is create a simple code
generator. The sensibility of this approach depends on how often you are
going to use this pattern. On a single page, it is probably wasted time.

These are some ideas off the top of my head. I will have to think about
it more to see if there are other ideas that come to mind.
 
J

James Page

Thanks Gregory

Is there no way to publically reference a particular control so that its
properties can be set from anywhere, on any event?

For instance if I create a class and create a public shared variable, say:

Public class myclass

public shared mynumber as integer = 0

end class

when ever I need to set this variable (or refer to it)
All I have to do in an aspx code behind page is - for example:

imports myclass

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
mynumber = 1
End Sub


I can then set or use this variable from anywhere within the codebehind.

I suppose what I am trying to acheive is to use a similar process but with a
publically declared control.

Hope this makes sense

Thanks
 
Ad

Advertisements

G

Gregory A. Beamer

Thanks Gregory

Is there no way to publically reference a particular control so that
its properties can be set from anywhere, on any event?

For instance if I create a class and create a public shared variable,
say:

Public class myclass

public shared mynumber as integer = 0

end class

when ever I need to set this variable (or refer to it)
All I have to do in an aspx code behind page is - for example:

imports myclass

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
mynumber = 1
End Sub


I can then set or use this variable from anywhere within the
codebehind.

I suppose what I am trying to acheive is to use a similar process but
with a publically declared control.

Here is what is happening, to understand why this is so complex compared
to windows forms and libraries, etc.

1. User changes drop down
2. User's browser has a javascript routine (emitted by your page) that
sets up a postback
3. Request is created as a POST with name of handler in it
4. IIS sees an ASPX request
5. IIS redirects to the ASP.NET engine (worker process)
6. The worker process finds the page to run (which could be compiled in
the bin or compiled on the fly, etc.)
7. The worker process process the page through a set of events
8. The response is sent back to the user

The issue here is most of the work is being done through event handlers
on the page class, so handling setting of multiple properties through a
single class would mean one of two things:

1. The page sends itself to a secondary class. Not impossible, but too
much chance of messing things up as the changer and the changee are the
same object.
2. The asp.net process is altered.

The second option can be done through Http modules, etc., but depending
on how deep you go, you may end up having to rewrite more of the ASP.NET
engine than you would like. This adds a lot of complexity to your
application simply to avoid some typing.

One potential option to achieve what you desire is to put all of the
form elements into a user class and expose the control as a Property
there. The issue, or perhaps potential issue, would come when you pass
the user control to the library that sets things on and off. I see two
choices (brainstorming again):

1. Control passed by value to ensure items set in it are set on the
page. This has overhead, but may have a higher degree of success.

2. Control rebound to the page. Not sure how difficult this would be.

There is one other possible choice and that is to completely build the
page on the fly. The issue with this route is there is a lot of room to
really screw up your page.

Oh, and one other option. Change to the MVC paradigm, where writing out
the entire page is not as dangerous. ;-)
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top