Saving a selected value in gridview to viewstate - asp.net

I am trying to capture and save the selected value in a gridview to a variable in viewstate which i would like to use to pass as a query string parameter to a differant page
Where would i save the ViewState Variable and how?
Iam very new and not usre whether i have provided enough information
Thanks

Values stored in ViewState work differently than session. Unlike Session, a value stored in ViewState of one page cannot be retrieved from another. Actually ViewState is nothing but a system managed hidden encrypted field in the markup produced. How do you save a value in ViewState? like this:
ViewState["MyValue"]=GrideView1.SelectedValue.ToString();
Later you can retrieve that value like this
if(ViewState["MyValue"]!=null)
{
Response.Redirect("SecondPage.aspx?param="+ViewState["MyValue"]);
}

Related

How to save textbox value in the database

How to save textbox value in the database
(Textbox value populated based on other textbox input value)
Solutions proposed in comments to the question seem to be reasonable, but I would offer one more if you don't mind. If TotalRate field's value is somehow important/vulnerable then I would never store it in database to avoid possible malicious manipulations from app users. In case there is some good reason/requirement to store calculated value in database I would go with calculation on the server side and handle it in onBeforeCreate and onBeforeSave model events:
// onBeforeCreate/onBeforeSave event handlers
record.TotalRate = record.PerHourRate * record.HoursNumber;

Keep ASP.NET ViewState without being passed via QueryString

I have a form with its method being "get" that passes the variables and their values to the query string, respectively. However, it also passes the viewstate variable in the query string. Now, I have a very long viewstate value on the given page, and if passed in the query string, the viewstate variable will cause the page to error out, due to "too long of a query string" which happens to also be too long of a url.
I cannot merely remove the viewstate variable - I need it. But I need to pass the viewstate variable along via some method other than get when the form is submitted, while the other inputs of the form (the non-hidden inputs) are appended to the query string. Is there any way to accomplish this?
Can you change the method to "post" and then use request.form to get your variables?
Otherwise, you might be able to use the session object...
...or a serializable class object
...or a temp table in a database
Several different options

ASP.NET Site architecture issue

Due to the nature of the way the website I am working on was designed I have an issue I now must resolve.
I will try to write this question as language-independent but the site is done in ASP.Net with C#.
Overview
The structure of our site looks like this for any given "object":
ASPX Page
Multiple UserControls (ascx pages)
Textboxes, Comboboxes, Labels,
Buttons, etc.
What this means is, if we have a User page and that page might have 3 UserControls. One for User Information (Name, email, etc.), one for Address (City, State, Zip, etc.) and one for Regional Settings (Time zone, language, etc.)
All of these fields on the 3 UserControls are all stored in the same table in the database.
Now, we are using DataBinding and EntityFrameworks to fill in the data and this means that when we save an object, we are actually calling save 3 times (1 per UserControl).
(Language-Agnostic: We don't actually
assign values like User.Name =
"Bob", it is handled by a Framework)
Because the UserControl only knows about the fields it contains, when the Databinding saves it "thinks" the other fields are now blank. (i.e. Address control saves and now "Email" and "Name" are null values). To "fix" this there is a method called "MapOldToNew" which goes out and grabs the old record and fills in the values on the New object that is about to be saved. This is generic, which means we don't have a method on the Address Usercontrol that says "go get the Name and Email fields and fill them in", instead it loops through all the properties of the Entity(object) and if a value is NULL on the New object but NOT NULL on the Old object (the one currently in the database about to be overwritten) it will make the New value equal to the Old value.
The Problem
Dates. We allow NULL dates in our database.
If a User fills in "Birthday" and saves, they now have a Birthday value in the field in the database.
If they go back and clear out the Birthday field on the web page and save, the MapOldToNew method grabs the old record, sees that Birthday is not null there and is Null on the New object about to be saved, it will override the New value (NULL) with the Old value ('7/23/1981' for example).
Solutions?
Rework our system to not use
UserControls but instead have all
controls on the same Page, thereby
reducing the number of times we need
to Save and not requiring
MapOldToNew. For now, let's
assume this is an impossible
solution (even though it is the one
I feel like should be done for
plenty of reasons).
Store all
dates as Strings and do conversion
on Load and Save. (Strings don't
have the same problem because once a
string has been modified it is now
an empty string, and not NULL)
Do not allow NULL dates in the
database and always store
DateTime.MinValue and do not
display it On Load.
Those are the ideas I have come up with off the top of my head. For arguments sake, let's assume #1 is not possible (because I have a feel management won't like the time it will take to accomplish).
How would you "fix" this?
Another Explanation
Here is the problem broken down more.
User Record has 2 fields. Name and Birthdate.
Assume that there is 1 ASPX Page with 2 UserControls (ascx).
UserControl1 has a single DatePicker DataBound to "BirthDate".
UserControl2 has a single TextBox DataBound to "Name".
When UserControl1 calls Update on the ObjectContext the User object it sends looks like this:
{ Name = NULL, BirthDate = 8/13/1980 }
MapOldToNew looks at the Database Record and sees that Name should be "Bob" so it fills the value in and the record saves.
UserControl2 calls Update now and the User object looks like this:
{ Name = "John", BirthDate = NULL }
MapOldToNew sees that BirthDate is NULL so it overwrites it with 8/13/1980 from the database and the record saves and the database has the correct values.
Now assume the user goes back in and deletes the BirthDate value. When UserControl2 calls MapOldToNew, it sees that BirthDate is NULL (which the user wants) and overwrites it from the Database.
If it DIDN'T overwrite it from the database, it would always save it as NULL and the user would never be able to set the BirthDate.
I would refactor MapOldToNew to understand nullable types. Im assuming you have a nullable date (for ex. datetime? or Nullable and if so - do not make the assumption of overwriting if null.
Also in the future consider MVC or for WebForms consider Dynamic Data because of the mapping of types to models.
http://aspnet.codeplex.com/wikipage?title=Dynamic%20Data

Why data binding does not remember the old values?

I have a classic ObjectDataSource and a ListView in my page. The List view just displays some data and when switched to edit template it allows the user to change the values. I want the user to edit just some values -- so I bind just these ones in the edit template.
The problem is that the other values suddenly turn to nulls or 0. I tried to bind all of the values at once and it works fine, but I cannot understand why the old/original values just disappear. Is there any way how to bind the old values?
Thanks for help.
The problem is, that only the data that is included into a round-trip to the server will be available in the postback. That includes all that that is bound to BoundFields, TemplateFields or if the Propertyname is included in the DataKey (or DataKeyNames, don't know right now).
The best approach to fix this, and to keep the overhead to a minimum is to add your primary key to the DataKeyNames collection. This allows you to have access to your custom object that contains an unique identifier and all properties that have just changes.
In your Update Method of the ODS (in your custom class) you now need to retrieve the old object by its unique identifier, manually assign the new values and saves your object back to the database

Data Binding fails

I have a form with 30 fields of data on it - mostly TextBox controls displaying string data, but there are 3 dropdownlist comboboxes and one textbox used to display a datetime. I'm using a BindingSource to bind these fields to a custom data object derived from BindingList<>.
Depending on the content of the data in the data source, sometimes a specific field will simply fail to bind - meaning the data from the data source does not get moved to the control. The control that fails is a simple textbox.text / string binding.
I can perform a specific query that in every instance will cause this specific field to fail to bind. I have other queries that can be performed that produce a set of data where the bindings all work perfectly.
My question is - what causes data binding to fail? Will the failure of some other bound field stop all the other bindings from firing?
Here are a few things that you could check
Is there a mismatch in the size of the data field and the field in the UI
Are there any special characters in the data
Is the code that binds the fields not being run in certain circumstances
Is there some code that is clearing the binding
I solved this issue by writing my own binding infrastructure. That gave me more explicit control over when bindings fired and what happened as a result.

Resources