Jquery onchange event not triggered - javascript

I have a drop down list inside a data grid in main page and upon changing values it should display popup and popup values are posted back to the main page. I have to trigger onchange event which is not working.
Below is the code I am using.
Server side:
ddlID.Attributes.Add("OnChange", "return Show("txtFName.text,txtLstName.text");
Client side:
$('#ddlID').val(6).trigger("change"); //should hit function Show() which is not triggered.

Finally I figured out. The control is inside data grid and when I am using the ID directly via jquery ($('#ddlID')), it is not working. I have to get the client side ID using $(""#"+ddlID.ClientID+"""). Also we can use class selector. I am triggering on change using a external js file.

Related

How to call a javascript function from code-behind of a partially loaded aspx page

I have an asp.net application and I use jquery's .load function to load content into a specific div like so:
$('#pnl').load('/Panel.aspx #panel', loadLogs());
From the Panel.aspx page I populate some dropdowns in the code-behind load event, and from there I have some javascript functions(in a separate javascript file) that display text files based on the content of the dropdowns, which is controlled by the change events on the dropdowns. I am having an issue of when the page is loaded, the dropdowns are populated but I need to actually click on one of them to get the files to display. I have tried to call the javascript functions from the load event of Panel.aspx with Page.ClientScript.RegisterClientScriptBlock and they are not firing. I have also tried firing it from window.onload or $(document).ready from the javascript file and it is still not working.
Any suggestions for how I can get this done?
try something like this
If you Used Update Panels Then You can Use:
ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "javascriptFunction();", true);
Other Wise You can Use
ClientScript.RegisterStartupScript
(GetType(),Guid.NewGuid().ToString(), "javascriptFunction();",true);

Dropdownlist Postback interferes with Javascript

My ASP page has a DropDownList (DDL) and a bunch of checkboxes. When my user selects a DDL item, I have some JS/jQuery code to make several checkboxes invisible according to some logic. However, the postback of the DDL, needed to run the C# code-behind event handler, causes a redraw of the entire page and makes all checkboxes visible.
How can I prevent the postback from wiping out the actions of the JS? Should I store visibility bits in something like ViewState?
Is it possible to have the JS code run AFTER the postback, instead of before?
Is it possible/easy to use CallBack instead of PostBack for the ASP DropDownList? I was thinking that the fun of Ajax was avoiding postbacks and only updating the control instead of the entire page.
Well if the dropdown posts back in the same action that you flip the switch for the checkboxes, why not then put the logic to show/hide checkboxes on the server instead of the client? You can use the hidden field approach, and that would work fine.
The callback means the UI needs updated by JavaScript. I don't know what all the postback updates, but you would have to send everything up via JSON, and refresh the UI via JavaScript. So only you can make that call.

jquery trigger action on option add

I have an select field which is on a jsp page and I add options to it through an external js library. Now is there a way on my jsp to trigger an action when an option is added?
Its because I need to get some value from backend and its not possible to do everything on an external js page.
I used .change() but I guess because of the fact that the adding takes place in an external page, it does not trigger anything.
Use .on
$('#your_dropdown_id').on('change', function (d) {
// your code here..
});

Forcing a POST on every checkbox/dropdown change

How can I force any change to a checkbox (inside a form) or to a drop-down menu selection to cause a HTTP POST to be issued by the browser?
Bandwidth is not an issue, page reloading is not an issue and I don't want to go the full AJAX route.
What I really want is an HTTP POST to be done when the user clicks on a checkbox (or selects something from a drop-down menu), etc. without the user having to click 'Submit' after its change.
Maybe it should be done with some JavaScript on the client-side? (I couldn't succesfully Google anything)
Use Javascript. Add onchange="document.getElementById('myFormId').submit()" to the elements, or do this programatically. myFormId must be replaced by the HTML id of the form element.
You could use the JavaScript onchange event to then call the submit() function on the form.
if you have a form already set up just put a class to the element you want to use as trigger and then
for select
$(".classname").change(function(){
$("#formid").submit();
});
for checkbox/radio
$(".classname").click(function(){
$("#formid").submit();
});
AFIK it can't be done without client-side scripting. The easiest way would be to trigger the submit event for every form change. With jQuery it's done with the following code:
<script type="text/javascript">
$(function() {
$("input[type=checkbox],input[type=radio],select").change(
function(evt)){evt.target.form.submit();}
);
});
</script>
If you don't use jQuery, you'll have to write some boilerplate event handling code. See this introduction for more info on JavaScript event handling.

Mouse Rollover event for ASP.NET control

I have a ASP.NET ImageButton control (no AJAX) with a Image_Click event that fetches data from SQL server. Now I want to add a Mouse Rollover to the Image Button that should fetch the data from the SQL server and display it on a Label.
I know you can add a Javascript Mouse Rollover event on the Image Button.
imgae_Button.Attributes.Add("onmouseover", "myJacascriptFunction()");
How do I get the Javascript code fire Server side code to fetch the SQL data?
Can this be done?
EDIT: Clarification onclick() event not only fetches data from SQL but also writes to another table. This event has alreday been coded without AJAX.
It is the mouse rollover event that I need help with. When the user hovers over the image, data should be fetched from SQL database & displayed on a label control.
This sure can be done, and will almost certainly involve AJAX.
In your mouse rollover event you want to define myJavascriptFunction() to click the image button on the users behalf, this will subsequently postback to the server as if the user had simply clicked.
You then want to place the image button and the label you want to show the answer into an UpdatePanel and ensure you stick a ScriptManager on the page too to handle the request/response.
This should achieve what you want.

Categories