Friday, March 16, 2012

Parameters between pages and control

Hi, all

I have few web pages and a control included(shared by each of page), they have a code-behind file individually. How can I use parameters between contol and page?

Details:
In each of web page, different colors will be mainly displayed, also I want to keep the same color in the control in that page. That means that the control will has a parameter for color which will be decided by the web page. When the web page loaded, the color parameter will be set up, and would be inherited by control.

I tried to set up a public parameter in page_load event inside the page's codebehind, but I got the error said that 'Public' is not valid on a local variable declaration as there is no this parameter inside the page but inside the control.

Thanks in advance.One way to accomplish this would be to expose a color property from your user control that you set during page_load from your page(s).

The other way is to use the Page property of you user control to get a reference to the current page, and use it to access the appropriate property or public variable on your page.

I prefer the first option since it is cleaner - the code in your user control is encapsulated from it's container. This means that your control will not be dependant on the page it is on.

If you need help with the code, please let me know what language your using.
Yes, NetProfit , thanks a lot.

Could u show me the code? I am using vb.
I could use this code if someone would post as was requested by the original poster.

Thank you.

Wow, looks like I lost track of this post!

Please have a look through the following and then give it a try. If you still run into problems after doing this, please let me know.

Walkthrough: Authoring a User Control with Visual Basic .NET
Manipulating User Control Properties
Understanding User Controls - Part 1
view post 708445

Thank you for your reply. However, I'm having a brain lock and I'm doing something wrong. Base on the post, 708445, you mentioned I cannot get this simple test to work. Here is my control and page code.


Public Class ksfTest
Inherits System.Web.UI.UserControl

Private m_showImage As Boolean = True

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

imgLogo.Visible = m_showImage ' Show/Hide image
Label1.Text = ShowImage & " - " & m_showImage 'Test to show values

End Sub

Public Property ShowImage() As Boolean
Get
Return m_showImage
End Get

Set(ByVal Value As Boolean)
m_showImage = Value
End Set

End Property

End Class

=== Page Code ======================

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

Dim ctlKSF As New ksfTest
ctlKSF.ShowImage = CheckBox1.Checked

End Sub

The checkbox is set to auto-postback. The image visible property is true.
When I check the box the image does not hide. I even put a lablel on my page to get and display the ShowImage property value and it does show it to be changing.

Thank you for your help.

This is because the OnLoad event for your page fires before the OnCheckChanged event for your CheckBox control. Move the code that sets the visible property for your image to a private procedure in your user control, and call that method from your CheckBox1_CheckChanged event handler. You may wish to call it from your Page_Load event handler also, in the case where Page.IsPostBack = False.

Let me know if this helps.

Thank you for your help so far. However I don't know what I'm missing. When I use a method to set the visible property I get the error "Object reference not set to an instance of an object". I am calling the method from the CheckBox1_CheckChanged event. The label1 is getting a null reference, object reference not set.

I see what's happening now. I didn't realize before that your checkbox belongs to your page, and not your user control. If you've added this new method in your page, it will not have access to Label1 directly.

Here is what I would do. Forget what I told you last time. Instead, put the code in your property Set statement for theShowImage property to set the visible property of your image control (and set the label if need be). When your page sets the ShowImage property of your user control, the code inside the property statement will do the rest.

I hope this works for you. Please let me know if it doesn't.

0 comments:

Post a Comment