We help IT Professionals succeed at work.

How can I call a control inmy page from a web user control?

eddyperu
eddyperu asked
on
Medium Priority
392 Views
Last Modified: 2012-05-06
Hi,
There is a button in my page:
<asp:ImageButton runat="server" ID="btnGetSessionStatus" onclick="btnGetSessionStatus_Click" ImageUrl="images/GetSession_Button_OFF.png" />

Also I have a web user Control: "MywebuserControl.ascx" in the same page
<uc1:Summary ID="EnrollmentSummaryControl" runat="server" ></uc1:Summary >
Inside this Web User Control I have a button like this:
 <asp:ImageButton runat="server" ID="set" ImageUrl="images/Close.png"
                                    onclick="Close_Click" />
In the codebihind of the web user control I have this:
        protected void Close_Click(object sender, ImageClickEventArgs e)
        {
         // I want to be able to call the "btnGetSessionStatus" here, and change some attributes like enabled="false" , how can I do this???
        }

Thanks
Comment
Watch Question

You'll probably be better off using javascript to enable/disable the buttons, since the controls on the page aren't declared as public controls.

Author

Commented:
What about function, I have this function in my maing page:
        public void GetEnrollButton()
        {
            btnConfirmBenefits.Enabled = true;
            btnConfirmBenefits.ImageUrl = "images/ConfirmBenefits_Button_ON.png";
        }

How can the web user control call this function with the click of the button? Would you mind to show me some examples with my code!
Thanks

Author

Commented:
I have tried:
        protected void Close_Click(object sender, ImageClickEventArgs e)
        {
            WebForm1 cd = new WebForm1();
            cd.GetEnrollButton();
           
        }
But there is an error that says:
- " Object reference  not set to an instance of an object"
You would need to setup a delegate on your page, you can see an example here.

http://aspnet.4guysfromrolla.com/articles/031704-1.aspx

Author

Commented:
Thanks, but I am new in C# and vb. the example is in vb.net. Would you mind to send me an example in c# or write an example using my code. I wan to study the procedure that you are taking to create a delegate and use it.
By the way. For what reason I will use a delegate here?
thank you so much
Actually, try this first.

protected void Close_Click(object sender, ImageClickEventArgs e)
        {
            WebForm1 cd = (WebForm1)me.Parent();
            cd.GetEnrollButton();          
        }

Author

Commented:
There is an error " The name 'me' doesn't exist in the current context
Oh, that should be this.Parent()

WebForm1 cd = (WebForm1)this.Parent();

Author

Commented:
another error:
" Non-Invocable memeber 'System.Web.UI.Control.Parent'  cannot be used like a method "

Author

Commented:
hi aibussnesssolution for all your help  but I sore it of found the answer, I think :

What I did to fix this problem is just create a dummy button to be attached to the modal pop up extender and then call the Modal pop up extender from an event of a button  " ModalPopup.show() "
Also, I created an asp button inside the modal pop up extender that will render to the server side  and will change the attributes of the button in the main page. Like you say trying to access the buttons from the web user control can be tricky specially because they are privates.
I still have an update panel that wrap all this:
New problem : a message appears"
  Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common cause for this error are when the response is modified by calls to Response.Write(),response filters, HttpModules, or server trace is enanbled.
Detailst: Error parsing near'
<!DOCTYPE  HTML PBU'."

Hae you seen this error before?

Author

Commented:
I ifc the proble iwth that error.
I have a Server.transfer inside the event . It looks like update panel and server transfer don't like each other somthing to do with the way how update panel is rendering the page.
Anyhow to solve this problem I switch it to Response.Redirect and  now it is working great!
Thanks aibusinesssolutions for your help!

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.