ASP.Net Form Databound problems - c#

I know there is a similar problem on this forum, but the solutions did not really work for me. I am populating form controls with fields from a few different data sources, and the data shows up great.
I have an ImageButton control, which has an OnClick Event set to grab all of the data from the form. Unfortunately, when I click the button, it seems as though the page is reloading first, and THEN is executes the OnClick call. The data that was hand-entered, or hard-coded seems to be pulled fine from the controls it was entered in, but anything that was pulled from a datasource is not able to be read. Any ideas. this is the last hurdle in a project that I have been working on for 6 months.

Are you talking about drop downs or gridviews? When are you binding data, on page load?
Good design will have you bind your data upon page load but only in
if(!isPostBack){
dropdown.databind()
gridview.databind()
}.
Otherwise it will rebind every page load. If its not reloading you can get selected values from those controls if thats what your looking for.
An alternative is to set your data source and databind in your aspx page with a datasource object. That automates the above automatically.

Have you enabled viewstate on your controls? Posting code samples would go a long way to helping solve your issue.

When you click on a bottom in asp.net at first all the page Events take place like Page_Load and ... and then the event happens ( in this case Click).
But because everything is loading again, I think that you have a !isPostback in your code that you use to bind the data, you should remove that in order to get your data every time.
Or if it is not the solution please post some code and more description for the problem

Actually, it is hiddenfields, dropdowns, labels, and textfields. I just tried doing the binding in the init, and the load, but no dice. When I tried binding it on !isPostBack only, none of the fields showed up.
I think one of the main problems is that the dataset I am getting is from a method call to an API. I receive the data fine, but it comes in programmatically, then I have to do all of the control-setting programmatically as well. Would you like to see the code for ideas? Thank you for helping, no one is working today!
Jason

Related

Search Button on Customized Gridview Takes Two Clicks to Fire Event

I have a customized Grid control that is inherited from Gridview that has search and pagination. Everything works great except this one little thing. Whenever I use the pager to go to the next page, then I use the search, the event that handles the search does not fire on the first click, it takes a second click to get it to fire. Any ideas?
The code for the control is here:
Grid.cs
Important Note
I am aware that it is frowned upon to post large blocks of code... But, the entire control's code is posted to give the whole picture of how it is built. The control itself is a bit on the complex side as it performs searching, sorting, and pagination all server-side; and this code is a completely custom control that just INHERITS the GridView.
I AM NOT looking for someone to write a fix for me, just an idea of why this one situation may be happening!
With that said, to break things down a little more with the code... The search form (text box and buttons) are created dynamically and added in the CreateChildControls method. The search form works perfectly when the Grid is initially loaded, but after using the pagination, the first click of the Search button does not fire the method assigned to the Search button's command event, but the second click does. It seems as though something in the postback is not recognizing the button's command event has been triggered...
A trace of the calls to the methods show:
1st Click - Everything from ViewState is loaded, no postback events are called.
2nd Click - Everything from ViewState is loaded, postback events called.
I'm looking for ideas on where to go from here, as I've been trying everything I can think of page life-cycle wise to see if I can get this functioning properly. The only thing that has worked is setting EnableEventValidation to false on the page that implements the Grid control, and performing the "initial" data bind on every "Page_Load" (not just "if !IsPostback"). But, for obvious reasons, that is not an acceptable solution.
Found the issue... Posting here for anyone who might make a similar mistake.
In CreateChildControls(), I was executing the base before adding the search form to the control. A simple switch around to calling the base after adding the search form, and getting rid of the condition "if (this.HasControls())" resolved it. From what I can see, calling the base after adding any child controls allows those child controls to have their events/handlers properly registered.

.Net form controls drop by one "line" after selection

Yes, I realize that's a bit of a vague title but I'm having a hard time stating the problem. I have a .Net .aspx page that has a Master page, some Ajax, and an updatepanel. My problem occurs on 2 different pages but in both cases I'm either selecting a radio button or a checkbox when the behavior occurs. Immediately after selection the entire page moves down. It does not scroll but instead it is like an extra tag was inserted into the source. I have done HTML source comparisons before and after this change and nothing is different. I can only assume it is related to the updatepanel but I cannot determine where this may be happening.
I'd be happy to provide more information if you can direct me towards a solution.
Thanks!
I am not entirely sure if this will do it and there is not enough detail here to be sure, but have you tried setting RenderMode to inline on the UpdatePanel? Maybe that will do the trick. Otherwise go take a look at Fiddler and see what is coming back from the server. Alternate recommendation (if none of the above work) is to just get your result in Json and change the markup yourself with jQuery or something like it.
Incredible. This was exactly the problem. I've modified my code as follows and form stays in place after selection. This was a simple case of selecting a radio button and having it auto-populate a dropdownlist.
[asp:UpdatePanel ID="panelValidation" runat="server" ChildrenAsTriggers="true" UpdateMode="Always" RenderMode="Inline"]
[ContentTemplate]
[asp:ValidationSummary ID="ValidationSummary1" runat="server"]
[ContentTemplate]
[asp:UpdatePanel]
Thanks!

How can I refresh gridview in ASP.NET C# after Ajax call and executing static method?

I need to refresh data in GridView after I call static method (Asp.Net, C#).
Is it possible to accomplish it?
well, you can rebind the dataset/datatable you got to the gridview like this,
myGridViewId.DataSource = myDataTable;
myGridViewId.DataBind();
EDIT:
After seeing the comment:You mean with out submitting the form manually ,you can trigger it with the following javascript code..
<script type="text/javascript">
setTimeout(function(){window.location.reload(true);},timeoutPeriod);
//timeoutPeriod in milli seconds..
</script>
I know I am grave digging but I had to do this and I had one heck of a time finding an answer that was satisfactory.
If I am reading the question correctly you have a girdview that is to be 'refreshed' after you complete some call to the server.
So I assume you are doing something where you are making changes to a grid's datasource and then need to reflect those changes to the client by changing the grid they are looking at.
I found this great article where they are discussing building a gridview on the fly.
"Using Ajax to create a gird view"
I found the methodology was what I needed except the datasource was not what I needed and it did not go into detail about how you would format anything in that grid (because nothing was there to start).
So I took that example and created a web method where I got my data. I then on the server side added the data into a different datatable that contained only the strings I needed, after manipulating it, and passed it back to the page.
then using some of the jQuery in that article, I was able to 're-bind' or more factually rebuild the grid.
The drawbacks with this are that you are not going to be able to manipulate your data in your aspx page. This method is dependent on a 'template' row existing. The grid I was managing did not need to have data from the start, so this was fine for me. if it did need data at the start in document ready I would have needed to add a call to my method to update the grid.
I hope this helps someone else.

Formview Being Cleared

My problem is that all the textbox's my formview are getting cleared when I hit the submit button.
I currently have a page with a small section that has an update panel around it. This small section adds an address to my databse. To the left of this form there is a gridview that is tied into the formview. So if i click on an item in the gridview its contents fill the address section(formview) with the correct data.
When I hit add the data gets validated in the c# code behind, and if all the info is correct the address is inserted. If there is an error the entire form is cleared and the error message(label) is displayed.
I have already done this many times in other pages, but none have had the gridview tied to the formview, and they have all worked. I tried removing the gridview and the form still erases itself.
Is there some reason that .net thinks it should be clearing the form? When in other cases it decides it won't? If so what are these cases, or what general tips should I try to solve this?
in the page_load are you using if(!Page.IsPostback) { ... } so if it's a postback nothing gets re-bound?
is the ViewState enabled?
Yup many hours later I found that a single panel that was wrapped around the section had a EnableViewState="false" added to it. Sad part is that I know I didn't add that because I didn't even know what it was until craig here mentioned it. Visual Studio must have added it sometime.

Code thinks Datagrid footer textbox is empty

I am working on an .net (C#) web application.
Recently a defect came my way that stated that when two users were logged into the application at the same time they both could not update values without one refreshing the page. When I looked into the issue I discovered that the author of the code has used static datasets. I changed the datasets to not be static and everything works great.
However, This issue spans many pages in the application and I must fix it everywhere. On some of these pages the application uses datasets to bind data to datagrids. The datagrids are populated with the information in the dataset and the footer contains some textboxes and an add button to add extra rows. Here is where the problem starts:
When the page was using static datasets and the user attempted to add a row through the interface everything worked fine. However, when I changed it to use datasets that were not static (they are loaded every time the page loads) and the user attempts to add a row, the code thinks that the textbox is empty (discovered when debugging even though I can see the text that I entered) and empty field validation fails and a message is displayed.
Can someone please tell me why on Earth this is happening? Why does it see the text when the dataset is static (the dataset NEVER populates the foot row) and not see the text when it is not static? Some insight would be awesome!
Thanks in advance!
Turns out there was an issue with how the grid was binding.
The binding was occurring when the datasets were being instantiated. This means that every time the page loads the binding was occurring again and causing the text to be blank. This is weird considering the item command looked like it was executing before the page load, but the datagrid didnt like it anyway. I moved where the binding was occurring so that it only happens once when the page loads (and of course it happens when something is added to the grid).
This seemed to do the trick!

Categories