Reputation: 1344
I have a hidden field that I am updating via Javascript once I click a button, but when I try to access it on the code behind there is no value until I click the button the second time. I am able to see the hidden field value when I inspect it via the browser.
Default.aspx
<script type="text/javascript">
function LoadHtml(inputState, inputStateAbbr, inputProgramType, inputHealthCenter, inputCity) {
$.ajax({
url: omitted,
type: "POST",
async: false,
data: {
state: inputState,
stateAbbr: inputStateAbbr,
programType: inputProgramType,
healthCenter: inputHealthCenter,
city: inputCity
},
success: function(result) {
document.getElementById('DataHiddenField').value = result;
},
error: function (jqXHR, textStatus, errorThrown) {
//omitted
}
});
}
</script>
<asp:Button ID="Button1" runat="server" OnClick="Button1_OnClick" CssClass="top-buffer" Text="Compare Sites" />
<asp:HiddenField ID="DataHiddenField" runat="server" ClientIDMode="Static" />
Code Behind
protected void Button1_OnClick(object sender, EventArgs e)
{
RetrieveHtml();
}
private string RetrieveHtml(){
Page.ClientScript.RegisterStartupScript(this.GetType(), "MyKey1", "LoadHtml('Alabama', 'AL', 'Program Awardee Data', 'Alabama Regional Medical Services', 'Birmingham');", true);
return DataHiddenField.Value;
}
Upvotes: 0
Views: 392
Reputation: 19772
You seem to have a fundamental misunderstanding on how web pages and asp.net webforms, specifically, work. Generally when a form posts a form to a server, a request for a new page is made, the server does some work with the form variables and sends a new page as a response. There is a disconect between client side and server side at this point.
Let's dissect your code:
<!-- Causes a postback to the server, no javascript run yet -->
<asp:Button ID="Button1" runat="server" OnClick="Button1_OnClick" CssClass="top-buffer" Text="Compare Sites" />
Code Behind
private string RetrieveHtml(){
/*Tells the page to run this script - WHEN IT NEXT LOADS*/
Page.ClientScript.RegisterStartupScript(this.GetType(), "MyKey1", "LoadHtml('Alabama', 'AL', 'Program Awardee Data', 'Alabama Regional Medical Services', 'Birmingham');", true);
/*Gets the value from the hidden field.*/
/*On first click the above java-script HAS NOT RUN*/
return DataHiddenField.Value;
}
/*After the server has finished work, it sends a new page response.*/
/*THEN the javascript runs*/
So what do you need to do?
Upvotes: 1
Reputation: 2264
The reason that you can't see the content of hidden field on the first button click is beacause your hidden field content is filled on the client side after you click the button. Ex:
1º Client Side: User clicks on Button1
2º Server side: Method RetrieveHtml sends "order" to client side to run LoadHtml method with provided parameters. At this moment DataHiddenField.Value is not visible in server side because LoadHtml wasn't executed yet.
3º Client Side: Executes LoadHtml that calls Server Side on omitted url.
4º Server Side: Execute code in omitted url and returns content to client side.
5º Client Side: Success function adds content at DataHiddenField. Only now you will be able to se this content on server side.
Obs: DataHiddenField.Value will always be related to the last execution inside RetrieveHtml method.
Are you able to see the hidden field value on inspect it at the browser before clock on Button1?
Upvotes: 0