html textbox funtion onkey_Up is not working on ascx control - javascript

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.

Related

Linking Value from Javascript to .cs file

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.

Calendar.js fills textbox but ASP.net reads the textbox empty

I have a text box that is filled in with a date based on the Calendar.js file that I use (can be found at http://www.mattkruse.com/javascript/calendarpopup/source.html)
The user clicks on the image button, which pops open a calendar and then fills the textbox.
It clearly works when I use it; however, when I debug my VB.net coding in Visual Studios the textbox shows empty:
Any suggestions why this is happening? I will need to read the string in the textbox later. I need to somehow convert the text that is filled from the Calander.js file and save it as a string inside the textbox.
<script type="text/javascript" id="js1">
var cal1 = new CalendarPopup();
</script>
HTML:
<td style="padding-top:20px; padding-left:15px;">
<asp:TextBox runat="server" id ="CERT_AGENCY_EXP1" Cssclass="fieldrequired" name="Agency1 Date Txt" size="10" maxlength="10" BackColor="#CCCCCC" ReadOnly="True" TabIndex="40"/>
<asp:ImageButton name="anchor1" id="anchor1" runat="server" style="padding-top:30px; float:left;" value=""/>
<img src="Images/calendar.png" value=""
onClick="cal1.select(document.forms[0].CERT_AGENCY_EXP1,'anchor1','MM/dd/yyyy'); return false;" title="cal1.select(document.forms[0].CERT_AGENCY_EXP1,'anchor1','MM/dd/yyyy'); return false;" name="anchor1" id="a1" />
</td>
Your Textbox (CERT_AGENCY_EXP1) is marked as ReadOnly. Remove that attribute and you will see the changes after the postback.
If you still want to keep the Textbox as ReadOnly, but still get the modified values do this:
CERT_AGENCY_EXP1.Attributes.Add("readonly", "readonly")

runat="server" breaks my jQuery - datapicker

The runat="server" is breaking my jquery. I have two input, but for testing purpose added runat="server" in only one of those. Actually , I need to add on both.
Below you can find JS script to trigger the datetimepicker:
note: dateTo has runat="server" set and tried to change the way JS trying to get its ID, but still not working.
<script>
$(function(){
$("#dateFrom").datetimepicker();
$("#<%=dateTo%>").datetimepicker();
});
</script>
Here you can find the HTML input using runat="server" or not into asp.net code.
<tr>
<td>
<input type="text" id="dateFrom" name="dateFrom" value="" class="dateFrom" />
</td>
<td >
<input type="text" id="dateTo" name="dateTo" runat="server" value="" class="dateTo" />
</td>
</tr>
Does anybody has any idea,hint.....?
thank you
Use ClientID to get the generated Id of a server side control:
$("#<%=dateTo.ClientID%>").datetimepicker();
ASP.NET will generate a specific id attribute that is different from the id attribute of the server side control, so you need to use the generated value in order to access it from jQuery.
Since you're supplying class names, I suggest simply using those.
$(function(){
$(".dateTo, .dateFrom").datetimepicker();
});
Alternatively, you could give any "date" field on your form a class of "date" and use that:
$(function(){
$(".date").datetimepicker();
});
This is a common pattern for client-side validation, and also allows you to provide context clues through styling with CSS.
If you're using .NET 4 you can set the ClientIDMode attribute to Static. This will prevent the framework from changing the element's ID:
<input type="text" id="dateTo" name="dateTo" runat="server"
ClientIDMode="Static" class="dateTo" value="" />
ASP.NET will treat the inputs as server-side controls when runat=server is added, and this will result in transformed identifiers of those inputs so that they start with a container prefix (something like ctl00_), hence 'breaking' your jQuery selectors.
If you're using .NET 4 then you can disable these transformations by altering the ClientIDMode. Otherwise you will need to include the prefix in your selectors, or refactor your selectors to be independent of ID and somewhat distinct for selection by other means.
Use this to get correct client id for server controls
$('[id$=dateTo]').datetimepicker();

javascript not accessing asp .net based html page element

I'm just trying to access the value of a textbox control (which has the runat="server" attribute). Been stuck for hours looking for an answer. Every time I try to get the value, it is null.
The javascript file:
function validateRegistration() {
var username = document.getElementById("<%=username.ClientId%>").value;
document.write(username);
}
The aspx file:
<asp:Content ID="Content3" ContentPlaceHolderID="mainContentPlaceHolder" runat="server">
<p>
Fill in the following form to register:</p>
<p>
Username:
<asp:TextBox ID="username" name="username" runat="server"></asp:TextBox></p>
<input id="register" type="submit" value="Register" onclick="validateRegistration()"/>
</asp:Content>
Did you try using
var username = document.getElementById('<%= username.ClientID%>').value;?
i have similar code in my page and works fine.
It should be (note the capital D):
var username = document.getElementById("<%=username.ClientID%>").value;
document.write(username);

getting control ID inside repeater by Jquery

I have Entries I want to comment on them . So I created a Repeater with a TextBox and a
Button inside it . How can I get the id of the button and textbox for specific row by
Jquery ?
<ASP:REPEATER runat="server">
<ItemTemplate>
...
// Entries: bind data from DB
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" />
</ItemTemplate>
</ASP:REPEATER>
Thanks .
Your big problem is that your asp repeater control is going to generate ids like this ct100_repeater_txt1 which isn't going to help you get that comment associated with its corresponding db row
The common way I have found to accomplish this (which is a pretty standard way I believe) is to assign the elements id with the row id from the database, or if you have no control over the id, to create a custom attribute.
Pseudo code I have used looks something like this: (I'll try and make it language/platform agnostic so you can learn the concept, as opposed to only the language)
<repeater template>
<textbox id="txt1" dbid='<% eval database id>'/>
<button id="btn1" dbid='<% eval database id>'/>
</repeater template>
that should generate code that looks like:
<input type="textbox" value="" id="ct100_txt1" dbid="14" />
<input type="button value="submit" id="ct100_btn1" dbid="14" />
<input type="textbox" value="" id="ct100_txt2" dbid="12" />
<input type="button value="submit" id="ct100_btn2" dbid="12" />
<input type="textbox" value="" id="ct100_txt3" dbid="39" />
<input type="button value="submit" id="ct100_btn3" dbid="39" />
Then in whatever language you want you can read from that attribute:
Button.Click{
get attribute dbid of button clicked;
save text of textbox of same dbid to a database;
}
Many cheers! Hope that works for ya
EDIT
In response the the comment "where do id save the dbid?!" I'm assuming in a database :) How should you save it? There are lots of ways. Here's one you could try with jquery:
create a javascript function to save answers that takes a parameter. when you bind the dbid to the control, bind it into an onclick function and pass that id to the function. The function then gets the text where the dbid matches and saves the comment and id to a database.
<textbox dbid="14" />
<input onclick="doStuff(14)" />
<script>
function doStuff(var id){
var comment = $('textbox[dbid=id]').value();
ajax(your url and arguments);
};
</script>
Without more information this is the best I can offer you:
for html like this (which is how asp.net will render your button/textbox):
<div id="row1"><button type="button">Click Me!</button><input type="text" name="tb_info" /></div>
<div id="row2"><button type="button">Click Me again!</button><input type="text" name="tb_info" /></div>
You could select the button and textbox for specific row by using the following jQuery:
$("#row1 > button, #row1 > input")

Categories