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.
Related
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
I have an ASP.NET page with a Wizard control containing several steps and about 80 form fields. The data is collected and inserted to a database from the code behind page. I need to set this form up so you can not only insert, but edit a record as well. So I want to databind the fields. I'd rather not use a FormView because then I would have to revise my existing code, since you can't access controls inside a FormView directly. Is there a way to databind the fields without using a FormView? I'm new at this by the way so sorry if the answer should be obvious.
A wizard is just a UI control with many steps in it. You can use it to insert, edit, delete or anything else you can think of. You can have an INSERT wizard and an EDIT wizard. The difference would be that there would be two of them and that the code behind for each one (presumably on the CompletedStep) would have slightly different code to persist the data. The insert wizard would call an insert database query and the edit one would call an update query.
That being said, you can access the control inside a FormView, I'm not sure why you said that you can't access controls indie a FormView. You can. See Using FindControl: Accessing Controls in a Formview.
You could even put the two wizards inside the two states of the formview - InsertTemplate and EditTemplate but thats getting a little crazy :)
I have an ASP.NET page using the AJAX library. There is a dropdownlist inside an UpdatePanel, which on index changing is supposed to update a different UpdatePanel to modify a grid control.
But after the page first comes up, and you adjust the ddl, the entire page postbacks, followed by a (correct) partial postback. After this one time, every other usage of the ddl performs correctly, trigger partial postbacks.
Both the panel and the ddl are being added from code. The UP is Conditional updates and ChildrenAsTriggers = true. The dropdownlist is AutoPostBack true and has an event set up on SelectedIndexChanged.
So what is going on? I've tried adjusting every setting I can think of and still the page completely refreshes once, then works fine after that.
I believe I've solved my current woes in regards to this problem, though I'm left feeling a bit dumb by the outcome. When programmatically adding the dropdownlist, I didn't give it an ID, assuming ASP.NET would just assign it some sequential name-container mangled ID anyway. It seems this was the cause. I'm assuming that the initial postback was due to ASP.NET trying to deal with the lack of a proper ID, assigning one, and then using that afterwards for the partial postbacks.
Or maybe it's something totally different at root, but the simple fix was: add an ID to the control.
I've been getting this exception in my code and wondered if anyone could help me out.
I have a Repeater Control bound to an ObjectDataSource, and the itemtemplate for the repeater contains a User Control (ASCX). This user control in turn contains several other controls, mainly a GridView that is associated with an ObjectDataSource.
On initial use of the controls in this setup, everything works great - data displays properly. However, when I change a filter option (dropdowns outside of the repeater), and then rebind the Repeater, I get the exception:
The ObjectDataSource control 'expDataSource' does not have a naming container. Ensure that the control is added to the page before calling DataBind."
at System.Web.UI.WebControls.DataBoundControlHelper.FindControl(Control control, String controlID)
...
...
at System.Web.UI.WebControls.ObjectDataSource.LoadCompleteEventHandler(Object sender, EventArgs e)
I'm not sure what the problem is - I've read in a few places that moving the datasource outside of the ASCX control might help - this does nothing. The objectdatasource appears to be properly structured, and as I said, it works the first time (only).
I noticed in the stack trace for the exception that this is occurring when ASP.NET is calling FindControl() after LoadComplete() occurs. If I step through my code, it appears as though all my code is finished executing before this happens, so it's all "system" code.
Why would ASP.NET not be able to find this datasource control in the LoadComplete Handler?
Thanks!
Other Notes:
This error occurs every other time. So the first time the data loads properly, then on second refresh fails with this error. Clicking "Load" again, it works (on the third time).
On the times that it fails, it looks like "Page_Load" is being called twice in the ASCX control. So the patterns are:
Working Pattern:
Page_Load on Parent Page
Page_Load on ASCX
Data Loads fine
Failing Pattern:
Page_Load on Parent Page
Page_Load on ASCX
Page_Load on ASCX
Exception
This is all happening from a call to "Repeater.DataBind()", but it behaves differently depending on if it has already been bound or not (evidently).
More Notes:
Real strange behavior. I removed the list of SelectParameters from the bottom of the ObjectDataSource, and all of a sudden, the page does not reject the ObjectDataSource as not having a NamingContainer. Of course, without these parameters, Databinding won't actually work...I can add them in the code, but why would it matter?
Found a strange solution, that I'll post and we can discuss to maybe figure out why this fixed it.
On my page, I had the following structure (paraphrasing the tags somewhat):
Page
DropDownFilter
Repeater
UserControl X
ObjectDataSource
ControlParameters Referencing DropDownFilter
End ObjectDataSource
End UserControl X
End Repeater
End Page
So as you can see, within the Repeater ItemTemplate was the user control, which in turn had the "guilty" ObjectDataSource with ControlParameters. These control parameters had the name of the DropDownList filter on the parent page referenced (so basically, if this control was added to any other page, it would of course fail if it couldn't find a control with the proper name).
So when I went through and changed all the ControlParameters to Parameters (removed the reference to that DropDownList control), now I no longer get the error.
All I can assume is that the fact that this datasource referenced a control on the parent page meant that it was having trouble getting added back to the page's control set on a DataBind(). You would have thought it would fail the first time if it was going to fail at all, so that's still a mystery.
Any thoughts?
This is an exceptional error in ASP.NET DataControls. I had similar problem and lost few months behind this eccentric error, but finally got the solution. The reason is; To display items in ItemTemplate, we should use a server control in the LayoutTemplate to act as the placeholder for the ItemTemplate. For example, we could use a Table/Div control with an ID Property in Layout Template. At run time, this placeholder control will be replaced with the contents of the ItemTemplate and "naming container error" will be disappeared.
Finally, if you are having an objectDataSource in ItemTemplate, make sure that you added somthing(like table/Div) with "Id" property in Layout Template.
Thanks,
Sunil.
Ray hit the nail on the head. You are definitely missing an "if(!IsPostBack)" somewhere. How are you adding the user control to the repeater? Is it dynamic? You say it's in the ItemTemplate, so probably not... But multiple calls to Page_Load imply multiple copies of the control.
Use both DataBind. Example:
SqlDataSource1.DataBind();
ListView1.DataBind();
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.