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

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.

Related

Loading UserControl via AJAX, and posting back to the server

Summary
Is it possible to dynamically download the contents of a UserControl via AJAX, insert that into the HTML, post-back the page to the server and for the server to be able to process the dynamically loaded controls?
In particular I'm looking at doing this multiple times, to create the ability to "add rows" to an existing table structure in the page.
More Detail
I have already written ASP.NET code that successfully downloads the contents of a UserControl via AJAX using the <asp:ScriptManager>, and for that new downloaded HTML to be placed into the current document in the correct position.
This is fine, because all saving of data takes also takes place via AJAX, so it's simple for me to build the data to return to the server.
Part of the problem is that if the UserControl had been loaded as part of the original page, the controls within it would have been given UniqueId along the lines of...
ctl100$container$repeater$ctl100$myTextBox
But when loaded outside of the structure via the AJAX route, it is given a UniqueId along the lines of...
ctl100$myTextBox
... with the result that (obviously) the server doesn't know what to do with it.
So what I'm looking to do is be able to download dynamically a usercontrol, and be able to post that back as part of the page.
What is the standard approach to achieving this?
The standard ASP.NET Web Forms approach is using of UpdatePanel control. There are a lot of functionality like controls state maintaining, events handling, request validation that requires that server controls must be rendered and added to page's control's tree on postback.

How to review all datas in one pages in asp.net?

I have five pages in asp.net web application. I entered information in first page and click next. it goes to next page and enter information in this page. I continue this process for five pages. Before click Submit button in fifth page, I need to review once all information entered by me in all previous pages under review button click. How do this? pls Advice.
You can store your data in the database, as you move along the steps, and retrieve it in the final step. You can also store it in Session, as an alternative.
Surely there are other ways to implement this scenario, but the idea is to somehow persist the data and retrieve it in the final step.
I personally think that you are going the wrong way about this. Passing data from page to page is going to cause you all sorts of trouble.
There are a number of methods to achieve this on a single page. You could create 5 seperate controls and place them all on the single page (if they aren't all too big and slow down the page load), then make the appropriate control visible depending on which the user is to see. For example, when you click next make the current control visible property false and then display the next one. Then at the end of the process when you click complete you can access the entered data from each of the controls, you could create a fancy function in the controls that return you a populated class or even pass by reference (we won't go in to that here).
You could also look in to using Views. Which is another method that you could implement on a singular page.
If you're adamant with using multiple pages then you could temporarily store the data in a database and then on the final page get the IDs from a Session and store it all finally as one. But I wouldn't advise this, although it depends on the situation. If you want them to be able to fill half the form and then continue at a later date then the database method would be advisable, but if you only want data being stored upon completion then you will end up with a lot of half completed redundant data using this method.
The solution totally depends on the framework and methods you are using. I'll try and help you with an ASP.Net MVC3 solution, as I think MVC3 is really the way to go for when using ASP.Net.
When the user clicks on the 'next' button, just load a partial view (containing the next step html) with the JavaScript / jQuery Ajax method, OR just present the next step by using using the jQuery show / hide functions.
Keep your form data within one html form-tag (or at least one holder-div).
When you need to submit the information at the last step, simply collect the information with a single jQuery serialize call and send it to your ASP.Net receiver.
Some code snippets:
Display a partial view from the Razor view-engine:
#Html.Partial("/Views/Instance/_General.cshtml", Model)
Load a partial view through jQuery - for more details, please see http://api.jquery.com/category/ajax/
#Html.Partial("/Views/Instance/_General.cshtml", Model)
Post your data to the server though jQuery:
$("#submit-button-id").click(function () {
var data = $("#form-id").serialize();
$.ajax({
url: "/Example/Save",
type: "POST",
data: data,
cache: false,
success: function (success) {
if (success) {
// things worked out just fine..
}
else {
// present some kind of error..
}
}
});
});

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

Paging by using dynamically created html table

I am coding online sales for a website. I have a problem on my main page where I would like to show products. I defined a table with 4 columns, but there are over than a 100 products and I need paging for that.
How can I implement paging with 20 products per page?
There are a lot of finished controls that will help you on the way, check this MSDN Article out: Creating a Pager Control for ASP.NET
You could also check out one of the many jQuery Pagers out there, however if you use the jQuery solution all the rows will always be loaded. If you use some pre-built ASP.NET control for it, it will only fetch the number of elements you desire for each page.
If you are dynamically creating an HTML table (and not using any of the built-in controls provided by ASP.net for presenting tabular data), then do the following:
Add TextBox and Button controls to allow the user to input and control the page number to show
Add logic to display the current page number and total pages, items, etc
Change your data retrieval to use the current page number and total number of items per page to only display the items that you want. Ideally you would also only retrieve the items that you need (rather than retrieving the entire dataset and then only showing some of it). There are different ways to do this based on how you are retrieving your data.
Also check out one of the following articles:
Custom Paging in ASP.net 2.0 with Sql Server 2005 - Scott Mitchell
Efficiently Paging through Large Amounts of Data - MSDN, C#
Creating a Pager Control for ASP.NET - Dino Esposito, MSDN

ajax search that filters returned records

hi i want to create an search that uses ajax.
here are the requirements
1)on initial page load return all search data
2)have textbox that when typed in filters out the none matching records
any good tutorials out there on this
forgot to mention i'm using the telerik controls, using radgrid to display the results.
Not the answer you are looking for but if you are going to return all the search data on the initial page load there should be no reason for another trip to the server to filter the results. I would suggest that you use javascript to filter which results are show to the user. Jquery might make this easier. http://api.jquery.com/filter/

Resources