Trying tocall a jquery function from server side - c#

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.

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#

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

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

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

Categories