Not able to call a javascript function through alert - c#

I am creating an alert and trying to call a click event through javascript function when "OK" of alert is pressed.It runs pretty well if I create the alert on page_Load but When I crate the alert on clicking of a buttton, then on pressing "OK" of alert the required click event is not called.
This is how I create the alert
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Startup", "Test();", true);
}
This is the javascript function which calls a click event
<script type="text/javascript">
function Test() {
alert('There is no Bookmarked Question Available');
document.getElementById('btnReview').click();
}
</script>

if btnReview is server button then try change you script like this
for asp.net
<script type="text/javascript">
function Test() {
alert('There is no Bookmarked Question Available');
document.getElementById('<%= btnReview.ClientID %>').click();
}
</script>
for asp.net mvc (razor)
<script type="text/javascript">
function Test() {
alert('There is no Bookmarked Question Available');
document.getElementById('#btnReview.ClientID').click();
}
</script>

You just add below code in javascript function
__doPostBack('btnSubmit','OnClick');

Try this
protected void Button1_Click(object sender, EventArgs e)
{
string scripts = #"
function Test() {
alert('There is no Bookmarked Question Available');
}";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Startup", scripts, true);
}

Related

Trying tocall a jquery function from server side

I am trying to call a jQuery function from the server using script manager but I'm not able to make the call. How should it be called? Any ideas would be appreciated.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDummyRow();
}
TW12HVGI();
}
void TW12HVGI()
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "dynamicForm();", true);
}
}
<script type="text/javascript">
// $(document).ready(function () {
$(function dynamicForm() {
$("#openForm").click(function () {
});
});
</script>
The issue is because you've defined dynamicForm() as the function you provide to the jQuery document.ready handler, so it's not in the correct scope. Try this instead:
function dynamicForm() {
$("#openForm").click(function () {
// do something...
});
}
Also note that you could make this a lot simpler by just calling dynamicForm() within your JS and removing the unnecessary RegisterStartupScript() from your server side logic.

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; });

asp.net textbox focus problems

I am trying to set focus on textbox on page load in asp.net as follows
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
// fillUnitType();
//
fillLastCode();
txt_Grn_Date.Text = System.DateTime.Now.ToString("dd-MM-yyyy");
setinitialrow_lvl();
txt_Po_No.Focus();
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}
But the textbox is not getting focused. What is that I am missing. I have used update panel is it because of that.? Or my css is slightly faulty.
Write following function in your codebehind and for every control call this function
private void Set_Focus(string controlname)
{
string strScript;
strScript = "<script language=javascript> document.all('" + controlname + "').focus() </script>";
RegisterStartupScript("focus", strScript);
}
Set
tapindex = 0
TextBox1.Focus();
or
textBox1.Select();
or
protected override void OnShown(EventArgs e)
{
textBox1.Focus();
base.OnShown(e);
}
or
setTimeout("myFocusFunction()", 500);
function myFocusFunction(){
$("#myTextBoxID").focus();
}
try this in javascript
<script language=javascript>
function fnLoad(){
document.getElementById("<%= txt_Po_No.ClientID %>").focus();
}
</script>
call "fnLoad()" function on "onLoad" event of body..
You need to add this function in body tag : Like
<body onload="fnLoad()">........</body>
Update:
try another way
<script language=javascript>
$(document).ready(function(){ document.getElementById("<%= txt_Po_No.ClientID %>").focus();})
</script>
or
<script language=javascript>
$(window).load(function(){ document.getElementById("<%= txt_Po_No.ClientID %>").focus();})
</script>
I have tried this with updatepanel and textbox inside it.
CodeBehind
.aspx
output
Try this code..
string jsCode= "<script language=javascript>document.getElementById('<%= TEXTBOX.ClientID%>').focus();</script>";
ClientScript.RegisterClientScriptBlock(GetType(), "txtbox",jsCode, false);

How to Call Javascript function in ASPxPopupControl_WindowCallback in asp.net

I have an ASPxPopupControl and in Callback Function I want to call a javascript function
<script type="text/javascript">
function ShowAlert() {
alert('Display Message Alert');
}
</script>
protected void ASPxPopupControl_WindowCallback(object source, DevExpress.Web.ASPxPopupControl.PopupWindowCallbackArgs e) {
I want to Call ShowAlert() javascript function from here
}
I tried this code but it only works on button click event
Page.ClientScript.RegisterStartupScript(GetType(), "MyKey", "ShowDetailView();", true);
This is a working code which will invoke the javascript function from codebehind.
<script type="text/javascript">
function ShowAlert() {
alert('Display Message Alert');
}
</script>
Code Behind
Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "ShowAlert();", true);
Extra in case if you want to open a window in javascript instead of a pop up ,
ScriptManager.RegisterStartupScript(Page, typeof(Page), "Alert", "window.open('description.aspx','Mywindow','scrollbars=Yes,width=800,menubar=yes, resizable=No');", true);

Categories