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);
Related
I want to show a div after an action.
For now i will just use a button click event.
this is my jquery function:
function hideMessageBlock() {
$('.alert').delay(5000).fadeOut('slow');
}
i also tried this one:
$(document).ready(function(){
console.log('READY to animate');
function hideMessageBlock() {
$('.alert').delay(5000).fadeOut('slow');
}
});
in my code behind i have the following code:
protected void Button1_Click(object sender, EventArgs e)
{
alertSuccessBlock.Visible = true;
lbl_alertSuccessBlock.Text = "Animate this block with timeout!";
ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptManager1", "hideMessageBlock();", true);
}
in the aspx i have declared a scriptmanager right after the scripts i use (bottom of the page before form and body closure):
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
The Error that i am receiving is : ReferenceError: hideMessageBlock is not defined
The function is called before the whole jquery script is loaded (just guessing)
How can i resolve this issue?
I have already solved the issue. I had to move the function definition outside of the document.ready callback
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!');
});
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; });
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... });
I have a button on my aspx page. I want to use javascript confirm before continuing execution when clicking on that button. I can do it easily if i am writing javascript in aspx page itself . But my problem is each time the confirm message may be different. I need to check various condition to generate appropriate confirm message.
Can I call confirm in my code behind, so that I can construct confirm message from there?
What I'm trying is:
protected void Button1_Click(object sender, EventArgs e)
{
//just the algorithm given here
string message=constructMessage(); \\ its just a function to construct the confirm message
if(confirm(message)) // i know i cant use javascript in code behind direct. How can i do this
{
//do something
}
else
{
// do nothing
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string message=
"if(confirm("+message+"))
{
//do something
}
else
{
// do nothing
}";
this.ClientScriptManager.RegisterStartupScript(typeof(this.Page), "warning", message, true);
//Prints out your client script with <script> tags
}
For further reference on ClientScriptManager
I just got this link which describes different ways of calling javascript
http://www.codedigest.com/Articles/ASPNET/314_Multiple_Ways_to_Call_Javascript_Function_from_CodeBehind_in_ASPNet.aspx
may be this will help..