I want to call a javascript function from my codebehind.
In my button click event handler I have:
protected void load_data_Click(object sender, EventArgs e)
{
if (dt.Rows.Count == 1)
{
BindDl();
}
else
{
//if dt.rows.count! = 1 I want to call a JavaScript function where be one alert! how to do?
}
}
This page will be helpful for you
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
Type cstype = this.GetType();
String csName = "MyAlertFunction";
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csName))
{
String jsFunction = "yourFunctionHere()";
cs.RegisterStartupScript(cstype, csName, jsFunction, true);
}
user scrip manager
ScriptManager.RegisterStartupScript(this, typeof(string), "SHOW_ALERT", "alert('')", true);
where in place of alert you can put your javascript code, next argument true puts in script tags automatically so you dont have to write them.
Related
With the click event of a link button calling a server side method from Form1
Form1 aspx.cs page code:-
protected void lnkTakeAction_OnCommand(object sender, CommandEventArgs e)
{
if (e.CommandName == "duplicate")
{
//Some operations conditional
//If condition true
//call javascript method to call a javascript function to open a rad window
ScriptManager.RegisterStartupScript(this, typeof(System.Web.UI.Page), "ABC "ABCPopUp" + ID + "','Tag');", true);
}
}
Form1 aspx javascript code:-
function ABCPopUp(ID, Tag) {
var oWindow = window.radopen('XYZPage.aspx?ID=' + scoreMethodId + '&Tag=' + mode, 'rdWindowMapping');
oWindow.SetSize(600, 500);
oWindow.Center();
var titleBar = oWindow.GetTitlebar();
$telerik.$(titleBar).parent().hide();
return false;
}
Issue facing - The XYZPage is not opening.The same functionality works if I call the function ABCPopUp from javascript.But when I call from server side the form is not opening.Please give expert advice
Most likely, the script runs too early. You have two options:
use AJAX for the grid and do not include the RadWindow in the response
OR, use the Sys.Application.Load event to call your function: http://www.telerik.com/help/aspnet-ajax/window-troubleshooting-opening-from-server.html.
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; });
Here's what I want to do. I have a form that the user fills out (creating sections and subsections) and when they click save, I want to check the database to see if they have named a section the same as one that already exists. If they have, I want to get a confirmation from them to let them know they wil create a duplicate if they proceed. If they click yes, I need to continue, else I need to abort. Here is some psuedocode for what I have so far.
protected void SaveButton_Click(object sender, EventArgs e)
{
try
{
if (CheckForDuplicates())
{
//proceed normally
}
}
}
private bool CheckForDuplicates()
{
//check database
if (/*there are duplicates*/)
{
string message = "A duplicate name exists. Would you like to continue?";
string scriptString = "<script language='javascript'
type='text/javascript'>" + "return confirm('" + message + "');</script>";
ScriptManager.RegisterStartupScript(this, this.GetType(),
"script", scriptString, false);
//here i would like to return their confirmation
}
}
}
return true;
}
All help is appreciated and thanks in advance!
Add Javascript such that if user confirms, you can call the JavaScript __doPostBack('','UserConfirmed'); function. Just add the logic in your codebehind along with your confirmation logic that you are registering with the ScriptManager . When the postback occurs, you can then check to ensure that the postback was, in fact, initiated by the user's confirmation (as opposed to some other action on the page):
public void Page_Load(object sender, EventArgs e)
{
string parameter = Request["__EVENTARGUMENT"];
//if parameter equals "UserConfirmed"
// User confirmed, so do whatever
//
}
Information on __doPostBack: Understanding the JavaScript __doPostBack Function
Am I injecting this correctly?
string myScriptName = "EventScriptBlock";
string myScript = string.Empty;
//Verify script isn't already registered
if (!ClientScript.IsClientScriptBlockRegistered(myScriptName))
{
Response.Write('b');
myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
myScript += "alert('hi');";
myScript += "\n\n </script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), myScriptName, myScript);
}
This is in my Page_Load, but I never see an alert and I have no JavaScript errors either.
You can use registerstartupscript instead of registerclientscriptblock!
RegisterStartupScript
When you use RegisterStartupScript, it will render your script after all the elements in the page (right before the form's end tag). This enables the script to call or reference page elements without the possibility of it not finding them in the Page's DOM
RegisterClientScriptBlock
When you use RegisterClientScriptBlock, the script is rendered right after the Viewstate tag, but before any of the page elements. Since this is a direct script (not a function that can be called, it will immediately be executed by the browser. But the browser does not find the label in the Page's DOM at this stage and hence you should receive an "Object not found" error
Difference between registerstartupscript and registerclientscriptblock
protected void Page_Load(object sender, System.EventArgs e)
{
string myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
myScript += "alert('hi');";
myScript += "\n\n </script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, false);
}
I have a feeling this is related to your asp.net/html markup.
Do you have a form tag like so in your .aspx file?
<form id="form1" runat="server">
....
</form>
Both RegisterStartupScript and RegisterClientScriptBlock will work.
Problem lies in myScript (string variable).In myScript variable you need to use alert variable only, as whenever you use this, script tag will be added automatically to your page's HTML at runtime. To check this right on your page and see the source of the page.
protected void Page_Load(object sender, EventArgs e)
{
string myScript = string.Empty;
//myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
string registerKey = "alert('RegisterClientScriptBlock');";
myScript = "alert('RegisterStartupScript');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "RegisterStartupScript", myScript, true);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "RegisterClientScriptBlock", registerKey, true);
}
Note: I have executed RegisterStartupScript first and than RegisterClientScriptBlock.But RegisterStartupScript alert will be executed at last, as it will be added at the end of the page.
RegisterClientScriptBlock will always be added at the starting of the page.
You should use RegisterStartupScript.
I have a heisenbug - it works when I put a Firebug breakpoint on it, but it doesn't work without the breakpoint.
The LoginSubmit function is called from two places in my code. In one instance, the btnLoginSubmit_Click event is fired successfully, but in the other instance, the event doesn't fire unless I put a breakpoint on the $submitButton.click(); line.
function LoginSubmit() {
$("#diagnostics").text("");
var $submitButton = $("#<%=btnLoginSubmit.ClientID %>");
$submitButton.click(); // <-- This is the line of code that is not executing.
$("#diagnostics").text("LoginSubmit failed to submit postback. ");
}
<asp:Button ID="btnLoginSubmit" runat="server" Text="Submit" CssClass="hiddenButton"
OnClick="btnLoginSubmit_Click" OnClientClick="return true;" Visible="true" />
Code behind page:
protected void btnLoginSubmit_Click(object sender, EventArgs e)
{
....
}
Consider registering a PostBackEventHandler instead of firing the onclick of the button
public partial class ScheduleEdit : System.Web.UI.Page,
System.Web.UI.IPostBackEventHandler
{
protected void Page_Init(object sender, EventArgs e)
{
var postBack = Page.ClientScript.GetPostBackEventReference(this, null);
var script = "function postback() { " + postBack + "; }";
Page.ClientScript.RegisterStartupScript(GetType(), this.UniqueID, script, true);
}
// this is called via the client side 'postback' script
public void RaisePostBackEvent(string eventArgument)
{
DoStuff();
}
}
// client side script:
$("#DP").datepicker({
onSelect: function (dateText, inst) { postback(); },
});
The issue involved a problem in jQuery involving dialogue boxes. When I closed the dialogue box before issuing the submit, it worked.