I had created a checkbox at the server during runtime, and populated it in a table on the webpage, and I would like to make use of it to trigger some JavaScript to handle some functions in the client.
However, I'm not sure of how to trigger the function using a checkbox created in this manner.
I understand that I can trigger the function when I declare a checkbox in this syntax.
asp:CheckBox ID="Checkbox1" runat="server" Onclick="javascript:myfunction()"
However, this checkbox may or may not be required to be created on the webpage, and hence I'm unable to insert this hardcoded on the webpage
Is there any way which I can use to detect the status of this checkbox and trigger my JavaScript in the client? or if I can handle what I need to do at code_behind?
suppose that we are adding CheckBox to td of an table
<table>
<tr>
<td id="td1" runat="server">
</td>
</tr>
</table>
and in the code behind,
CheckBox chkbox = new CheckBox();
chkbox.ID = "CheckBox1";
chkbox.Attributes.Add("onclick", "click_Func(this);");
td1.Controls.Add(chkbox);
and javascript will be look like this ,
<script type="text/javascript">
function click_Func(chkbox) {
alert(chkbox.id);
}
</script>
you can add a script tag on the page like this
<script type="text/javascript">
$('#<%=Checkbox1.ClientID%>').live('changed',function(){
// Do your work here.
});
</script>
Related
This isn't working:
My table is generated dynamically using PHP, so each row's edit button has a unique id, and a unique inline jQuery function. That jQuery function will load a jQuery modal dialog for the user to modify the row's data (triggering AJAX to update the database and reload a new tbody into the table with the updated data).
In practice, the button doesn't respond. I've put a simple alert into the function, but it also doesn't work.
(I'm using the $(#...).dialog('open') idiom elsewhere on the same page, and it works well.)
Here is the code:
<tr>
<td>ADT Corporation Common Stock</td>
<td>adt</td>
<td>$31.59</td>
<td>$-0.78</td>
<td>-2.47%</td>
<td>4</td>
<td>FidelityIndividual</td>
<td>4</td>
<td>
<script>
$("#edit199").click(function(e){
alert("in the <script> function"); // doesn't work!
//alert( $("input[name='editfields199']").val() ); // really doesn't work!
e.preventDefault();
$('#the_edit_form').dialog('open'); // should open a nice modal pop-up
});
</script>
// The edit icon that the user can click in order to edit that row/record.
<input type="image" name="editfields199" id="edit199" src="images/gtk_edit.png"
value="ADT Corporation Common Stock^adt^4^FidelityIndividual^199" />
</td>
</tr>
<tr>
....more like the previous one...
<tr>
I've a webapp using hibernate to mysql. The webapp works fine, if my database gets new values they appear instantly on the webapp after refresh of the page. I want to get away from my "refresh-the-whole-page-every-five-seconds" build and have a checkbox that when activated the list in tbody will refresh alone. I've tried autorefresh on the div as well, but yeah, if anyone of you guys know this please help! thank you!
Code block 1 (head):
<script type="text/javascript">
function auto(){setInterval(
function () { $('#doRefresh'). ????
;}, 2000); } </script>
Code block 2 (div 1):
<input type='button' value='UPDATE' class="btn" onclick="auto()"/>
Code block 3 (div 2):
<tbody id="doRefresh">
<c:forEach var="sensor" items="${listSensors}" varStatus="status">
<tr class="listData">
<td>${sensor.fileName}</td>
<td>${sensor.date}</td>
<td>${sensor.time}</td>
<td>${sensor.channel1}</td>
<td>${sensor.channel2}</td>
<td>${sensor.channel3}</td>
<td>${sensor.channel4}</td>
<td>${sensor.channel5}</td>
<td>${sensor.channel6}</td>
<td>${sensor.channel7}</td>
<td>${sensor.channel8}</td>
</tr>
</c:forEach>
</tbody>
I have been to the following pages for inspiration (and more):
1. https://wowjava.wordpress.com/2010/02/06/auto-refreshing-the-content-
without-reloading-page-using-jquery/
2. http://www.sitepoint.com/auto-refresh-div-content-jquery-ajax/
3. http://crunchify.com/how-to-refresh-div-content-without-reloading-page-
using-jquery-and-ajax/
4. http://stackoverflow.com/questions/5987802/refreshing-only-one-div-in-
page-with-just-regular-javascript
Thank you
Since you already have server code that can be used to generate the html you can use the simplest jQuery ajax method which is load().
Method replaces all the html within the selector with new html from server by simply passing in a url.
/* within interval function */
$('#doRefresh').load('path/to/server/script', function(){
/* new html has been inserted, can run any html related code here*/
});
Since the <tbody> doesn't get replaced, only the rows, just send back rows. Otherwise use $('#doRefresh').parent().load(... to keep <tbody>
Reference: load() Docs
I have a page with a table, bound with Knockout inside an UpdatePanel. My goal is to get the binding successfully applied after a postback. Everything works as expected on initial page load, so the model is working.
I'm using pageLoad() to call a function that gets the model's JSON data from a HiddenField that was set server side. In the view I have each RadioButton's click data-bind set to a JS function outside the ViewModel that sets a hidden input field's value to the ID specified by the button, and then causes a server postback by clicking a hidden button client-side.
Inside pageLoad() I'm using Sys.WebForms.PageRequestManager.getInstance().add_endRequest to specify that the binding helper function should be called after the server-side of the postback is complete.
My problems:
After being set server-side, the HiddenField with the JSON data still has the same string as it did client-side when it returns to the client, even though when the server-side scriptlet is executed, I can see that the value updated correctly. Not sure what is going on here.
Even if the values don't update, BindingHelper() is called, although for some reason clicking on the RadioButtons no longer causes the event after one postback has already occurred.
Any insight into this would be greatly appreciated!
Code:
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(BindingHelper)
function pageLoad() {
if (!isPostBack()){
BindingHelper();
}
}
function BindingHelper() {
var data = <%= hfData.Value%>;
var model = new DataViewModel();
model.load(data);
ko.applyBindings(model);
}
function Select(id) {
document.getElementById('<%=hfSelectedID.ClientID%>').value = id;
document.getElementById('<%=rbSelect.ClientID%>').click();
}
function isPostBack() {
return document.getElementById('hfPostback').value == 'True';
}
</script>
HTML:
<table id="tblData">
<thead>
<tr>
<th></th>
<!-- ko foreach: subViewModel -->
<th><input type="radio" name="rbGroup" data-bind="value: id, checked: $root.selectedID, click: function() {Select(id);}" /><span data-bind="text: name"></span></th>
<!-- /ko -->
</tr>
</thead>
</table>
<asp:HiddenField ID="hfData" runat="server" />
<asp:HiddenField ID="hfSelectedID" runat="server" />
<input id="hfPostback" type="hidden" value="<%=Page.IsPostBack.ToString()%>" />
<div style="display:none">
<asp:RadioButton ID="rbSelect" runat ="server" AutoPostBack="true" />
</div>
Edit: I've since determined that this works if the binding is not inside an UpdatePanel, just by registering BindingHelper() as a startup script in Page_Load.
Oddly enough, even if I register the startup script by specifying the UpdatePanel, the results are the same. There are other controls in the UpdatePanel so I know the UpdatePanel is in fact, updating, and it isn't set to conditionally update.
I solved this. I ended up needing to find the HiddenField in the DOM and then parsing the JSON from that to get the data for the model. It seems weird to me that even after the value had been set server-side, it was keeping the old value, but the UpdatePanel complicates the lifecycle.
Also I changed the RadioButton that is clicked client-side to a LinkButton to get it to always force a postback to the server.
I am beginner to php and Javascript. i am facing an issue that is i've a page say page1 which has two input fields and a button(Go).While clicking 'Go' the page2 populates on same page (page1) which has a data table. i am trying to implement JS on that Table BUT JS scripts are not Executing.
(If i have a data table in a single page it works but upper scenario it does not).
Page1(Main page having inputs and button)
<script src="../javascript/file_history.js"></script>
<input name="advance_search" type="text" id="advance_search" size="26" />
<input name="button" type="button" onclick="showFileHistory()" value="Go" />
The Other Page having data table on ajax call through JS
<script src="../javascript/file_history.js"></script>
<script type="text/javascript" src="../javascript/gs_sortable.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script type="text/javascript">
var TSort_Data = new Array('tbl_filemgtview','s', 'i','f');
tsRegister();
alert('This should be pop up');
</script>
<table id="tbl_filemgtview">
<thead id="thead"> <tr class="viewpage_heading">
<th>Table heads </th></tr>
</thead></table>
What should be there? or the way of executing Script tags which are getting by pass in page 2?
Possibly Its because the elements previously were not in the DOM and when you click on the button the elements manipulate the DOM so events firing like
$("#test").click(function(){
// Do something
});
Wont work, Now to make it work you need to use
$(document).on("click", "#test", function(){
// Do something
});
it would be better if you provide us Code or jsfiddle
I have a asp:checkbox and a asp:textbox on my aspx page , I want to know show the textbox when the checkbox is checked and hide it if the checkbox is unckecked using javascript , I really apprecieatee anyone who could help me .
you can uee this code
<script type="text/javascript">
function radio_yes(){
if(document.getElementById("c1").checked==true)
document.getElementById("atext").style.visibility="visible";
else
document.getElementById("atext").style.visibility="hidden";}
</script>
<input type="checkbox" id = "c1" name="subscriptions" onclick = "radio_yes();radio_uncheck();" value="CorporateEmails"/>
Corporate Press Release Emails<br />
<input type="text" id="atext">
Assuming these render on the browser as a normal checkbox and textarea, then you can get notification of when the checkbox is ticked or unticked using its click event:
$("selector_for_the_checkbox").click(function() {
// ...handle the event here...
});
Within the event handler, this will refer to the raw checkbox element (not a jQuery wrapper for it).
To show a hidden element with jQuery, you use show; to hide one, you use hide. You can also use toggle to show or hide the element on the basis of its current state or a flag you pass in. So:
$("selector_for_the_checkbox").click(function() {
$("selector_for_the_textarea").toggle(this.checked);
});
It's well worth spending an hour (that's all it takes) reading through the jQuery API documentation beginning to end.
try this script:
<script type="text/javascript">
function toggleTextBox(bEnable, textBoxID) {
document.getElementById(textBoxID).disabled = !bEnable;
}
}
</script>
and your aspx would be like:
<asp:CheckBox ID="chkApplied" runat="server" onclick="javascript:toggleTextBox(this.checked, 'txtAmount'); />
<asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
and if your client side id differs, you can bind it from server side like this:
chkApplied.Attributes.Add("onclick", "javascript:toggleTextBox(this.checked, '" + txtAmount.ClientID + "');");