ASP.Net Textbox loses value on button click

Associate
Joined
25 Feb 2007
Posts
905
Location
Midlands
Hi,

I have a textbox and a submit button in a hidden div (this div is shown when a different button is clicked).

With the above set up, when the submit button is pressed, the value of the textbox is set to "" when debugging the submit button onclick handler.

If the div isn't hidden, the submit button works as expected and the textbox retains it's value.

Any ideas how I can keep the textbox value with the hidden div? This is in C#.

Thanks,
Matt
 
Associate
OP
Joined
25 Feb 2007
Posts
905
Location
Midlands
OK, since posting this I've made some progress.

The div is a jQuery UI Dialog widget, with it's autoOpen option set to false when the page loads. The button then calls the dialog's Open() function, which then shows the div.

However, I've since found that the dialog appends itself to the body rather than the form (from this link) so I've added the following line after the dialog is initiated:

Code:
$("#dialog").parent().appendTo(jQuery("form:first"));

This resolves the problem of the textbox losing it's value.
 
Associate
OP
Joined
25 Feb 2007
Posts
905
Location
Midlands
OK so as a result of doing the above, if I set the 'modal' property to true, it disables all elements on the screen, so the dialog becomes disabled too!

Can anyone shed some light on how I can use the modal property without it disabling the dialog?
 

AJK

AJK

Associate
Joined
8 Sep 2009
Posts
1,722
Location
UK
Can anyone shed some light on how I can use the modal property without it disabling the dialog?

Leave the dialog where jQuery UI wants to put it! Moving the DOM position of the dialog involves also moving the modal blind (so that the dialog still appears in front of it) and changing the CSS for both the blind and dialog to ensure that they're still positioned correctly eg. the blind fills the page.

Better would be to come up with another way of posting the data back to .NET, for example having a hidden form field within your .NET form, the value of which you tie to the textbox in the dialog - the hidden field then posts back instead of the text box. Suitable?
 
Associate
OP
Joined
25 Feb 2007
Posts
905
Location
Midlands
I'm not sure that the hidden field would retain it's value either, why would that behave any differently than the textbox?

I'll give it a try, but if you could expand on your answer that would be great :D

Every solution I've seen to my initial problem was to move the dialog position in the DOM...
 
Associate
OP
Joined
25 Feb 2007
Posts
905
Location
Midlands
Ah I see what you mean, I was thinking that the HiddenField would be inside the Dialog too.

How would I set the value of the HiddenField to be the same as the Textbox? If I use the TextChanged handler for the TextBox, it doesn't fire, which I assume is because it's been moved by jQueryUI?
 
Last edited:
Associate
Joined
10 Nov 2013
Posts
1,808
Use an 'on submit' handler to assign the value of your textbox to your hiddenfield value before the form is submitted?
 

AJK

AJK

Associate
Joined
8 Sep 2009
Posts
1,722
Location
UK
Forget .NET handlers, just use JS. Insert something like this wherever you're setting up the jQuery UI dialog:

Code:
$("#textboxID").change(function() {
    $("#hiddenFieldID").val($(this).val());
});

You may be better using a CSS class for the hidden field selector, since .NET will use generated ClientIDs - same difference though.

The textbox doesn't even need to be a .NET control (unless you're populating it with some initial value server-side, which is fair enough).
 
Last edited:
Back
Top Bottom