Passing Control from one Event to another in C# - c#

Following are the details of my problem:
I have 2 user controls (U1 and U2) in a Workspace. My first User Control U1 contains a grid. U2 has a richtextbox control in it. Currently when we change some data in a row in U1, and click the save button, the data is entered in database. When a child row is created for a row in U1 grid, it has U2 embedded in it. When i enter data in the richtextbox and click on Save. Save does not fire properly.
Purpose:
I need to save the data entered in textbox in a variable before the Save event is fired.
So, am using the leave event of textbox to save the text.
Problem:
But when i enter some data in the textbox and click on save, first the Leave event of textbox is fired and so the Save function does not fire properly.
Could anyone provide any hint as to what event of textbox i can use to do the following without affecting save functionality or any other approach to it?

If you are using server side lost focus (leave) event of textbox , then after postback this event will be fired.
So you had to wait for postback to complete beore clicking on save button.
If you have to store the value of textbox , you can create a hidden field and use textbox's client side blur event to store value at clientside only.
e.g.
javascript:
function savedata()
{
hdnValue.value = document.getElementById('TextBox1').value ;
}
Later after postback you can use this value from hidden field before performing Save operation.

Related

Asp.net Textbox.Autopostback causes another issue

I have dropdown and texboxes and one button on the form. I'm using 'autopostback=true' for all the form elements. When you fill the form you need to push 'send' button. But because of texbox.autopostback, you need to push 2 time to send form.
If you select dropdown lastly, then there is no problem. But if you're filling textboxes than you need to click 2 time to send form.
Is there any solution for it? I must use textboxes.autopostback='true' but need some solution.
Thanks,
UPDATE:
All controls stays in updatepanel element.
Set a hidden field value to "1" when textbox posts back and its value is valid, else set the hidden field value to "0"
On client-side, in pageLoad event, automatically click the button if the hidden field value is "1". So, to the user button is only clicked once and your auto submitting the form when text post back returns.
Both the above points need to be done in your code-behind.
The result of of this logic will enforce a consistent workflow, where the button can be clicked only once to submit the page.

Need suggestions on best approach to update text entered into multiple textboxes without causing a postback that shifts the page

This is a concept question. I have a web form with 6 gridviews, below each gridview is a textbox. Each row of the gridview contains a question and 5 radio buttons. When a radio button is ticked or text is entered in the textbox it updates the database immediately with one caveat, the textbox is committed when the user presses the tab key, or refocuses curser outside the textbox, or clicks an unrelated button (basically when a postback or textchanged() event occurs).
The problem: There is a delay during postback when the text is committed to the database causing the user to think they can move to the next textbox only to have the curser return to the previous textbox. I added code to prevent the curser jumping but the delay is still an annoyance to users during testing. I added a confirmation message (label) to alert the user when it's ok to move on but when the confirmation message disappears at the next postback, usually when the user ticks the next radio button, the gridview shifts up and the user's curser is pointed to a different line of the grid. This is also annoying users.
Solutions? In my limited experience I have 2 alternatives maybe 3 (below). The reason I did not do either was because I wanted data to update the database as soon as it was typed or ticked. Since the radio buttons cause an immediate update I didn't want the users to inadvertently think the textboxes did too and forget to click a button to commit the text. Since entering text in the textboxes is optional, I don't have a way to validate if the user is done completing the form and remind them to click a button to commit their text input.
Put a button by each textbox to commit the text
Use one button to commit all textbox data when form is complete
Find a way to put a placeholder where the confirmation label is when its hidden so it doesn't shift on postback.
I'm starting to think one Submit button is the way to go and let the user think that is what saves the data? Simple.
At any rate is there a better way to achieve my goals of having input updated in database immediately without annoying delays (postbacks) and grid shifting at inopportune times?
In my case I changed the textbox AutoPostBack property to False and removed all code in the text_changed() event for all textboxes. I am committing the text when a Submit button is clicked. (The radio buttons still commit to db immediately when selected). In the end, I think users are conditioned to click a submit or save button when they finish filling out a form anyway.

Must click a button twice to fire an event

I have a data grid that is created dynamically to display information retrieved from a database. The data grid consists of multiple bound columns and two button columns for each row, a view and a delete button.
Clicking either button calls the Page_Load() method with Page.IsPostBack as false.
if (!Page.IsPostBack)
{
this.bindForm(); // Populates drop down lists that the user can select
setAccess(false); // Sets access for the page
// Stuff commented out to avoid confusion
}
else
{
bindDynamicGrids(); // Create the data grid(s) and populate them
}
So when I first click the button the above code gets called with IsPostBack false, and the dynamic grids get bound. But, the button click event does not fire. When I click the button a second time, Page_Load is called with IsPostBack false, and after executing bindDynamicGrids() the button click event is fired. I cannot understand the difference between the first and second click.
I have read a few threads;
ASP.NET C#, need to press a button twice to make something happen
http://forums.asp.net/t/1783694.aspx?ASP+NET+Button+needs+to+be+clicked+twice
To attempt to understand the issue, but I must be missing something. From what I am gathering from the second link, a session variable may be getting set in the click event, which is also being set in Page_Load, and this is all an issue with ordering. If that is the case I am not seeing where it is happening.
When it is not a post back the bindForm() method is called, which populates all drop down lists. The edit click event populates those drop downs with the values from the row, but the click event is always a post back and the form has already been bound.
I have also considered having a script automatically double click one of the buttons anytime the user single clicks, but I have not been able to find an "OnClick" property for the column button. Any help that could be provided would be phenomenal.
The thing is, that the button click event happens after the Page_Load event meaning that the filtering does not get applied on the first postback. It has been updated on the second postback and you see the filtering.
You can try to move the code of your page_load event to OnPreRender so the reload happens after the button click event.
for more information look here: Button needs to be clicked twice
and here

How can I load data when I click in another box?

I have an autocomplete on my page where I use it as a search. I want to be able to select an item that appears in the autocomplete and then when the user clicks another box it will do a postback and if there is data for the one selected, it will populate the other fields for that chosen selection.
So what I am asking, is how would I go about doing a postback when the user clicks into another textbox on the screen?
Hi you could use event from textbox (TextChanged) inside this event put your code to verify if this data is correct. Don't forget put property AutoPostBack from textbox in True. This action only can be use if the data in textbox change if for example you put
My Dog
and data not exists if you change focus nothing will happen until you change the data for example
My Cat

Dynamically Created User Control's Dropbox Items from Database resets on postback

First off, I have managed to create a web application where my dynamically created user controls are recreated and repopulated with the correct information upon postback. I am not sure what my problem is, but i hope that you will be able to help me figure it out based on my situation:
On my page i enter the number of controls to be created into a hardcoded textbox (its on the aspx page) and click the okay butten. This in turn, creates the specified number of user controls dynamically using c# in the background.
So far the desired number of dynamic controls are in a table on the page.
Next...
I have 1 textbox and 4 dropboxes on each dynamic user control. When i type a company name into the textbox field and press enter or click away (on text changed event) it autoposts back and the textbox retains the company name that i have typed in.
Based on this string the dropboxes are populated from the database. Now when i select the desired items from the dropboxes and click on the save button (located outside of the dynamic controls, on the page) it does an insert to the database, but it turns out that upon this postback the indexes from the dropboxes have been reset and the wrong values get inserted.
The following pictures show firstly, how it should be and then how it is.
Basically the company name remains in the textbox of the dynamic control, but the information i choose from the dropbox resets to the first index.
It's hard to tell what happend without code, but this is a common mistake:
If you fill/create the dropdownlist controls in the page load event and you post back, the code will refill/recreate the controls. That's why you have to use something like If(!IsPostBack) in your page load event. Otherwise it will execute that code everytime you do a postback and actually just want to execute the code in your event handler for that button.
If you're dynamically creating the controls, make sure to do that in the Page_Init event. Dynamic controls have to be recreated on every postback. Their state is restored after the Page_Init (if it is a postback), so make sure to only set their values in Page_Load if you want to overwrite them.

Categories

Resources