ASP.net Calendar Control with an embedded textbox for each day - asp.net

We want to use the calendar control to fill it with some text for each day, we tried two methods:
Defining all the textboxes (one for each day) statically in desing mode and moving them (e.cell.Controls.Add(tbDay1)) to each day cell on the OnDayRender event. But there is some problem with viewstate as the textboxes appear 2 times (the ones put in design mode plus the ones in the calendar) and in each page load the contents of the textboxes are duplicated exponentially (test->testtest->testtesttest), it adds 1 semicolon per postback too..but we think its caused by some problem with the updatepanel container.
Defining the text boxes dynamically...we dont know exactly how to include the logic in order to: Load the initial data from database (it would have to be after OnDayRender event somehow, preserving it between page loads (dynamic + viewstate..) but not overwriting new values the user may input...
Any ideas? Preferably for method one as its already implemented.

In the end we defined statically a htmlcalendar and 31 textboxes, added a javascript function that places each textbox inside each day div (with some formating) and on the pageload added some bussines logic to enable/disable the textboxes (with FindControl()) depending on the holidays and so..

Related

Create Dynamic Controls using retrieved data [Asp - Vb .Net]

I'm trying to create multiple controls by using retrieved data from query, but preventing them from dissapearing on postback, allowing me to get and mantain their values, the problem I have is that I cannot create them on Init because the number of controls, their IDs, and other properties are only known after user selects an item on menu.
Page loads a Menu with all its items and values (Data dependent), plus, a button is loaded too
User clicks a item on menu.
Selected value is used to perform a query by using a Dataset (This happens inside a function which is called from Menu_ItemClick event).
Retrieved data from query is used to determine how many controls must be created (2, 4, 6, etc.). Each control has its unique ID, which is given according to data.
Controls are created and placed into a Panel (named p).
Now controls are visible and available for editing (RadioButtons, TextAreas)
User clicks on button to save information from dynamic controls into a Database
Problems I'm facing
Controls dissapear on postback when clicking button, because they weren't created on Init.
Placing button on UpdatePanel to prevent whole page postback, makes dynamic controls not accesible when trying this:
For Each c In p.Controls
...
Next
The only control it gets is a single Literal control (Controls count is 1), as if other controls didn't exist.
Thank you in advance.
When you wrote "Controls dissapear on postback when clicking button, because they weren't created on Init", did you mean to say that "Controls dissapear on postback when clicking button, because they weren't re-created on Init"? If not, then that is likely a root cause of your problem - dynamically-created controls must always be recreated in response to a PostBack (cf. ASP.NET dynamically created controls and Postback). There may be other issues as well, as dynamic controls in Web Forms can provide a lot of challenges as your scenario gets more involved - here's one article that lays out many of them under various scenarios http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx (e.g., if the user can re-select from the DropDownList to generate a different set of dynamic controls). The canonical reference on all of this is http://msdn.microsoft.com/en-us/library/ms178472.aspx.
Now, on PostBack you'll need some way to ascertain which controls were dynamically created so they can be dynamically re-created. As such, you'll need to store somewhere whatever information allowed you to dynamically create the controls. Since ViewState isn't available in Page_Init and there can be other issues introduced when using sessions, my suggestion is to simply declare a HiddenField that contains that state information. In Page_Init, you'll then need to get the HiddenField's value from Request.Form (since the value of your HiddenField won't be loaded until after Page_Init from ViewState) and go from there to re-create your controls.
My final suggestion: try getting everything working with a regular Panel first and then try and introduce the UpdatePanel - no need to over-complicate the problem at first.

I have to do two seemingly mutually exclusive things on leaving an asp:textbox. Please help me get some clairity

This project has gone from being a simple '99 Ford F-150 to the Homer.
I've got controls with a gridview with textboxes for data entry.
All the user controls on the pages are in AJAX updatepanels.
User types in a database column or budget entity or some other financial thing they want to include in the report.
The textboxes in the gridview have autopostback = true set.
overly long background info
When the user leaves the textbox, during the postback (triggered by onTextChanged) I do some validation back on the server on their entry - regexs, do they have rights to that column, is that column locked, etc. If it fails, I put a error message next to the textbox. If it passes, I wipe out any title or error that used to be next to the code.
Focus is getting lost from the postback if they're tabbing out of the box, rather than going to the next textbox in the gridview.
So to fix that I need, if their leaving the tb via the tab key, to also figure out what textbox or gridviewrow they're on, if they're not on the last row, and after the validation and labeling, put the focus on the textbox in the next row.
I can't figure out how, in ontextchanged, to find what caused me to leave the textbox, so I'm thinking use javascript onkeyup to test the key pressed and then find the next box etc, but the ontextchanged fires first and then the js never does, and also, since the control is all AJAXed, the javascript can't find the textboxes because when you enter the page everything is collapsed (the requirements people loooove to collapse and expand things), and so when it's expanded, all the 'new' textboxes are up in the viewstate stuff in the page source, and not down where javascript can see them.
The questions
So I'm wondering if I can have an onblur in the javascript that can trigger a postback where I can do my validation and such, and either 1) include the keypressed or pick it out of sender in the event or 2) followup the onblur with onkeyup and somehow figure out what textbox is next on the grid and throw focus there.
Or, is there another .NET based approach that could work for this? In terms of tearing the whole thing down and starting from scratch, I couldn't sell that to the bosses, I'm past the point of no return as far as that goes.
Does changing the one textbox value change anything else on the screen besides the error message/title associated with the textbox?
If not, my first thought would be to stop using the AutoPostBack and change to a Page Method hooked up to the onblur event. This should fix your tab issue and also reduce the back and forth between client and server.
Here's a blog post that talks about update panels and also Page Methods; The blog also includes a lot of useful posts about using javascript with .net, ajax, etc.

State storage in GridView

gridview has 20 pages. The user is see first gridview page . He decide to apply highlite style using jquery on few row . Now he move to second page . He apply style this page too. Now when he go back to first page, he cannot see row with style he apply before he move to page 2.
How to store state of the rows style when user moving on pages? Perdon my English
Use a cookie to store highlighted rows on a given page. Hook up to a event on which highlighting takes place (click?) and add a code to rewrite a cookie with currently selected rows. When you will be printing the table just read the cookie and restore selections based on the value stored in it.
I would save the highlighted row id's in a hiddenfield. Pageing causes a Postback! Also you need some javascript code to re-highlight the fields.
If can afford to make the solution less efficient, I would change the highlight method so that in runs server side with a post back instead of using JavaScript and then put the GridView in a UpdatePanel.
Also remember to use DataSource object (e.g. ObjectDataSource) instead of binding your data manually using .DataBind() in the code behind. This should ensure that you don't rebind the data on every post back.

Custom Control Event Not Firing First Time

My application runs from a single page (default.aspx) and loads custom controls based on what the user selects (this is done in the LoadPage event).
The problem is that the FIRST time some new controls are loaded onto the page and a button is clicked, the page is NOT firing the Click event. This also happens when using a GridView and other controls.
However, if I simply reload the page a second time, the events are fired properly. (this works successfully every time)
Two questions:
1) Why would this occur?
2) How can I troubleshoot it better? I've tried checking the Request.Form(__EVENTTARGET) (which is blank)
There are times when it works properly which suggests a PostBack issue - but it's not obvious.
I have identified the source of the problem and wanted to note it here so that it may help others.
The code in question had three different methods that assigned the Custom controls ID property. Two of those methods were doing it dynamically so the first time in, the custom control's ID was "UserProfile" - the second time (during the post-back), it became "uc1" (user Control 1). As a result, since the two IDs were not the same, ASP.Net did not recognize them as being the same control.
The SECOND time it posted back, it used the second method and thus the controls were the same and did get updated.

Can't find dynamic controls in datagrid on postback

Using .NET 1.1, I have a DataGrid that contains three columns for each row.
Row one has a label with some text.
Row three will always have two radio buttons and two checkboxes and those are in the HTML side of the page.
Row two will have dynamically generated controls (just textboxes for now) and there can be 1 or more per row. These will be used for user input.
There is a button on the page and when the user clicks the button I need to update the DataGrid’s source (my DataTable) with the new values from the user’s input.
The issue is the DataGrid seems to be losing the dynamically generated controls on PostBack. I can loop through each Item in the DataGrid and I can access the radio buttons and the checkboxes, but the textboxes aren’t there.
Any ideas?
Remember: every time a postback occurs you are working with a new instance of your page class. Dynamic controls added to the page during a previous postback went to the garbage collector as soon as the page for that postback rendered to the browser, along with the rest of that page instance. You need to re-create your dynamic controls on every postback.
you have to regenerate the controls. You should be able to get their values from the http request object

Resources