Get new values of textbox from a listview - asp.net

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)

Related

SqlDatasource Update query controlparameter not picking the new value

if you have not encountered this problem and I have to upload my code in order to explain it for you or for you to be able to debug it, then this question is not for you.
I know I can solve this problem by using code-behind, but I don't want to do it. Eventually I will have to do it if nothing works.
I am not using any databound control (gridview,Formview,Detailview etc). Everything is a general form control: textbox and ListBox. I am using their Text and SelectedValue properties to supply values to Updatequery's ControlParameters. Everything should be working as expected. I have played with the ViewState property of the texbox control and the sqldatasource control itself, to no avail. The stored procedure used for the update command is logging the values supplied from the ASP.Net side and amazingly it shows the old values of textbox that were there when the form loads and not the changes I make.
Whats going on here?
Thanks!
Seems like you are missing something in the asp.net life cycle.
Remove any DataBind calls to your sqldatasource on page Load.
If you are using DataBind on Load to populate those text and select controls, a databind will just overwrite any values you entered.
Don't be so shy to show your code, it helps a lot in providing good answers.

How to fill a TextBox in InsertItemTemplate(ListView) with a selected Value from ModalPopup displayed by clicking a Link in the InsertItemTemplate?

I have really put in significant efforts to somehow find an answer; having failed I have then searched many forums before coming to bother the experts here. Please help!
I have a Listview in a USerControl of my main form; its InsertItemTemplate has a TextBox whose value is to be filled in by Clicking a Linkbutton in a Row of another Listview (in another UserControl) in the ModalPopUp Panel, which Pops up when a Linkbutton in the Parent UserControl is Clicked. After the selection (by clicking the LinkButton in the ModalPopUp), the ModalPopup closes immediately and in Code-Behind of the Child-UserControl, I save the Selected-Value in a Session variable. My problem is (Because the InsertItemTemplate is already remaining displayed) I do not know where to put the code for filling the TextBox (in the Parent UserControl) with the saved value from the Session-Variable.
ItemCreated, ItemDataBound are already executed before the ModalPopUp is displayed. So, though I have the required Value of the TextBox in the Session-Variable, I am unable to fill the TextBox in the InsertItemTemplate.
I will appreciate any suggestions, if any of you have found any answers to such a situation. I feel SOMEHOW the InsertItemTemplate should be made to load again and this TextBox in it should be filled in the ItemCreated for the InserItem datatype. But I do not know how to make it happen???
Any thoughts welcome. I have similar logic in many places in my App; and if I can't do it it will significantly affect my app :-(.
Hope one of you experts will kindly help me out. Thanks.
Thanks. I could fix it. Mentioning it here for some newbie (like me) who may face a similar issue.
The question was, where to put the Code (for the Selected Value to fill the TextBox in the InsertItemTemplate) in the scenario described above in the question.
I find that in the Pre-Render Event of the Parent UserControl, in the PostBack mode, after finding the said TextBox Control via the e.InsertItem.FindControl("TextBox"), I am able to fill it with the selected value. I don't know if this is the best way to do it, but it works fine now.
Hope this may be of some help to someone! Thanks.

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

Bizarre updatepanel problem

I have an updatepanel, with a bunch of things in that are generated dynamically.
Among them are textboxes, and checkboxes. Each is created in code with Textbox t = new Textbox() and Checkbox c = new Checkbox and are added withotu altering any other properties.
Later on when I hit "Save", I get the values. The Textbox value is whatever I typed in before I hit the save button, but the Checkbox is always false regardless of whether I checked it or not.
Is there any explanation for this discrepancy?
Another problem solved after hours of debugging... I fixed a problem a couple of days ago that was caused because my controls didnt have an ID when they were dynamically generated. I read somewhere that controls without IDs sometimes behave oddly...
So in this case I gave my controls (checkboxes, not the textboxes) a random GUID, but it changed every time they were redrawn, which confused the viewstate and meant it couldnt preserve the contents.

ASP.NET GridView problem

Well i have a gridview where i have defined the columns on my own and turned autogenerating off but now i have the problem that i cant access GridView.SelectedRow.DataItem.
As it turns out to be null now, when it had a value when auto generation was turned on..
Edit:
What i need is a way to save the ID of the row while not showing the ID to the user so if there is any way to do this?
I'm guessing DataItem is only properly filled when you are using DataBinding.
Are you using DataBinding?
Ok from this url:
The GridView (and actually, all our
data controls) does not save data
items across postbacks. This reduces
ViewState (if the objects are even
serializable) and enables garbage
collection to happen and clean up your
objects. So, when you click the
button to post back, the GridView has
not called DataBind and therefore your
data item isn't there. This is what
you've discovered.
Guessing you're reading the value from a postback, might just be the problem.
Try using SelectedValue, if you've setup the (primary) key for the items.
I've always used that and it worked.
msdn about SelectedValue
You can create a new hidden template column that will have a label with the ID . and in the cs file you use .FindControl on the rows.
You also have DataKeys property on the gridview, witch I think also does what you want

Resources