Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Open Points: 75 Time: 03:58 - May 31, 2007  

manohar.patil

How can I call any method which is written in WebForm1.aspx.cs in WebUserControl1.ascx.cs?

Question faced during development, so please reply fast.

Categories

Answer Discussion
Tutorials

 

PeterNZ

Date:: May 31, 2007

Time:: 17:29

I hope that's fast enough!!! ;-)

First, let me tell you, that what you want to do is not good design. You tie the user control to the parent and are dependent that there is a method or property on the parent! This creates dependencies which should be avoided. It would be the same as if a Button control is using a property on the form. You would only be able to add Button controls to forms which have this property!

Anyway, here is a solution:

I have a form which has a textbox named Textbox1. I create a Web User Control, which has a button and a label. If you click the button, the web user control gets the text of textbox1 from the parent control and sets the label's text to this text.

The code is:
protected void Button1_Click(object sender, EventArgs e)
{
TextBox tb = (TextBox) this.Parent.FindControl("TextBox1");
  if(tb != null)
  {
Label1.Text = tb.Text;
  }
  else
  {
  Label1.Text = "TextBox1 doesn't exist in parent container!"
  }
}

This only lets you access a public property on the parent form!!!

The way you should do this (and a good design!) is, you should add an event to your web user control and handle the event on the parent form. Lets say you want to update an address after a user entered his/her address details in your web user control. The method which updates the database is on the parent form. You fire an event i.e. AddressUpdated Event from the web user form and handle this event on the parent page. The eventhandler initiates the update on the database. If you need to read a return value from this update, you should put it in a public property of the parent page and read this property in the web user control.

Does this make sense?

Cheers

Peter

admin

Date:: Aug 22, 2007

Time:: 20:22

manohar.patil, did you get your question solved?

If you did then please close this question and distribute the points. If you found the solution on your own, we would be very happy if you could explain it here for the sake other users having the same problem.

If you didn't get a solution to your problem, please leave a comment here to let the experts know that you're still looking for an answer.

Thanks,
The Quomon Admin Team

admin

Date:: Mar 04, 2009

Time:: 11:44

The question looks to be abandoned by the user who asked it. If no action is taken within 2 days, a Quomon Moderator will consider closing the question and distributing the points.

The Quomon Team

huddiestbeast

Date:: Nov 12, 2009

Time:: 00:57

declare the method in the form aspx.cs file as public ,then inherit the class to the ascx.cs form and then call the method

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Tutorials cannot be submitted until the Answer Discussion is complete.

Submit answers in the Answer Discussion area

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10354

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es