HiddenField value lost - asp.net

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.

Related

ASP NET Dropdown box in a user control losing viewstate

I have a usercontrol in an ASP NET web application on which I create dropdown lists based on some loaded data (from an XML file via a component in the BIN directory)
I found that I couldn't retrieve the values across postbacks, and I've been doing a fair bit of reading to figure out why. What I would like someone to explain is why the following condition exists:
Dim dd As New DropDownList
With dd
.Items.Add(New ListItem("First Item"))
.Items.Add(New ListItem("Second Item"))
.Items.Add(sts(0))
.Items.Add(sts(1))
.Items.Add(sts(2))
.Items.Add(New ListItem("Third Item"))
End With
AddHandler dd.SelectedIndexChanged, AddressOf changed
divControls.Controls.Add(dd)
In the code above the three items 'First Item', 'Second Item' and 'Third Item' all behave correctly, as you can see they are added directly to the dropdown box in code. The items sts(0), sts(1) and sts(2) are all loaded from an XML file into either an array or list of string
Interestingly, sts(0) always posts back correctly, but any subsequent items which are added from the dynamic source don't. I've tried a bunch of ways of getting my list of strings into the dropdown so that they are still there upon postback but I'm not having much luck, would be most grateful if anyone could shed some understanding on this
I figured it out - I need to give each ListItem a value, I had lazily coded just the text portion since I wasn't using ordinals in the list, but ASP NET needed the ordinal value, or the list item value, to register the item for event validation, I expect ASP NET was only able to give subsequent items in the array or list of string the same ordinal or listitem value as all the other members of the collection, since they are from the same object. Works now!

Enter number in one textbox and get word as output in another textbox

This Question was asked in interview but i was unable to answer it
please any one can tell me how to do this ?
i have two textboxs txtInput and txtOutput
When user clicks the btnSubmit, he should get the number written in txtInput in words in txtOutput. Eg. if user enters 100 in txtInput, he should get hundred in txtOutput
Prumably this is JS. If it is the you can setup an event listener on the submit button and disable the default behavior. Pass the input value to a function that builds the number string and update the dom element for the output textbox with that return result.
If you're using a round trip to the server for the calculation then just ignore all the dom stuff and return the page with the appropriate values set.
If you're using a front end framework then this gets easier but more specific to the framework.
thanks ..found solution here
.NET convert number to string representation (1 to one, 2 to two, etc...)

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.

How to properly increment a specific variable in aspnet?

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.

when using datasource and databind to set items in a dropdownlist - index is always the value of first item

I am trying to use datasource and bind in my application to set a dropdown list to the results of a query. The dropdownlist populates correctly (shows each different type_name in the set) but later when I use ddltype.selectedvalue - I am always getting the value from the first item in the dataset. Here is the code.
if ds.hasRows = true
ddlType.DataValueField = "type_id"
ddlType.DataTextField = "type_name"
ddlType.DataSource = ds
ddlType.DataBind()
end if
Then later on I use the following code to try to get teh selected value
Dim typeID = ddlType.SelectedValue
I have ran the query to get the dataset on my SQL server and get the below results - but every time I use the above dim statement, the value is set to 812 even when type 2 is selected. Below is the results from the query that fills the sqldatareader named ds.
type_id : type_name
___________________
812 : type one
813 : type two
Thanks in advance.
Based on the information in your question, I don't see anything wrong.
But based on common mistakes people make with this pattern, I am guessing you are probably running ddlType.DataBind() on every postback. So the user clicks a button or something similar to fire the postback, and the DDL is re-bound before your click handler checks the value. Re-binding sets the selectedvalue back to default.
Edit: Your databinding code in Page_Load should only run once. One way to do this is to wrap the databinding code...
If Not IsPostBack Then
If ds.hasRows = True Then
ddlType.DataValueField = "type_id"
ddlType.DataTextField = "type_name"
ddlType.DataSource = ds
ddlType.DataBind()
End If
End If
This will cause the databinding to occur only the first time the page is requested, but not after a button click or other postback action.

Resources