How to pass data from .ASPX to .ASPX.CS (C#)? - c#

i'm working in a webform page and i want to pass data from the "Client-Side" (ASPX) a value returned by jquery event to the "code behind" (ASPX.CS).
Thanks.

You can save the value in a hidden field and access to it from the server:
or
Set value of hidden field in a form using jQuery's ".val()" doesn't work

Just set the value to an input field like normal. When the page posts to the code behind, the value will be resident.
Example
HTML Markup:
<asp:Textbox id="myTextbox" runat="server" ClientIDMode="Static"/>
jQuery:
$('#myTextbox').val('myVal');
Again, when the form is posted, the value will be sent..

Related

google map parameter not showing

I have textbox in asp.net where i am writting Address.
On linkbutton below i wanted to direct it to goole map.
I have below textbox:
<asp:TextBox ID="txtJobAddress" runat="server" TextMode="MultiLine" Width="95%" Height="20px"
onBlur="javascript:saveChanges('JobAddress');"></asp:TextBox>
Linkbutton as below:
<a href="http://maps.google.com/maps?q=<%=txtJobAddress.Text%>" target="_blank">
<img src="images/gmap_button.gif" alt="Map" />
</a>
but when its getting directed through anchor tag, its not catching "q" parameter.
Its giving as:
https://www.google.com/maps/preview?q=
why its not taking value of:
<%=txtJobAddress.Text%>
Please help me , how can i attach textbox string here in
http://maps.google.com/maps?q=<%=txtJobAddress.Text%>
as below
http://maps.google.com/maps?q=NewYork
The <%=txtJobAddress.Text%> will be empty on page load. Presumably your saveChanges method prompts an ajax call to save the "JobAddress"? If this is the case then a full page postback won't occur, and therefore the <%=txtJobAddress.Text%> isn't populated with the updated value as this would occur on the server. You'll want to populate the query string value client side
e.g. in your blur event you could add a call to a javascript function that updates your link:
onBlur="javascript:saveChanges('JobAddress'); updateLink()"
in your updateLink function you would update the link query string value

asp:button with no post back or refreshing the page

I have button in my page that when click on fires a jquery method that shows a div, but if the browser doesn't support javascript I want to fire a code behind method (using a postback). Is this possible? I'm already using this code but i can't see any results:
<asp:Button ID="Button1" runat="server" Text="Upload Files Here" Width="187px" onClick="CodeBehindMethod" OnClientClick="show_upload_box();return false"/>
and this is my jquery code:
function show_upload_box() {
$("#pop_up_background").css({
"opacity": "0.4"
});
$("#pop_up_background").fadeIn("slow");
$("#signup_pop_up_box").fadeIn('slow');
window.scroll(0, 0);
}
If the browser doesn't support JavaScript then your button would not work.
In ASP.NET, the runat="server" instruction is actually tied to a JavaScript method that submits a form (view page source to see that method).
Change or remove the value of that property to prevent the post back to your server.

Store innerhtml by jQuery in asp.net?

I have
<div id="ulAndil" runat="server">
</div>
and button
<asp:Button ID="btnAssignJudToCourse" runat="server" Text="تاكيد "
CssClass="button" CausesValidation="false"
OnClientClick="javascript:getShape();"
OnClick="btnAssignJudToCourse_Click" Visible="false" />
At runtime I create a lot of sortable html controls and then click confirm
I want to save inner html of this div before postback of button in session or anything
Using jQuery so when user click the button jQuery take inner html of div and stores it in cookie session any thing to store it after postback I can retrieve it
Please I want the exact code for this jQuery or javascript method and how to call on button
Why don't you put the generated HTML in a hidden field and postback.
Say getShape() is your javascript function
function getShape()
{
//your validations applied
document.getElementById('hdnHTML').value = document.getElementById('ulAndil').value;
return true;
}
where hdnHTML is the hidden element of HTML.

How to store a value during partial postback inside update panel

I have a ajax file upload inside my update panel to upload the file immediately after the user selects a file to upload. During the file upload in btnVidUpload_Click I assign the file name to the hidden field which is also inside the update panel. Then when I click Submit Send_Click tries to get the value from the hidden field, but the hidden field is empty, why is the hidden field losing its value, cant it retain the value during post back or is there a way to store the value without using session. thanks
<asp:AsyncFileUpload ID="AFU_Video" OnUploadedComplete="btnVidUpload_Click"
runat="server" UploaderStyle="Traditional" ThrobberID="aajaxLoader" />
<asp:HiddenField ID="Hidd_VideoLoc" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:ImageButton ID="btn_Send" Text="Submit" runat="server" OnClick="Send_Click" />
The AsyncFileUpload internally uses an iFrame to render the FileUpload control.
That is the reason why it seems to lose it's value.
You need to use the Session in btnVidUpload_Click.
Own answer on a similar question: https://stackoverflow.com/a/8495986/284240

ASP.NET How to read HTML Form Element?

I have an ASP.NET web form where I have an hidden field, like this:
<form id="form1" runat="server" action="http://localhost/fa/Default.aspx">
<div>
<input id="requestData" type="hidden" name="requestData" value="" runat="server" />
<asp:Button ID="btnPOST" Text="POST" runat="server" OnClick="do_POST" />
</div>
</form>
On the method do_POST I have this:
protected void do_POST(object sender, EventArgs e)
{
//requestDataField is of the type protected global::System.Web.UI.HtmlControls.HtmlInputHidden requestData;
requestDataField.Text = "FOO!";
}
When I submit the form (by pressing the button), it goes to the server (an handler) wheer I have this:
string requestData = context.Request.Form["requestData"];
I get an empty string..
But if I assign a value like this:
<input id="requestData" type="hidden" name="requestData" value="FOO" runat="server" />
I get the "FOO"
What am I missing?
The reason why it's not doing it is because the method is called after the page has been post back. Meaning, it is actually working if you change .Text to .Value unfortunately by that time you have already read your form and it was an empty value. I remember working on a project where you could tell your form not to submit until a function has been run (but it was with a javascript that needed to run an complete before aspx submitted). You should try to see if there is a way to force your form to run your function BEFORE doing the postback.
Your do_POST method runs on the server, not on the client, and so is setting the value of the server-side object which represents the <input> control. Your context.Request.Form["requestData"] gets the value of the field from the client side data submitted in the POST request, which was never set, so it is blank.
If you want the onClick to be a client-side function, then you need to do it a little differently. Use the OnClientClick attribute (instead of onClick). Then create a javascript method to set the field value:
<asp:Button ID="btnPOST" Text="POST" runat="server" OnClientClick="do_POST" />
<script>
function do_POST() {
document.getElementById("requestData").value = "FOO!";
}
</script>
I tried your code and did few changes to it.
Change requestDataField.Text = "FOO!"; to requestData.Value = "FOO";
Also I added two buttons. One for do_POST function and the UseSubmitBehaviour property is set as False. The other one was to submit the form.
If you want to set it on client side then you will have to use Javascript.
Use "Value" instead of "Text" property for HtmlInputHidden control:
requestDataField.Value = "FOO!";
instead of
requestDataField.Text = "FOO!";

Categories