Loading several DropDowns on page load faster - c#

I have several DropDown controls on a Web Form which are filled using AJAX call to a method in a class.
I call methods as below:
FillDistricts();
FillVillages();
FillTowns();
I thought these independent calls are creating too many round trips to the server and therefore I altered the class to fill all the DropDowns at once.
I pass the names of all the DropDowns as an array to a class which has a method to populate those DropDowns in a loop.
I want to know whether it will make some difference in page loads.

Use Page.Cache to store the results in, if that cache doesnt exists load the items, and save them into the cache, and may the same results be queried again, check to see if its already in the cache if so load it from the cache

Related

Controller calling itself and passing a value

How to pass data from the controller to itself?
Consider this example:
I have a page that consists of two parts: (1) a simple html form with a couple of text boxes and a submit button and (2) a table that is updated when the button from part (1) is pressed. When it happens, the data from the form has to be appended to the end of the table.
As I see it, there should exist a List of objects. Every time the button is pressed, the controller is called with two parameters: the old list of objects and textbox values. Then, the controller generates the object, adds it to the list and passes the new list to the view. The view is rendered with the new data and rows are successfully added to the table.
However, that requires reloading of the page and that feels kinda wrong.
The problem is, that there is no static object that can contain the list permanently, or at least that exists during these controller-self-calls. If there was such, I would not have to pass the whole list (which, as I said, I can't even do) but just new textbox values.
I have heard that partial views can solve the problem, but I can't see how.
What can I do?
For starters, as you said you'd like to achieve this without javascript, I see no way of avoiding: reloading of the page and that feels kinda wrong.
Not sure as well how partial views will make things work since they're rendered from your main view and require the same or part of your model, so you'll need to have that data there.
You have to get the information back from the controller, and the controller must get this information somehow so as I see it, these are your options:
Keep part (2) inside your form, thus making both parts available when you hit the controller. Model will get populated with the values you need and then the data is available for you when you're back at the View.
Keep a hidden input field inside part (1) containing the data you require to create the list. It's similar in concept to option #1 but I don't like this method too much, you'll have to do some parsing on that input field and this is not very elegant.
You could also try use Session or database but I think the latter is an overkill and a hit on performance so I wouldn't go with that.
If you don't have a database backing the form data, you could use Session data to hold the List.
In the controller, do something like this:
[HttpPost]
public ActionResult AddToList(object newObject)
{
var list = Sesssion["List"] as List<Object>;
if (list == null) {
list = new List<Object>();
Session["List"] = list;
}
list.add(newObject);
return View(list); // Assuming the view is a strong-typed view with List<Object> as model
}
As for partial views, they do not alone solve the problem of reloading the page. The solve the problem of a reusable, self-containing component of the page. If you don't like the reload of the page, you can use partial views together with Ajax calls though, in order to refetch the table whenever a new item is added. Here is an example

c# ammend SQL data

I am making a basic ticketing system in C# with basic coding experience. Most of my experience is in SQL.
I have the database and tables. I have stored procedures to create and amend tickets.
I am stuck (this is probably very basic) because:
On my EDIT ticket page, I populate various text boxes and drop downs from my existing data via inline SQL.
On my page, I can edit all the fields and dropdowns with new values. (i.e. change a ticket priority from when it was first logged)
I have a button, that calls my "update" stored procedure, however it updates only the NEW fields I have, any amendments to the existing fields are overwritten by the original values before submitting.
The original values are called on pageload, so I think the button reloads the page before submitting. I want it to submit all the values that are on the screen.
I think this must be something obvious, remember I am a novice so I may have missed something simple.
If what you're saying is you load values from the DB into the form controls in the PageLoad event handler, then yes, you're probably overwriting the changed values. To keep things as simple as possible for you, wrap the original values loading code in the following:
if( !IsPostBack )
{
// load initial form values here from DB
}
I'd suggest you read about the ASP.NET page lifecycle: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.90).aspx

Persist c# objects across postbacks

I've got an asp.net page that has c# code-behind that does some stuff in the Page_Load() method (like query a database and make a few other calls to populate objects with data). I then display this data on the page. This all works fine. I set up a couple of postbacks so that when a value in a listbox is clicked, a panel control is filled with the rest of the corresponding object's data. I thought postbacks were the right way to do this, but this causes the (entire class?) to be re-called, which re-initializes my objects and destroys the data I want to keep.
Will some form of partial-postback solve this problem, or is there a better way to implement what I'm trying to do?
I don't want to re-populate the objects every time a postback is called, as that takes a database query, and I want to avoid re-querying every time something is clicked...
I've found many questions regarding persisting Javascript objects, but nothing that really seems to address this. I'm using .Net 4.0
Put the objects into the Session for the current user.
Put all your initialization stuff in an (!IsPostback) { } and use partial postbacks. That way the initialization code doesn't get called again during the postbacks.
Why don't you cache the object?
Caching API, Using the Cache Object:
http://msdn.microsoft.com/en-us/library/aa478965.aspx#aspnet-cachingtechniquesbestpract_topic4

Form reloading in C#.net

I have a form i am fetching many records from database that are rendering dynamical on from. When i am clicking on Delete button this is deleting as well but I want to re-render my form so user will feel that records is deleted completely. when i am writing code this.refresh(); this is not fetching values from database and i am not seeing record is completely gone. It is windows C# form.
The methods Form.Refresh and Form.Invalidate have nothing to do with causing the application to retrieve new data from the database.
Form.Refresh - redraws the form synchronously (i.e. Form.OnPaintBackground and Form.OnPaint are called directly on the current thread).
Form.Invalidate - redraws the form asynchronously (i.e. WM_PAINT message is sent to the window, so Form.OnPaintBackground and Form.OnPaint will be called by the UI thread when it handles messages).
In order to update records, you need to make your control retrieve updated data.
If you specify more details on how the Form pulls that data, someone can help you figure out how to update it. For example, are you using data binding? Did you write your own code to retrieve the records?
Basically, without seeing your code, I would say you can take whatever it is you are doing in the constructor and put it in a separate method (let's call it RefreshData). Then, call RefreshData after you delete records from the database.
So, you're creating new controls that represent the data. In that case, there's no mechanism to automatically do the updating work for you, you'll have to code it. You generally have two options:
Keep a reference to the controls you created, and remove them from the Form before updating it.
Implementing something more efficient, like maintain a Dictionary that maps from the Data Rows to the controls that represent it, so that you can go over the updated data, and for each row in your dictionary that disappeared from the data, remove the controls.
You may also want to consider using some control that has a built-in Data Binding mechanism that would save you lots of work. For example, a GridView.
Could you be looking for Invalidate() ?
If you are filling the form in constructor you need to extract loading code to a method.
Then call it on DeleteButton.OnClick event after performing delete.

access values of controls dynamically created on postback

My problem is:
I've got a table, dynamically created, fill with a lot of dropdownlists witches IDs are dynamically created.
When a button is pressed, I need to scan all controls in the table and save their value.
But after the postback I can't no longer access to the table, and I've no idea how can I get those values...
Thanks!
Controls created dynamically must be created again on every postback on the event Init or PreInit (before ViewState is loaded) otherwise you won't be able to retrieve their values.
Some reference links
http://msdn.microsoft.com/en-us/library/ms972976.aspx#viewstate_topic4
https://web.archive.org/web/20210707024005/http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
https://web.archive.org/web/20210707024009/http://aspnet.4guysfromrolla.com/articles/082102-1.aspx
If the form was posted, shouldn't they be in the Request.Forms collection.
Let's say you named them all starting with dct.
Then you could loop through the collection and taking what values you need.
You could access the values with Request.Form("dct_001") etc...
Since the lookup is string based you could put it in a loop to catch the value.
BTW this classic ASP approach still works in 4.0

Categories

Resources