Call jquery button click event from code behind - c#

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#

Related

Tekerik radwindow open and close issue

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.

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

Not able to call a javascript function through alert

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

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

jQuery Submit Heisenbug

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.

Categories