Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing Textbox value on postback

In a page I have a link; clicking on it opens a dialog and sets a textbox value for that dialog.

However, once I click on submit in that dialog, the textbox value is null.

Link:

<a href="#" onclick="javascript:expand('https://me.yahoo.com');
jQuery('#openiddialog').dialog('open'); return false;">
<img id="yahoo" class="spacehw" src="/Content/Images/spacer.gif" /></a>

Script:

<script type="text/javascript">
  jQuery(document).ready(function () {
    jQuery("#openiddialog").dialog({
        autoOpen: false,
        width: 600,
        modal: true,
        buttons: {
           "Cancel": function () {
              $(this).dialog("close");
           }
        }
    });
});
function expand(obj) {
    $("#<%=openIdBox.ClientID %>").val(obj);
}

Dialog:

<div id="openiddialog" title="Log in using OpenID">
<p>
    <asp:Label ID="Label1" runat="server" Text="OpenID Login" />
    <asp:TextBox ID="openIdBox" EnableViewState="true" runat="server" />
    <asp:JButton Icon="ui-icon-key" ID="loginButton" runat="server" Text="Authenticate" OnClick="loginButton_Click" />
    <asp:CustomValidator runat="server" ID="openidValidator" ErrorMessage="Invalid OpenID Identifier" ControlToValidate="openIdBox" EnableViewState="false" OnServerValidate="openidValidator_ServerValidate" />
    <br />
    <asp:Label ID="loginFailedLabel" runat="server" EnableViewState="False" Text="Login failed" Visible="False" />
    <asp:Label ID="loginCanceledLabel" runat="server" EnableViewState="False" Text="Login canceled" Visible="False" />
</p>
</div>
like image 388
stoic Avatar asked Jun 09 '10 07:06

stoic


People also ask

Why doesn't postback catch changes in textbox value?

If you're using an ASP.NET TextBox Control (for example), and it's READONLY or DISABLED, the postback won't catch the changed value. Per my issue, I was changing the value of the control thru javascript and even though the browser rendered the change, on the postback, the control still retained the original value.

Why postback is not working in textbox in GridView?

When PostBack happens i.e. when Button is clicked or any other control that does PostBack inside GridView, the GridView is again populated with data and hence the modified values in TextBox are lost. Solution to this problem is to wrap the GridView binding code inside the Not IsPostBack condition as shown in the example below.

Why are textbox values not being saved after page_load?

If you put code in your Page_Load event to set the values of the ASP.NET textboxes, the values that were posted back will not be saved, because Page_Load happens after the child control states are restored in the ASP.NET page lifecycle. The values are already restored by ASP.NET, but you are overwriting their restored values.

Why textboxes are not saved after submit button is clicked?

The initial values of these textboxes are retrieved from a database. When a user changes these values, they are not saved and the textboxes are cleared out after the submit button is clicked. Please help resolve this confusion. Thanks. thanks for your reply but it is still not working.....


1 Answers

I figured:

I have to add this line to append the dialog to the form, as jquery dialog appends to the body:

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

The entire script should now look like this:

<script type="text/javascript">
jQuery(document).ready(function () {
    jQuery("#openiddialog").dialog({
        autoOpen: false,
        width: 600,
        modal: true,
        buttons: { "Cancel": function () {
            $(this).dialog("close");
        }
        }
});
$("#openiddialog").parent().appendTo(jQuery("form:first"));
});
function expand(obj) {
    $("#<%=openIdBox.ClientID %>").val(obj);
}

like image 93
stoic Avatar answered Sep 30 '22 21:09

stoic