A simple syntax question

G

Greg Smith

I am sure this is very simple but I can't seem to get the syntax correct.

I have a class that I added to my application and I would like to reference
controls on a webform. Something like:

WebForm1.TextBox.Text = "Fred";



Any help is greatly appreciated.
 
C

Curt_C [MVP]

is "TextBox" a server-side control? if not you can't.

<asp:TextBox id="text1" runat="server"></asp:TextBox>

text1.Text = "this is a test";
 
M

Michael Ramey

Unless this is your CodeBehind class you are talking about, I think you are
trying to break all rules of object orientated programming. :) Better
solution, create a function in this class, that takes in values from your
codebehind class, and returns what you want, then call this function from
your codebehind to set the value.
 
M

Matt Howeson

Assume you mean from your codebehind file.

Make sure you have the TextBox declared in your codebehind file....

Assuming your control is called txtBox1

protected System.Web.UI.WebControls.TextBox txtBox1;

then from within your Page_Load event you would have :

txtBox1.Text = "Fred";

You shouldn't reference the form from the server side code (assume that is
what you are trying to do with WebForm1 below).

Matt
http://www.3internet.com
 
S

Steven Cheng[MSFT]

Hi Mark,


Thanks for posting in the community!
From your description, you'd like to get some suggestions on how to refer a
certain control(contained in a web page)'s value from a class module,yes?
If there is anything I misunderstood, please feel free to let me know.

As for this problem, here are my suggestions:
Generally, in a ASP.NET web page's code-behind class file, if you want to
get a webcontrol(such as textbox) 's value, you could use the code as:
this.TextBox1.Text = ....

However, if we want to retreive a certain page's sub control 's value in a
certain class module, we could use the following two means:
1. define a property for the page class which delegate the certain
webcontrol member, then in the class module, you can get the page's control
's value via the public property. For example:

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtA;

public string TextA
{
get{return txtA.Text;}
set{txtA.Text = value;}
}

.............
}

Then we can define a method in the class module to get the property's value
from a certain page, just like:
public static void GetPageTextBoxValue(Page page, string property)
{
string value = (string)DataBinder.Eval(page,property);
page.Response.Write("<br>"+property +": " + value);
}

2. If you'd like to directly access the certain control's value via its
control's Id, you can pass the certain page class's instance into the class
module's method and do any operations on it. For example:
Still use the
public class WebForm1 : System.Web.UI.Page as above one,

In class module , we define the method like:
public static void GetPageTextBoxValue(WebForm1 page)
{
string value = page.txtA.Text;
page.Response.Write("<br>txtA.Text: " + value);
}

Thus, it'll looks much simpler.

Please check out my suggestions. If you feel anything unclear, please feel
free to let me know.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Mark,


Have you had a chance to try out my suggestions or have you got any further
ideas on this issue? If you have any quesions, please feel free to let me
know.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top