Linking Value from Javascript to .cs file - javascript

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.

Related

html textbox funtion onkey_Up is not working on ascx control

i tried onkey_up to copy one textbos data to another textbox on an user control ascx file but when i run its not wroking.is it because of i wrote onkey_up function on .ascx , i should write on master page?
function sync() {
var TextBoxA1 = document.getElementById('TestAmountTextBox');
var TextBoxA2 = document.getElementById('TestAmountTextBox_2');
TextBoxA2.value = TextBoxA1.value;
}
html inpoutbox :
<input type="text" runat="server" id="TestAmountTextBox" class="form-control currency-input" placeholder="From " aria-label="From Transaction Amount" data-allownegative="true" style="width: 100px;" maxlength="14" tabindex="7" onkeyup="sync()"/>
<input type="text" runat="server" id="TestAmountTextBox_2" class="form-control currency-input" placeholder="To " aria-label="To Transaction Amount" data-allownegative="true" style="width: 100px;" maxlength="14" tabindex="13" />
By default, adding a textbox to a usercontrol will force ASP.NET to render a unique ID. Something like "usercontrolId_textboxId". You can find the actual ID, by viewing HTML source.
There are a few options to get around ASP.NET generated IDs:
Option 1:
You can turn off ASP.NET autogenerated IDs, by setting ClientIDMode="Static" E.g:
<input type="text"
runat="server"
id="TestAmountTextBox"
ClientIDMode="Static"
... [the rest of the attributes]
There are alternative properties in ClientIDMode that can be used. Also it can be turned off completely in web.config.
If ClientIDMode is set to Static, then ASP.NET will throw an error if the same ID is reused. For example using the usercontrol on more than once on the same page.
Option 2:
Bind the ASP.NET ID to the JavaScript code, using ClientID, For example:
function sync() {
var TextBoxA1 = document.getElementById('<%= TestAmountTextBox.ClientID %>');
var TextBoxA2 = document.getElementById('<%= TestAmountTextBox_2.ClientID %>');
TextBoxA2.value = TextBoxA1.value;
}
This will only work if the JavaScript is placed on the usercontrol.
Option 3:
A third option is to replace getElementById with getElementsByClassName and add another class to the textboxes.

Set selected value on html select without using runat=server

I'm using this iconpicker plugin to let the user choose an icon and save it on a database. After that, when user go back on the page, I want to load the saved icon as selected value of the dropdown.
I can't use runat=server on the select because if I do the plugin stop working.
Select code:
<select id="ddlIcona" name="ddlIcona" class="myselect">
<option value="">No icon</option>
<option>icon-user</option>
<option>icon-search</option>
<option>icon-heart</option>
<option>icon-star</option>
<option>icon-users</option>
<option>icon-camera</option>
</select>
I don't bother using hidden field to pass value to the server.
How can I do this? Thank you
Aspnet renames the ID of a Control, usually when using Master Pages or Controls like Repeater, Gridview etc.
So a control that looks like this:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
Can have an ID of PlaceHolder1_Label1 in HTML.
For that reason ClientID is usually used to make sure the correct ID is referenced in javascript.
<script>
alert('<%= Label1.ClientID %>');
</script>

Issue passing variable from javascript to c#

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.

Maintain value after Postback using jquery?

I have a select list
<select id="type" name="type" onchange="type_Change(this.id)">
and i want to maintain selected value after the postback.
how will i acheive through jquery.
please anyone???
If you change the selected value of select through javascript/jQuery code then you can store the changed selected value in some hidden field and later using it in code behind.
Html
<input type="hidden" runat="server" id="hdnForSelect" />
Javascript
function type_Change(idOfSelect)
{
//your code
document.getElementById('%= hdnForSelect.ClientID %>').value = "changed value";
}
Code behind
string changedValue = hdnForSelect.Value;

django - getting value from a field which is hidden in html and using it in views file

I am working in django and i want to set a value to a hidden field in html and use it in django view file
html file
<input type="hidden" id="t" name="t" />
<script>
document.getElementById('t').value = $('#SelectBox').selectit('t').join(', ');
</script>
My views.py is :
pos= request.POST.get('t')
this is how i am calling in the view file.
I also tried using
pos=request.POST('t')
but it does not seems to work. How can i do it? Am i doing it wrong?
Thanks in advance
In your html file, you have written java-script without any event so how it will give any value to the hidden input field.
at the time of submitting Form, if there is not any value in your hidden input then it will not come in your view file and you will not get this "t" value.
so first check at the time of event function, value is coming in your hidden input field then you can submit form and u will get t value in view file.

Categories