How to properly increment a specific variable in aspnet? - asp.net

Im trying to increment this specific element in ASPnet. A user selects a specific item in the dropdown box, and it increases the quantity as the user selects.
If DropDownList1.SelectedIndex = 1 Then
DropDownList1.SelectedIndex = 0
intDonutqty += 1
txtDonut.Text = intDonutqty
End If
Everything else works fine, but for some reason it just stays at 1 without incrementing.
Thanks for the help

Variables in the page are local to the instance of the Page class, and the instance is only used to handle one request. When you make a postback and the next request comes for the page, a new instance of the Page class is created, so you will get a brand new variable which starts out at zero.
To keep the value between requests, you either have to store it in a session variable, in the ViewState, in a cookie, or a hidden field in the form.

Related

HiddenField value lost

I'm having a very strange problem with our ASP.NET application. It is losing the value of a HiddenField on the pages that I had programmed. I will explain the situation in a simple way:
I have a page that has 4 Buttons and 1 HiddenField.
3 of this buttons modify the value on the HiddenField.
The fourth button reads the value of the HiddenField, something like this:
Select Case hf_PageState.Value
Case "new"
'Validate data only on new
'...
Case "modify", "delete"
'Validate data only on modify or delete
'...
Case Else
'Critic error
Throw New Exception("HiddenField Value lost")
End Select
This button is only available after using any of the 3 buttons mentioned before.
In very very strange cases, when the user uses the fourth button, the application fires the "HiddenField Value lost", but I don't know why, because after a lot of test I can't reproduce the problem. This problem has ocurred approximately every 3 months.
¿There is a way a HiddenField can lost their values assigned for any reason? This weird problem happened now on 2 pages with distinct logic function.

Store function Varables and Prevent them from being set to their initial values on page_load

I need to navigate between some records that I have in a database.
For example, I have a prev-record button on my page which navigates back by one record.
But as soon as I click the button, the page_load is fired first thus setting my function variables to their initial values.
Thus I can use the button only once. The second time I use, it will navigate to the same record.
I need a way to store the value of the variable after the function is executed and reuse this value again.
I am a beginner.
Thanks for the help in advance!
This can be achieve in different ways.
You can keep the old value in viewstate or in query stirng and next time you can check it.
But if you are using gridview you can go through the below example
http://www.codeproject.com/Articles/67520/GridView-Paging-and-Sorting
view state example
http://aspnet-with-c-sharp.blogspot.in/2010/12/viewstate-with-example-in-aspnet-c.html
In your page load, only set the function variables if Page.isPostback is false. This will prevent the values from being reset on a postback. eg.
If (!Page.isPostBack)
{
//Set initial function variables
// also store the initial values, possibly in session
}
When you navigate to the next record, store the previous record values in session. Then when you you go to the previous record, use the session value.

transfering value from one page to another in windows phone 8

i have three pages a,b and c all pages have checkbox control.i transfer the value of one control on page b to page a by query with navigation method and when i transfer page c value to page a the previous value that is transfered to another control previously is removed on transfering value how can i save it so that it remain on the page.
please help me in finding solution
Thanks
Use PhoneApplicationService.Current.State.
It works like session variables.
This is how you can use it:
Page 1 - //saving the value
variable 1=[your value];
PhoneApplicationService.Current.State["any session variable name"] = variable 1;
Page 2 - //retrieving the value
variable 2 = PhoneApplicationService.Current.State["any session variable name"];

how to store and which object should be use to store value for the later usage

I have a three texbox and Next and Prevoius BUTTON .
When i click on next, next value with respect to AUTOGENERATED ID should be fetch and when i click on previous ,prevoius value should be fetch.
let suppose, i am displaying current record as 2 ,when i click on next 3 should be display,
but when i click on previous 1 should be display.
But my problem is that each time whenever i click on NEXT or PREVOIUS button i have to get data from the database .I dont want to do this i want to store previous and next record in some object.how will i do so.I dont want to use viewstate or cache or session or application object .Is there any diffenrent logic .please help
If you dont load the page again, use an asp:HiddenField to store that value.
Or... use cookies
Then you check if the hidden field or specific cookie has any value assigned
Create a cookie that stores the data you got from database.
Check if cookie is not null, if true get it's value else go to
database and create the cookie with the value
how to manipulate cookies:
HttpContext.Current.Request.Cookies
.Add(new HttpCookie("myCookie", "1"){ Expires = DateTime.Now.AddMinutes(5) });
var myCookie = HttpContext.Current.Request.Cookies["myCookie"];
if (myCookie != null)
Response.Write(myCookie.Value);

How to create a form with variable no. of field set

I have to design a page for user information, for some background verification purpose, at my work. I need a set of fields for address, total count of which will be selected by user to update the form with that many fields. So, if user selects 3, form will have 3 set of address fields. Similar concept for work and education details.
Right now, I am passing the count to a handler page, which checks total count, and return it along with querystring, back to the main page. I am able to update the no. of fields, this way, but, all values are lost, once I return to the form. There are a lot of fields to even use session object for every value. Also, it resets the count of other such field set to 0. So, if I select 4 in address field, it renders four set of address fields, but fields for other details are gone.
I need to know, if it is possible to update the fields, using just one page, instead of creating a handler file to handle the redirect, so that I don't lose other data.
Sorry, for sounding a bit confusing. Will update the question, if needed.
Edit:
Similar blocks are there for education and work details. I want the update button to update the block, with that many fields, while retaining the values already entered by the user.
I have finally shifted the update code to one page. And the total count of blocks, is calculated by this way.
if request.form("addresscount") <> "" then
varaddresscount = request.form("addresscount")
else
varaddresscount = 1
end if
varaddresscount is used to loop through the html code which renders address fields. Even with this method, if I click on update button to change the total field count, every value entered by user is reset to default. Is there a way to retain the no. of fields without using session object, as there are way too many fields for which I have to store the value in session.
Why not have just a "add address" button that, whenever clicked, adds a extra set of input boxes using Javascript? That solves a lot of your problems regarding retaining the data on already filled in fields AND it makes it easier for the user.

Resources