Label Text Changed Event - c#

In my page if the text of a Label has been Changed I need to fire some function using JQuery as .Change() is restricted to only <input> and <select> I use a function like
$("#ctl00_ContentPlaceHolder1_lblMsg").on('labelchanged', function () {
alert('changed!');
}
});
$("#ctl00_ContentPlaceHolder1_lblMsg").trigger('labelchanged')
but the above function is not triggered when the text of the label has been changed kindly anyone point me what could be I am missing in the above function
Edit: The label's text will be changed during runtime only when some message occurs or some exception is been thrown it is ued to display that exception
My aspx Code where the label text will be changed
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
//submit code//
}
catch(Exception ex)
{
lblMsg.Text=ex.Message();
}
}

$("#ctl00_ContentPlaceHolder1_lblMsg").on('labelchanged', function () {
alert('changed!');
});
$("#btn_change").on('click', function(){
$("#ctl00_ContentPlaceHolder1_lblMsg").html('New Label Text').trigger('labelchanged');
});
$(window).on('keypress', function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13')
$("#btn_change").trigger('click');
});
demo fiddle

There are several questions/answer similar to your problem right here on stackoverflow.
I don't think you can just trigger any user-defined event in jquery like that. The workaround is using promise like this:
$("#ctl00_ContentPlaceHolder1_lblMsg").html("Duplicate record can't be saved.").promise().done(function(){
alert('changed!');
});

Related

Call jquery button click event from code behind

I am using the below code to display a div on button click event. Is there any way to call this button click event from C# code behind.
$(document).on("click", '#AddNew', function (e) {
e.stopPropagation();
if ($NewEntry.is(":hidden")) {
$NewEntry.slideDown("slow", "easeOutBounce");
$NewEntry.slideDown("slow", "easeOutBounce");
$filter.slideUp("slow");
return false;
} else {
$NewEntry.slideUp("slow");
return false;
}
});
use this code :
protected void myButton(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>myFunction(params...);</script>", false);
}
more read : Call jQuery function from ASP.NET code behind C#

Alert message on paging in gridview

I want to show an alert when user tries to do paging in gridview. I am using this script, but the alert pops up even if any of those buttons in the page is clicked in the page. I need to alert only when paging is clicked.
<script type="text/javascript">
function closeEditorWarning() {
return 'It looks like you have been editing something -- if you leave before submitting your changes will be lost.'
}
window.onbeforeunload = closeEditorWarning
</script>
Really appreciate any help on this.
Write an java script alert inside gridview_pageindexchanging() event to do this:
You can do this by using Script Manager as follows:
You have double script tags. Add the script tags yourself:
protected void grid_pageindexchanging(object sender, GridViewPageEventArgs e) {
string script = "<script type=\"text/javascript\">alert('abc');</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
}
Or let the method add it:
protected void grid_pageindexchanging(object sender, GridViewPageEventArgs e) {
string script = "alert('abc');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
}
Hope this helps..
Updated:
try including the following:
using System.Web.Script.Serialization
Updated 2:
Can you try this now:
string script = "alert('its working now!')";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "its working",script,true);
If jQuery is an option, set a CSS class on the pager and associate your script with the click event for that class.
Untested but probably in the right direction:
$('.myClass').click(function () { closeEditorWarning; });

Trigger postback on a particular time in asp.net c# from client side(javascript/jquery)

I need to do btnRate_Click after particular time on javascript/jquery
protected void btnRate_Click(object sender, EventArgs e)
{
if (Session["User"] != null && Session["ActiveDoctor"] != null)
{
if (objRate.InsertRecord())
Response.Redirect("../Clinics.aspx", true);
}
}
Please help....
Assuming there's a button which has a click event which is tied to btnRate_Click, you can use __doPostback to trigger the post back.
This means you could do something like:
window.setTimeout(function() { __doPostback('<%= btnRate.UniqueID %>', '');}, 1000);
This should 'click' btnRate 1 second after calling the code.
See the following question for more info on __doPostback:
How to use __doPostBack()

Run a javascript on postback in c#

I used the below manner to run a JavaScript upon postback and it worked fine for me.
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"PostbackKey","<script type='text/javascript'>document.getElementById('apDiv1').style.visibility = 'hidden';</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(),"PostbackKey","<script type='text/javascript'>function show()</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"PostbackKey","<script type='text/javascript'>document.getElementById('apDiv1').style.visibility = 'visible';</script>");
}
}
The above code worked fine and now I want to try something like below.
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"verify","<script type='text/javascript'>verify1();</script>");
}
else
{
}
}
in the above code verify1() is a JavaScript linked externally to ASPX page. I am unable to execute verify1() function using this code. And verify1() function works fine when placed as <body onload="verify1();"> Is there any syntax error(s) in my above code?
this may help you,
Page.ClientScript.RegisterStartupScript(this.GetType(), "verify", "verify1()", true);
Maybe that script is being executed before verify1()'s function definition. By the time body.onload fires, the main page has completed parsing and all external scripts have been loaded, which is why it works in that scenario.
Can you use jquery? One option is to use jQuery's onload functionality, which won't be executed until everything is intialized:
$( function() { ...my startup code... });

Call Javascript function from Gridview RowDeleting event

I need to call a javascript alert from an if condition inside a gridview in the gvLocations_RowDeleting section.
Code is as follows:
protected void gvLocations_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (CheckIfLocationExists(ID) == true)
{
//need to call javascript function sendmessage() here??
}
}
I have a javascript function in the .aspx file as follows
<script type="text/javascript">
function sendmessage()
{
alert("Area is associated with this location already");
}
</script>
I know this is an easy move but for some reason Im having trouble. Can someone help? thanks in advance. Stack Overflow rocks!
Actually I figured it out. Hopefully this will help someone else..Use the code below, works like a charm..
Page.ClientScript.RegisterStartupScript(this.GetType(), "helloworldpopup", "alert('hello world');", true);

Categories