Share same information on multiple web pages - asp.net

How can I share the same ordered list across multiple web pages without writing code behind? In this manner the information would only have to be updated in one location.

You can build an ASP.NET user control (http://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx) so you can put your list there and use your user control on every page you need.
With this, you'll have to alter your ordered list only in the user control, and this update will be available everywhere you've used the user control.

Related

Is there a way to redirect or override an existing ASCX?

I am working on a ASP.NET site that uses several ascx files scattered throughout the web project, I want to add my ascx files but keep the existing code-behind file. I also want to enable the user to easily switch between the mine and the existing ones. Ideally, I want to enable the user to install the ascx files with minimal hassle without changing any code or going through some complex error-prone process. (The goal is to provide a change of the markup code)
ASP.NET provides ControlAdapters, a mechanism that allows a server control to be customized through adapters that map to a server control. I was wondering if such a mechanism exists for user controls, if not, are there any features or a work-around that provides the same results.
EDIT:
I discovered a "feature" that's not well documented that may allow me to do what I want. It's called tagMapping. It is supposed to work with server controls, but it's failed to work with UserControls.
You can add a panel to where you want to display these user controls. Then, load user controls dynamicly how you wish on backend.
UserControl ctrl = (UserControl)this.LoadControl("/UserControls/"
+ whichone + "/.ascx");
yourpanel.Controls.Add(ctrl);

How to transfer data to another page

Hai, I have an ASP.NET page with 150 controls and i want to transfer data of these controls to another ASP.NET page. what method would be best for this task? Number of controls may increase.
Thanks in advance
There are many ways:
Using a Query String (Might not work in your case, only good for transferring small amount of data)
Getting Post Information from the Source Page
Using Session State
Getting Public Property Values from the Source Page
Getting Control Information from the Source Page in the Same Application
Its always preferable to wrap the data you want to transfer in an object and pass it using pt. 3 or Pt. 4 , though in case you have arbitrary number of controls, Pt. 5 may work better for you.
This should cover it comprehensively:
MSDN: How to: Pass Values Between ASP.NET Web Pages
ASP.NET 2.0 : Accessing controls in Previous Page
You can use datatable , populate the contents in the row and send it using session
Another way is use generic class and transfer it using session.
You can also transfer it using below mentioned code
TextBox previouspagetextbox = (TextBox)PreviousPage.FindControl("currentpagetextbox");
the above mentioned code will be written in the another page where you will access the controls of previous page.
Multiviews is an another option. So you donot need to transfer the contents. It will facilitate you in same page.
As i could understand your need it will be possible through server side session or any other servers side storing mechanism like you can store the data in the database also and then fetch the control values on the next page by the Primary key or any other composite unique combination but at the cost of your page performance i will suggest you better to use ASP:Wizard control that is available from asp.net 2.0.
Most of the things will be taken care by the asp:wizard and it will be easy for the user of the page to fill up the information in the controls.
for details ion wizard control read on the following link
Hope it will be helpful.
Happy coding.
You can use Server.Transfer('NewPage.aspx', True) to redirect to a new page and that page will have access to all of the controls that were on the previous page.
MSDN Article about it

Dynamically creating many instances of ASP.NET usercontrols: How do I create it, and where will my conflicts be?

I haven't seen this implemented before in ASP.NET, but am thinking about a UI that would look and act like this:
Conceptual Overview
A TabControl is loaded and the first tab contains a grid
When a row is double-clicked, a new tab is created with the record detail
The content of the tab/record detail is created by a usercontrol
Many tabs could be created, and therefore many instances of the usercontrol will be created
I know ASP.NET will rename my (runat="server") ID's for me, and that I can use jQuery or ASP.NET server-side code to work with the ID's... My concerns are:
How can I ask ASP.NET to generate a unique ID for each Nth instance of my usercontrol (to be rendered in a placeholder)
How do I actually create that extra instance of the control?
What else do I need to keep in mind?
Since I don't want postbacks I'm considering basing my implementation off of ComponentArt's Callback Control, and using ASP.net usercontrols to achieve this effect. This will allow me to do most things that require a postback, but won't refresh all the elements on a page... just the section that contains the user control. That being said, I'm not tied to a particular implementation.
You should look into the Page.LoadControl method. It works nicely and as far as I remember you put placeholders on your page and load the controls into the PlaceHolders, that's how you control the ids.
One thing that doesn't work out so well with this approach is when your control raises events that your Page object has to handle. If your control is selfcontained however you shouldn't have a problem.
This might help you get started:
http://www.codeproject.com/KB/aspnet/LoadingUSerControl.aspx

Adding controls dynamically to the page from asp.net web method

I am using jquery ajax method to get data from a web method and present data using DOM(similar to that of google search results).B'coz the data returned from the web method is huge I want to paginate the results.For that I need to create buttons corresponding to the page numbers based on the no. of records the web method retrieves from the database.So I have taken a div on the page.In the web method ,as soon as I can find the number of records obtained from the database,I want to create the buttons and add to this div and display 10 records per page.As far as I know, it is not possible to access anything that is placed on the asp.net page from Web method.In that case how do I paginate the results?
Please help.
if you are using JQuery ajax, you can recreate the UI on the client by doing statements like:
$("<input/>").attr("type", "button").click(function() { .. }).appendTo("parentElementselector");
$.each(webmethodresults, function(i, item) {
//Create UI here using approach illustrated above
});
And programmatically recreate for each page.
EDIT Or find a third party table control that you can bind to on the client side. MS AJAX 4 has some client-side JS components to do this or there are some JQuery ones... but either way, if using JQuery to stream via AJAX, you have to create on the client.
Here is an alternative suggestion on how to handle this scenario:
When the page loads determine how many records you will be returning.
Divide this by how many records you want to show per page (10).
Add paging controls based on how many pages you will have at 10 records per page.
Only query the database for the 10 records you will be showing on a given page. If the data is extremely huge you will not want to load it all into memory anyways. This can be done with a method signature that accepts how many records per page and the the current page you are on.

How to persist ViewState for reuse later?

Is it possible to save ViewState information, e.g. to session, so that when you navigate away from the page it is persisted somehow? Then when you return to that page you can reload the view state and the choices you've made are preserved.
Background
I have two pages, a report page, where you can pick data, do some filtering and sorting etc. and a chart-page, where the data you picked from the report page can be presented in different ways, with different choices for presentation.
If the user has tested different presentations, simply using the back-button could mean quite a few clicks before the user's back at the report page. I'd like a direct link to the report page.
Using QueryString to save control states is not an option.
I can't customize the ViewState storage for the whole application.
Yes, it's possible to store the Viewstate in something like a database. You just need to implement one of the viewstate providers. See here for an example using the SqlViewStateProvider.
Edit: Just re-read your post, and saw that you said you couldn't customize how the viewstate is stored for the whole application. If that's the case, you might want to look into storing it in a session. Scott Hanselman discusses that here.
Your link could automatically navigate back the required number of pages using JavaScript. Look at window.history, if you can count the number of pages forward you can navigate back that many.
The ViewState is already designed to persist the state of the user controls. If your user has made a selection and that selection is processed server side with a full page postback the new state of the controls will be saved in the ViewState (hidden input __VIEWSTATE).
If your report is using AJAX and partial page postbacks then you won't get the ViewState on the page anyway.
Just to clarify, the SQLViewstateProvider is NOT an application wide implementation. You have to create a class which inherits from the System.Web.UI.Page object and overrides the Save And Load viewstate methods of the Parent Page class. For each page that you want the viewstate to be saved on server side you then have to inherit from your newly created Page Template (Which in turn inherits and overrides the System.WEb.UI.Page class).
So it is applied on a per-page basis, not on an application-wide basis.
HEADS UP: Some controls might contain some client-side javascript code which may reference the viewstate on client-side (duh). If the viewstate is now stored on server-side you will get a null-reference exception (for instance, clicking a commandfield in a gridview). I'm working on a workaround for this problem, but unfortunately I do not have any concrete solution as of yet.
This is a bad idea, just use querystrings. I would be interested to know why they are not an option.

Resources