ASP.NET GridView postback not setting posted controls' values - asp.net

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

Related

Not able to retrieve RadTextBox/ASP.NET TextBox updated value in RadAjaxPanel

This is really awkward. I am working on Telerik controls. I have a .NET web usercontrol which have some RadCombo boxes and a newly added RadTextBox. All of this in a RadAjaxPanel and RadLoadingAjaxPanel. When the user control loads, all of the control load values from database, including RadTextBox.
(The problem starts here) And when I try to update the RadTextBox control value and try to submit, I "Always" get the old value of the textbox.
Please check your Page_Load event and verify that you have an if(!IsPostBack) surrounding the code that loads the original values to the RadTextBox.

ASP .Net Textbox Textchanged event

I have a webpage. I show records from table, lets say, students in my page. I query all the students and show them in grid. I want to use a textbox for filtering the datagridview results. For example if the user types a in the textbox the grid will show only the students who has "a" in his/her name. I want to refresh the grid at the same time while textbox is being edited.
i have set the autopostback property of textbox to true, and i refresh the grid in textbox's textchanged event.But the textchanged event fires only after the textbox loses focus. How can I make it fire after user types just one character ? thanks.
You have to use the onKeyDown event. However, I'd advise you to use ASP.NET AJAX or jQuery to load the results with Ajax.
Here is one example from asp.net: http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx
Another one, from Code project:
http://www.codeproject.com/Articles/38803/Google-Like-Search-TextBox
You might want to show some your present code, if there is a particular method you want to go with for this. Otherwise your going to get a people telling you the way they would do it.
Does it look something like this right now?
<asp:Textbox id="myTextbox" runat="server" onChange="txtChanged" AutoPostBack="true"/>
public void txtChanged(object sender, EventArgs e)
{
//Get text from textbox
string text = ((TextBox)sender).Text;
//Do what ever it is you want to do to edit the text
text = text.ToUpper();
//Update the other textbox with this text
txtMyText2.Text = text;
}
I think the best and most clean way is to use Rad Controls, here is an example on how to do it:
http://demos.telerik.com/aspnet-ajax/controls/examples/integration/gridandcombo/defaultcs.aspx?product=grid
The event TextChanged only fires when you send a request to server. If you wanna launch an event or make a function when the text inside textbox changes, use an OnKeyDown event (right with Schiavini).
You can use PicNet to do this in the Client instead of the Server for a better User experience. You can find it here http://www.picnet.com.au/resources/tablefilter/demo.htm Remember that the Gridview is rendered as a HTML table, therefore you can freely use this jQuery plugin.
Good luck!

How should I set an asp.net label's text to some value in another control?

I'd like to set the text of two labels to values found in a FormView on a page (whose data comes from an SQLDataSource.)
What's the best way to do this? I'm thinking of using the DataBound event for the FormView to set the label text to the value of a field in the FormView, or of using the SQLDataSource Selected event to set the labels to values retrieved by the query. Could I use the Page_Load event in conjunction with the FormView?
The FormView only displays one of the two values, though the other value is retrieved by the SQLDataSource.
I'm unfamiliar with accessing the data structures behind these controls but figure the data is there so I might as well use it rather than run the same SQL query twice.
My question then is which event do I use, which control do I access the data from, and how do I access the data from that control?
I'd use the OnDataBound event and get the value from the underlying datasource using:
lblExample.Text = ((DataRowView)((FormView)sender).DataItem)["fieldName"].ToString();
Hope it helps.
// CeriQ
If you're just trying to set a label, just set the label at the Page_load event:
myLabel.Text = "someValue";

Textbox Wrong Value

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];

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.

Resources