Textbox Wrong Value - asp.net

I have an asp.net page with a datalist with few textboxes, and a submit button.
when i cahnge the text in the textbox, and click submit, the value i get in the vb code is the old value and not the one i just entered.
Any idea?
thanks

There are two possible reasons for this.
Either (1) the part of your code that sets this value is being run at postback, thus resetting it, or (2) your textbox is disabled in .NET code (and enabled in javascript) so that .NET assumes that its value cannot have changed, and doesn't check the POST data.
Sorry for C# code examples, but i'm sure you'll work it out:
1:
if(!Page.IsPostBack) { myTextBox.Value = "original value"; }
2:
string valueFromTextbox = Request.Form[myTextBox.ClientID];

Related

Get new values of textbox from a listview

I'm generating a listview with textboxes inside. The textboxes values comes from a database. My issue comes when I want to retrieve the new values of the textboxes.
Here's what I do on click of the save button.
For Each item In mylistview.Items
Dim tbLgName As TextBox = CType(item.FindControl("tbLgName"), TextBox)
// code to encode into db ...
Next
The thing is it get the textbox but with the old values (coming from the db), How can I get the new value?
Thanx
I had this same issue and found that it was because I had added an OnLoad to my UpdatePanel that was binding this ListView. It was easy to fix because I was not using that way anymore and just removed it. For others it may not be as easy to fix, but may help figure out what is causing it.
(Yes, I realize how old this post is, but someone else may benefit down the road)

when gridView update is fired?

I asked a question Gridview is not displayed when using stored procedure in SQLDatasource which I want to resolve . Based on my old question, I am not sure when exactly is gridView update fired. I am under the impression
Gridview Update is fired automatically when the page is loaded if you use SQL Statement in SQLDatasource
GridView needs to be manually bound using sqldatasource.databind() function to update data if you use stored procedure.
But even then, the second case is not working for me. I know this implementation is somewhat buggy.Can anyone guide me throw this, when exactly Gridview is updated and possibly throw some light on my original question also?
Thanks!
Update: After further research, I found that databind() property is not needed to update the gridView. What is needed is a post-back. So if you add a button with no code inside it, it will update the gridView simply because the page has been reposted and (maybe)the textfield values have changed too.
I still dont get any result though. What I have done is entered "not data found" in empty template for the gridview. So now I get "Data not found" instead of nothing. Which does means that SP is run but returns 0 result.
Another thing that I found is, if you bound a text field to the SQLDataSource, by default, it changes the empty textbox value to NULL. This is kind of stupid, why will you need the NULL values for? This will result in 0 records since obviosly we dont store null int the database. (Null means nothing but Microsoft though the other way). So to fix that, go to the parameters list in SQL Datasource and select the parameter. Click on the link "Show advance properties" There change the property "ConvertEmptyStringToNull" from true to false. Although it did not fixed my problem yet (but fixed it somewhere else).
I am thinking I am using 4 parameters in sqlprocedure and one of them is causing problem? But the query does not ok when testing in SQLDatasource.
Is there a reason why you must use the GridView update event? Why not just add the code to bind the GridView during the Page_Load event or something? Have you at least tried to narrow down the causes for the erroneous behavior?
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlDataSource1.DataBind();
}
}
After my research, I found GridView is automatically updated when the page loads. It is the same weather an SQL Statement or a Stored Procedure is used in SQLDataSource. Also SDQDatasource.Databind() is not needed, but may be needed in special scenarios.
What triggers GridView update is page load. If you create a button and add a click event, just clicking that button will automatically cause gridview to update but clickevent cause page reload.
Hope it helps

ASP.NET user control - can't get client-side (JavaScript) altered value back into control

Ok guys and gals, here is my problem:
I've built a custom control that uses a textbox to present data to the user.
When the user interacts with the control the value of that textbox is altered with client side javascript.
I also have a button on my page. When the user clicks the button I want to take the value from the custom control (aka. the textbox) and use it elsewhere.
So, in the onClick event for the button I do something like this:
this.myLabel.Text = this.customControl.Value;
The problem is that the custom control does not have the new textbox value available. In the custom control the textbox is empty. However, I can see the correct value in the Request.Form collection.
Am I doing something wrong here? Or should I be reading from Request.Form?!
Interesting, I didn't realize readonly TextBox doesn't get updated from viewstate.
When I pull stunts like that in my web sites, I usually setup asp:HiddenFields that I dump data into with javascript (gotta love jQuery), and that I read values from on postbacks.
Keeps things cleaner I find.
Ah ha! I've solved my own problem!
Because I had set Readonly="True" on the textbox control ASP.NET was not picking up it's value from the postback.
Instead I should have manually added the readonly attribute to the textbox during my user control construction.
eg.
this.textBox.Attributes.Add("readonly", "readonly");
Strange that you answered yourself!
In fact, I've faced this nuisance before, and cost me some time until I found a note in the visual studio documentation describing the cause, you can read it here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.readonly.aspx in the "important note" section.

Fail to load values in textboxes

I have textboxes which is placed inside accordian (ajax control).In pageload event i have written the code to load values in textboxes. But onload event one error is coming as object not set to instance of a object. Values are not coming in textboxes.
Then i tried to initialize controls in textboxes .Then the error cleared. But
Values are not coming in textboxes. What may be the reason for this?
Have you set the properties of the Accordian properly?
If yes, check the values which you are assigning to the textbox. If they are blank, the values won't be visible.
Try giving a hardcoded value to your textbox and check if it appears or not -
textboxObject.Text = "Hello World!";
At a guess, as the Textbox is a child control of the accordian you possible have to use the AccordianObject.FindControl() method to find your textbox.
More information or posting some code would help, otherwise we are just going to be taking stabs in the dark.

ASP.NET GridView postback not setting posted controls' values

When adding an EditItemTemplate of some complexity (mulitple fields in one template), and then parsing the controls from the RowUpdating event, the controls that were manually entered by the user have no values. My guess is there is something going on with when the data is bound, but I've had instances where simply adding and attribute to a control in codebehind started the behavior and removing that code made the code work. As a work-around, I can Request(controlname.UniqueId) to get it's value, but that is rather a hack.
Edit
When I access the value like so
TextBox txtValue = gvwSettings.SelectedRow.FindControl("txtValue") as TextBox;
the text box is found, but the .Text is not the user input.
Did you turn off ViewState?
Did you add control programmatically in the template? If so, did you create them at the correct stage?
You should be able to use the GridViewUpdateEventArgs to retrieve the inputted value, for example:
TextBox txtValue = gvwSettings.SelectedRow.FindControl("txtValue") as TextBox;
I have used that syntax before and it works like a charm.
Moved post-back data-bind to Page_Init

Resources