how to use functionality from existing aspx page in usercontrol? - asp.net

I am trying to use existing functionality which is embedded in a asp.net aspx page. So I have controls on this page and code behind for it. What is the best way to reuse this functionality in a new ascx control? Or do I just have to copy everyting into the control?

Can you not just take the controls you want to use from the aspx page and turn those into a control? Then you can use that control wherever you like.

To access the page parent just written this.Page in your control. But for access to these methods, you must apply an interface to the aspx page.

Related

.aspx works but .ascx does not... why and how to fix?

I want to put a ASTreeView web control in a custom web control, ASTreeView sample code is like:
<ct:ASTreeView ID="astvMyTree"
runat="server"
...
LoadNodesProvider="~/ASTreeViewDemo5.aspx"
.../>
LoadNodesProvider is the page ajax called when loading a node...however if I changed the provider to my .ascx file, it does not work:
LoadNodesProvider="~/ASTreeViewDemo5.ascx"
it did not even go through the Page_Load part of the .ascx file
Though this might be related with astreeview itself, I'm wondering what the problem could be? anything I can do to fix it?
Thanks!
It is because ascx must have a container ie Page. You can't use it same way as Page.
ASPX is a page and ASCX is a usercontrol. You cannot ajax call a control, so you probably want it to be a page with the control on it.
While it isnt entirely clear to me what LoadNodesProvider is supposed to do, if you want to encapsulate some code or run a process via AJAX you have a couple of options. One would be to create a web service (you could use WCF for this) that the AJAX method could call. Another option would be to create an http handler (ASHX extension typically denotes this). Using an ASPX or ASCX for this doesn't make a whole lot of sense to me. Proco and Tomas are correct regarding the ASCX file, these are Usercontrols and are not stand-alone objects.
If you really, really want to use an ASPX page/ASCX control, then I suppose it would be best to create a blank ASPX page that has one placeholder, and then attach your user control (based on query string parameters or something I guess) to the placeholder to render out the content for your AJAX control

Where to put Datagridview be Place in ASP.NET Webforms?

I am studying ASP.net webforms and i am confused where to placed Datagridview and other Controls
from webpage.aspx or in a Control.ascx .
//Which Validation and CRUD operations was performed
Which is the Best Practice.
Thanks in Regards
ascx
- fragment
- re-usable you can place it anytime in your webpages(aspx)
- Need to attached to aspx to make it works
aspx - the actual page it self which you call on your browser which your ascx are placed
have a look at this same post:
Post 1
post 2
Building User Controls
Regards
You can put the thing in both but its depends on your requirement
aspx - is asp.net page so when you put control on it , it get utilize with in that page only i.e not resuable.
ascx - is user control , i.e usable control with in your application . once you design it you can use it in any page of by registering it on page.
so go for ascx when its resuable and going to use in no pages. go for aspx when its page specific.
If you want your DataGridView (maybe including some search form, button) appear on every page. Then use ascx. Else aspx. Ascx is a page fragment, not page!

Custom server control, with form and scriptmanager

I am building a set of asp.net server controls and in one of them, essentially a container control, I want to add a form control, a script manager and an update panel. Is this possible or will I have to create these in a Page base class that I have for the web project for page/control life cycle reasons?
I am not using master pages.
Regards,
Andrew
ASP.NET allows only one form on the page. When you create controls, they are used on a page. You need a Page object to add controls to. This is why controls like the form and ScriptManager (who can only have one instance on a page) are put on the Page itself, or on the masterpage (if you have one). Putting them in a control would provide the opportunity to have two instances on the page, which would not work.

Render a ASP.NET control in the master page code-behind

I'm using a Substitution control in my master page, and I want to render a user control content (related to the login area of my website) in the Substitution .
Seems like I must have a reference for the requested page so that it could render the control. But I need to render the control in the master page itself, as it's shared across multiple pages in my website. What are the guidelines to achieve that?
Tks
So you want to render a user control from your MasterPage code-behind, and add it to a Substitution that's also in the master page? Why do you need a reference to the page that's using the master?
Assuming your using VB and that I understand your question, try this in your MasterPage code-behind:
Dim someControl As MyControl = CType((New Page()).LoadControl("~/Path/To/MyControl.ascx"), MyControl)
mySubstitution.Controls.Add(someControl)

Possible to load ASCX with jQuery.load() function?

Is it possible without encapsulating these controls in aspx files? The Register tag makes me think that it is not.
You can't call ascx page directly, they can't serve any html without added to aspx or render then dynamically in response to any page/handler.
One possible way would be to have a generic aspx stub page that acts as a host to the ascx in the normal web forms way make a callback to the that page using JQuery's $.ajax() method with some arguments to denote what ascx control you want to load.
Then on your stub / host page, override the render method and render the control directly to the output stream using response.output.write. The callback handler on the client will catch the output and then it can be inserted into the DOM in the normal way or using using the version of $.load() that allows the specification of a DOM element.
hope this helps.
This is one way to load ASCX controls through jQuery, using an ASMX service that creates a Page instance to render the user control.
Since I've started using jQuery/Ajax more and more, I find I've been using server controls less and less.
Things that I used to put into controls are now just individual aspx pages which get loaded into panels.
If you have old ASCX files, it shouldn't be too hard to convert them to ASPX
No.
That said, you could place your ascx inside an UpdatePanel and set the panel's content visible/invisible using the standard Asp.Net postback mechanisms.
Or you could write a separate IHttpHandler which generates HTML code that your Javascript code adds to the page using DOM. This part of the page would however not be accessible in PostBack.
No, ASCX are user-controls, that exist on ASPX. You will need to make it a Page, or convert into Handler.

Resources