Friday, March 16, 2012

passing a value to another page in C#?

I have a master page with a label on it and i would like to set the text of that label from my other pages programatically. Basically I have tried to access the control directly, and also created a public method in the master page that takes in a string value and sets it to the control. However from say the default page, I cannot access that public method. How can I pass the value to the master page so that it sets the aspnet control text?

I have also tried making a public string variable, and while in the pageload having it set the text for the label. From the default page, in the pageload I tried to set the value, but I cannot. Any suggestions? Thanks.

Have you tried to add a Public Property in your MasterPages that returns the Label?

This way you can access the Label through any content page!

HTH,

regards


Hello,
If you are talking about the Master and Content pages, then i have a small information for you. You can work upon it and find out the details of it.

The Content page can access the Master Page through the Keyword "Master". So u can search the control from the master using Master.FindControl() and set its value.

Hope this works for you.

Az.
can you show me an example? I am not sure of what you are saying. Thanks.
what does that findcontrol method return?

If you have a label in your master page called Label1 and a textbox on your default page called Textbox1, then you could set the label with the contents of the textbox with this code:

protected void Button1_Click(object sender, EventArgs e) { ((Label)Master.FindControl("Label1")).Text = TextBox1.Text; }
 

The Master object is a property in the page object that stores the master page for the content page. FindControl returns a control from the page with an ID that matches the string passed as a parameter. The control that is returned from FindControl must be cast to Label to get the Text property.

This code allows you to set the value of the label in the master page, but that value will not persist to other content pages that use the same master page.


It is better to use a property, than the casting one!

Suppose you have changed the name of the Label in the master page, then you will have to go back to all your content pages and change the label name, but in the Master Page using a property, you could change the control name, and keep the property name the same!

HTH,

Regards


Well the main reason for not setting the label.text property in the master page is that it needs to be dynamic. The text of that label will be set on the page_load of the page using the master template. If i set it in the master template, I cannot change it dynamically from the aspx page. That findcontrol method casted worked great however. Thank you!

Please check my post again. I didn't say casting doesn't work!

I said watch out to a drawback when using Cast, which happens if you change the Control Name in the master Page!

HTH,

Regards


Hi ,

Can you show me - or point me to any ariticle(s) on, how to pass values from content pages to master pages after the properties are set in the master pages?

Example:

Say I have a text box id="txt1"

In the master page code behid we would have something like

Private _txt as string

Public Property thisText As String
Get
Return _txt
End Get
Set
_txt = value
End Set
End property

Me.txt1.text = thisText

I hope I am right so far...

The question is: How would I pass values for 'thisText' from different content pages.

Thanks.


samirayes:

*snipped*

I hope I am right so far...

The question is: How would I pass values for 'thisText' from different content pages.

Master.thisText = "foo"

0 comments:

Post a Comment