I am trying to pass the value of a variable from javascript to C#.
Here is the client side part:
<input type="text" value="1" id="h" name="n" runat="server" />
<script>
$("a").on("tap", function (event) {
var a = event.target.id;
$("#h").val(a);
});
</script>
and the server side part:
<%
string param = h.Value;
Response.Write(param);
%>
The input field has an initial value=1. A JQuery event changes the value attribute to, let's say, 5. When I try to access the value of input from server side (h.Value), the value is again, 1.
What am I doing wrong?
Thank you.
I beleive you need to use an ASP.NET TextBox control instead of a plain html input.
Related
I would like to include a global javascript variable that it is dynamically updated into the argument of an action form.
Previously, I was simply using django template syntax to include the django variable string returned by the view into the action form but now that it is updated I need to come up with a way to pass this updated javascript variable into the GET method.
I save the variable as a window property "window.subgraph_query = 'foo'" this variable is supposed to change over time so it needs to be updated also in the form argument as well. How can I reach this? I think it should be easy
Thank you!
<script type="text/javascript">
// BEFORE
var subgraph_query = "{{ subgraph_query }}";
//NOW
window.subgraph_query = "{{ subgraph_query }}";
</script>
// BEFORE
<form action="/graph/network_to_gml/{{ subgraph_query }}" method="get">
<input name="export-node-list" id="export-node-list"type="text"/>
</form>
// NOW
<form action="/graph/network_to_gml/JAVASCRIPT_VARIABLE" method="get">
<input name="export-node-list" id="export-node-list"type="text"/>
</form>
You need to add a hidden input field to your form and assign a value of a js variable. this way it won't be visible for a user but your script will receive it.
Example of a hidden field type:
<input type="hidden" name="country" id='hidden1' value="">
and in JS:
document.getElementById('hidden1').value = 'Poland';
I have an asp list item with a text box. I need to pass the text box value to my .aspx.cs file.This is the code. I created the hidden field control, but this control is not taking the javascript variable value in server side code. Please help.
<asp:ListItem Value=">1000"> <input type="text" id="tbGreater" onkeypress="return isNumberKey(event)"><br/> </asp:ListItem>`
Use ClientID to access server control in javascript. Javascript function should be
function SetHdnField() {
document.getElementById('<%= HiddenField1.ClientID %>').value = document.getElementById("tbGreater").value;
}
Reaon is HiddenField1 is a server side control and it's id is changed during rendering.
I have the need to create a hidden field in of my table. Later upon button click, i need to retrieve the data saved in the hidden field. Below is the code:
<td>
<input id="hdnr<%=RowNumber%>c<%=ColumnNumber%>" type="hidden" value="{{Html.HiddenFor(model => item.Key)}}" />
</td>
Where rownumber and colnumber are variables.
Later in jquery, upon button click, I am attempting to retrieve the value of the hidden field as per the code below:
var value = $('#hdnr'+i+'c'+j).val();
alert(value);
Somewhere something is wrong. Either the value is not getting saved in the html tag or it is not being retrieved correctly.
Please help.
Thanks in advance.
Your value in the input is not correct. You can put the value directly from the model.
<input id="hdnr<%=RowNumber%>c<%=ColumnNumber%>" type="hidden" value="<% model.Key %>" />
if you want to use the helper, but in this case the id will be generated automatically. :
Html.HiddenFor(model => item.Key)
You can still add custom attribute. I'm not sure of the syntax :
Html.HiddenFor(model => item.Key, { #class = "myClass" })
jQuery
$(document).ready(function () {
$('#ButtonRadioValue').click(function () {
var selValue = $('input[name=radio]:checked').val();
});
});
Radio
<div id="radio" align="center">
<input type="radio" id="radio1" value="BtnNextCaseClick" name="radio" checked="checked"/><label for="radio1" style="width: 109px; margin-right:-.9em">Next</label>
<input type="radio" id="radio2" value="CloseCase" name="radio" /><label for="radio2" style="margin-right:-.9em">Close Case</label>
<input type="radio" id="radio3" value="CloseWeb" name="radio" /><label for="radio3" style="width: 109px">Close Web</label>
</div>
So, below that radio I have a button, and I want it to execute different code behind according to radio selection. That value is stored in selValue. So how can I tell onclick event to run what's inside of selValue
Button
<asp:Button ID="ButtonRadioValue" CssClass="customButton" runat="server" onclick=" js variable here" Text="Aceptar" style="width: 111px; height: 30px"/>
Thanks.
EDIT
Since there was no real need for client-side procedure. I did it from Server side with this.radio1.Checked == true
Update: It appears I have misunderstood what is required, so I will leave my original below
If you want to know which radio button was selected on the post-back, then either make the radio's server-side controls (either by converting into <asp:RadioButton> or simply adding runat="server" to them. Then you can test for radio1.Checked, etc.
Otherwise you can check the form data using Request.Form("radio1"). If result is Nothing (VB.Net) or null (C#) then the radio was not selected. If there is a value there, you know the radio is selected.
Original / incorrect assumption
Unless I've misunderstood the issue, you don't need to have a special attribute in the <asp:Button> as you can do this just in the jQuery you've already written.
Add the following after your var selValue...
$("#" + selValue).trigger("click");
To produce...
$(document).ready(function () {
$('#ButtonRadioValue').click(function () {
var selValue = $('input[name=radio]:checked').val();
$("#" + selValue).trigger("click");
});
});
One thing to note is that if the control is within Master Page, Placeholder, etc, then the button will need the full ClientID, otherwise the jQuery will not find it
As a side note, the OnClick attribute of the <asp:Button> is used to wire-up the event handler for the click on a post-back to the server. If you want to run javascript then you want to use the OnClientClick attribute, which will produce an onclick attribute in the rendered HTML
It sounds like you are looking for eval().
var selValue = $('input[name=radio]:checked').val();
eval(selValue);
The Click is a server event. If you want JS code to execute use onclientclick.
You can try with OnClientClick event in order to call client function
Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.button.onclientclick(v=vs.80).aspx
Server Side : OnClick
Client Side : OnClientClick
i need help on html form.
I got a javascript variable, and I am trying to pass the variable to a html form texbox. I want to display the variable on the textbox dynamically. but i do not know how to pass the variable to the html form and call the variable?
var test;
<INPUT TYPE="TEXT" NAME="lg" VALUE="" SIZE="25" MAXLENGTH="50" disabled="disabled"><BR><BR>
How do i pass test to html form and change its value?
Thanks
Pass the variable to the form element like this
your form element
<input type="text" id="mytext">
javascript
var test = "Hello";
document.getElementById("mytext").value = test;//Now you get the js variable inside your form element
<form name="input" action="some.php" method="post">
<input type="text" name="user" id="mytext">
<input type="submit" value="Submit">
</form>
<script>
var w = someValue;
document.getElementById("mytext").value = w;
</script>
//php on some.php page
echo $_POST['user'];
instead of
document.getElementById("txtBillingGroupName").value = groupName;
You can use
$("#txtBillingGroupName").val(groupName);
instead of groupName you can pass string value like "Group1"
This was a problem for me, too. One reason for doing this (in my case) was that I needed to convert a client-side event (a javascript variable being modified) to a server-side variable (for that variable to be used in php). Hence populating a form with a javascript variable (eg a sessionStorage key/value) and converting it to a $_POST variable.
<form name='formName'>
<input name='inputName'>
</form>
<script>
document.formName.inputName.value=var
</script>
You could also use to localStorage feature of HTML5 to store your test value and then access it at any other point in your website by using the localStorage.getItem() method. To see how this works you should look at the w3schools explanation or the explanation from the Opera Developer website. Hope this helps.
document.getElementById("txtBillingGroupName").value = groupName;