C# - How To Register A Javascript Function To Run With Every Postback

Mar 17, 2010

I have a treeview in a user control. I need to run a javascript function with every asynch postback to scroll the div it's in to the right position. I've got it working, but I think there has to be a "cleaner" way to do it. In the Page_Load function of the control, I have the following code. Is there a better way to do it?

ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "key" + DateTime.Now.Ticks, "RestorePosition();", true);

For the benefit of anyone searching for this answer, here is what I finally did that worked. At the top of the ascx page, I have the following code:

<script type="text/javascript">
function pageLoad(sender, args) {
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(SavePosition);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(RestorePosition);
}
function SavePosition(sender, args) {
document.getElementById('hdnScrollSaver').value = document.getElementById('reportTreeViewdiv').scrollTop;
}
function RestorePosition(sender, args) {
document.getElementById('reportTreeViewdiv').scrollTop = document.getElementById('hdnScrollSaver').value;
}
</script>

And then I wrapped the treeview in a div tag as follows:

<div class="reportTreeView" id="reportTreeViewdiv">
<asp:TreeView ID="TreeView1" runat="server" OnTreeNodePopulate="TreeView1_TreeNodePopulate"
OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" PathSeparator="|" SkinID="ReportTreeView" />
</div>

View 3 Replies


Similar Messages:

C# - How To Register Multiple JavaScript Calls To Run On PostBack

Feb 10, 2011

I have an ASP.NET page with two instances of the same Web User Control (a simple WYSIWYG editor). On submit, the WUCs do a little JavaScript magic and then proceed with a normal postback.

The first instance seems to be working, but the second fails to post changes back to the server (it reverts to the original, and posts that). I believe the problem is that the JS only fires for the first WUC. I've traced that to the following code, from the generated client-side source:

function WebForm_OnSubmit() {
prepHtml('AddEditPopup1_ctlEditorQuestion_txtEdit','AddEditPopup1_ctlEditorQuestion_divEdit', 'AddEditPopup1_ctlEditorQuestion_divHT' );
//snip...
}

The problem seems to be that there should be two calls to prepHtml: one for the ctlEditorQuestion instance of the WUC, and one for the ctlEditorAnswer instance.

Instead, there's only the one for ctlEditorQuestion. Both controls are registering the OnSubmit event, but one of them overwrites the other.The prepHtml call is registered from the WUCs' C# code at runtime:

//Page_Load
_onSubmit = String.Format("prepHtml('{0}','{1}', '{2}' );",
txtEdit.ClientID, divEdit.ClientID, divHT.ClientID);
//OnPreRender
Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "get-html", _onSubmit);


I should point out that I didn't write this control myself, and I've never seen this kind of runtime registration of OnSubmit JS code before. Page.ClientScript.RegisterOnSubmitStatement is totally new to me. I need to register both prepHtml calls so they run sequentially. Is this possible? I'm open to alternatives to Page.ClientScript.RegisterOnSubmitStatement, so long as the code still gets fired on submit.

View 1 Replies

Javascript - Register Script After Partial Page Postback (UpdatePanel)

Aug 26, 2010

I have a simple form (textbox, submit button) which is wrapped in an update panel.

[Code]....

I type some text in the textbox, click submit, then the server creates a database record and returns an object, which has properties like ID, Name, URL, Blah, etc. These are the values that the script requires.

So if i were to call a web service from the client-code, in order to get the values that were just created, i would need to do some hacks (get last record modified that has the value of the textbox). Not ideal, and neither is two AJAX calls for one form post. (update panel postback, then web service call).

View 1 Replies

How To Run A Javascript Function Before Postback Of Button

May 27, 2010

I'm using Javascript to create a DIV element and open up a new page by using onclientclick. This works great. Now, I need to write to it from the server side and this element must be created before it is posted back.

How do I get the javascript to execute before the postback?

Currently, I have to press the button twice because the element doesn't exist to write too on the first click.

To be clear, I need this to execute before the "OnClick" of the button.

Update: It looks like the Javascript function is called before the postback but the element is not updated until I run the second postback.

Update: Unfortunately it is a bit more complicated then this.

I'm creating a div tag in javascript to open a new window. Inside the div tag, I'm using a databinding syntax <%=Preview%> so that I can get access to this element on the server side. From the server side, I'm injecting the code.

I'm thinking this may be a chicken-egg problem but not sure.

UPDATE!

It is not the Javascript not running first. It is the databinding mechanism which is reading the blank variable before I'm able to set it.

View 4 Replies

Hooking A Javascript Function To Every Postback?

Aug 15, 2010

This is some Javascript added on an .aspx page:

<Head>
<script>
function SayHello()
{
alert('Hello');
}
</script>

and here is how I am calling it from code:

protected void Page_Init(object sender, Eventargs e)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(),"script","SayHello()",true);
}

The problem is that the Javascript function is called only one time, on the very first page load. I want it to be called on every post back. for example on every button click

View 2 Replies

AJAX :: Call JavaScript Function After UpdatePanel Refresh (Partial PostBack) Is Completed

May 7, 2015

[URL] .... am using this functionality in my project and it is working very fine but i am facing very strange issue while using this.

The above functionality is not working when we place dropdownlist and textbox inside updatePanel and ModalPopupExtender

The .aspx page where i am using ModalpopupExtender and UpdatePanel for DropDownList and Textbox is below

<%-- *************************** MODAL POPUP EXTENDER ***************************************************** --%>
<asp:HiddenField ID="HiddenField3" runat="server" />
<asp:HiddenField ID="HiddenField4" runat="server" />
<cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupControlID="Div6" TargetControlID="HiddenField3"
BackgroundCssClass="modalBackground" CancelControlID="bclosemodalpopup" >
</cc1:ModalPopupExtender>

[Code] ....

The above code is not working with UpdatePanel but it is working if we remove DropdownList and Textbox from updatepanel.

The javascript code which i am using is below

<script type = "text/javascript">
var ddlText, ddlValue, ddl, lblMesg;
function CacheItems() {
ddlText = new Array();

[Code] .....

View 1 Replies

Register A JS Function To Run When A Browser Window Is Resized ?

Jun 24, 2010

How do I register a JS function to run when a browser window is resized ?I assume it's in the Page.ClientScriptclass methods somewhere,

View 1 Replies

Web Forms :: Getting Error Calling Javascript Function From Another Javascript Function

Jan 3, 2011

Getting error calling Javsscript function from another Javascript function

[Code]....

View 4 Replies

Web Forms :: Register JavaScript In UserControl

Mar 1, 2011

I have a UserControl, ASPX page, a Class file and a JS file. In Class file, I have a method for building and registering textbox controls:

ScriptManager.RegisterClientScriptBlock(this,
typeof(Page),
"onfocus", jsText.ToString(),
true);

jsText is nothing but Name and Value pair strings. Example : "KEY1:txtName, KEY2:txtDOB" In UserControl, I am calling the above Class method to pass all the textboxes and calling the JS file to validate the registered controls. Then, I am calling that Usercontrol in ASPX page. Here I am unable to validate the controls as it is saying "KEY is undefined".

View 2 Replies

AJAX :: How To Add Javascript Programmatically Or Register

Jul 28, 2010

In Asp.net how many ways we have to add javascript programmatically or register when we use ajax or normal page postback in 3.5 and 4.0 versions of asp.net

View 1 Replies

Register Javascript Code From Serverside?

Jul 1, 2010

I have a Asp.Net control inside a updatepanel thet is inside a modal popup. I wont to register write javascript code in client from the control code.

these is my code:

Dim output As String = .. javascript code
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "frmulaMatrix", output, True)

these is my second thinf but dont work

Page.RegisterClientScriptBlock("SCRIPTNAME", "<script language='javascript'>" + output+"</script>")

View 3 Replies

Register Javascript Inside User Control Using C#?

Jan 21, 2011

I want to call javascript function from User Control using C#. For that i am trying to use

ScriptManager.RegisterStartupScript(this, typeof(string), "alertbox", "javascript:ShowPopup('Select a row to rate');", true);

but it is not working for me. This works fine on the page. how can i call javascript function at runtime using C#.

View 5 Replies

Register Javascript File To Aspx Page From WebReousrce?

Jul 7, 2010

How do i add my javascript file as an embeded resource to the page after the ajax javascript already on the page? NB want to do this part dynamically to have code wrapped up in usercontrol.

in aspx page: scriptmanager

Code: Assembly: WebResource("Functions.js", "text/javascript")
Code: onPreRender:

ScriptManager.RegisterClientScriptResource(Me.Page, Me.GetType().BaseType, "Functions.js")

This code successfully adds my javascript code to the page but not after the AJAX javascript and so not all of my functions work correctly.

View 1 Replies

Call Javascript Function And Server Side Function From Linkbutton

Jan 27, 2010

<asp:LinkButton CssClass="button" ID="btnApply" runat="server" OnClick="btnApply_Click()" OnClientClick="Apply1('btnApply')" >
hi ihave this functin in .vb file
Protected Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
end sub

and javascript function also in aspx

function ApplySummerization(id)
{
alert("hai");
}

View 4 Replies

Register Javascript - Unable To Get Working In Usercontrol Inside Update Panel

Jan 27, 2011

i have a javascript color picker i can get it working fine in a aspx page, but i am unable to get it working in a usercontrol inside a update panel. I have tried using the following but i still cannot get it to workm the js file can be viewed at [URL]

ScriptManager.RegisterClientScriptInclude(this, this.GetType(), "colorpicker_1", "/_template/js/colorpicker/jscolor.js");

View 10 Replies

Page Title With Register Symbol Change In Master Page On Postback

Sep 21, 2010

I have a below Page title in my master page

<title>My Test Application NameŽ</title>

It renders Fine with Register symbol after the text "My Test Application Name" on start of the page but on any postback the title turn into My Test Application NameŽ Kindly let me know what can i do with minimal effort to fix this issue

View 1 Replies

Custom Server Controls :: How To Register Multi Javascript Files With RegisterClientScriptInclude Method In OnPreRender

Sep 29, 2010

how can i use multi javascript files with RegisterClientScriptInclude method in custom server controls. when i call two or more RegisterClientScriptInclude method, this method registers only one of the javascript files.

[Code]....

[Code]....

[Code]....

View 3 Replies

How To Replace A Function Name Using JavaScript And Have It Be Recognized As A Function

Aug 25, 2010

I am trying to replace the JavaScript onclick event handler in ASP.NET that is added to a button control when using validation controls. This is what is output into the HTML from ASP.NET in this scenario:

<input type="image" name="ibSubmit1" id="ibSubmit1" src="button-green-submit.gif" onclick="showProgress1();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ibSubmit1", "", true, "Group1", "", false, false))" style="border-width:0px;" />

I have looked pretty estensively, and unfortunately there doesn't seem to be a way to modify the function server side before it is injected into the page.

Since I am developing a control and desire it to be non-invasive and self contained, and I am interested in obtaining the validationGroup parameter of the WebForm_PostBackOptions object, it seems that the easiest solution would be to use JavaScript to replace the WebForm_DoPostBackWithOptions function name with my custom wrapper function and leave all of the rest of the parameter information intact - then I can extract the information I am interested in, call my custom functions, and then forward the call on to WebForm_DoPostBackWithOptions.

NOTE: I am using jQuery to build my custom function, so if there is an easier way to do this with jQuery it is an option I will consider.

Here is the code I tried to replace the onclick event handler (not working):

$('[onclick*=WebForm_DoPostBackWithOptions]').each(function() {
var txt = this.onclick;
txt = txt + '';
txt = txt.replace('WebForm_DoPostBackWithOptions','ml_DoPostBackWithOptions');
this.onclick = eval(txt);
});

Using alert(), I verified that the text is being changed correctly, however whether or not I use the eval() function, the onclick handler doesn't seem to recognize it as JavaScript.

I thought of using a regular expression to get the validationGroup value, but this seems like it will be far more elegant and flexible if I can get it working...

Note: If there is a way for my control to interrogate the page it is on to find all of the buttons that will post back (regardless of what type of buttons they are) so I can retrieve the property server-side, this is also something I will consider.

View 3 Replies

Call Codebehind If Javascript Doesn't Exist In Browser - Else Javascript Function

Sep 29, 2010

In asp.net page, How can i call the javascript methods for form processing-submitting if the user browser supports javascript and use code behind events if the browser does not support javascript.I have the javascript code to send the form data to an ajax server page using jquery. Don't know how to invoke the needed one based on the browsers javascript availability

View 1 Replies

JavaScript - User Control JavaScript Function Doesn't Run

Sep 22, 2010

I have the function put here like below:

$(document).ready(function () {
UserControlNameInit();
});

The script are put in the following and the block is in the .ascx page.

<script type="text/javascript">
</script>

However, the function UserControlNameInit() does not run when the page loads. It is showing in the page source. I can still call this function through FireBug console by manually typing the name of the function.

I did the same way with other user controls, and it works. Just 1-3 user controls are not working...

View 1 Replies

Javascript - Postback And Get Value From Html In That Postback?

Feb 4, 2011

I have a js function

function(id){
// do a postback here and get the id on the server side
}

I need to send the id to the server, how could I do this?

any way will do, using additional/hidden controls anything

View 1 Replies

How To Stop Button Postback With If Function

Jul 6, 2010

i have a button that will check whether textbox1.text is empty or not. I have put a if function inside the btn_click event and look like this

Code:

If TextBox1.Text <> Nothing Then
''Will COMMIT if everything OK
MsgBox("OK Popup userCon2")
Else
End If

May i know what shpuld be the code inside the else statement if i do not want the button to create a postback and just do nothing?

View 3 Replies

Forms Data Controls ::Postback Of Page Using JavaScript Postback's Slow Down The Page?

Sep 22, 2010

I'm at my wits end on this one looked/Searched/Played/Experimented and I need your help if you can.This page is going to be primarily used on a mobile device using Win Mobile 5 Need to Accomplish:Prevent Postback(serverside) of Page using JavaScript (Postback's slow down the page on mobile device)

[code]...

View 1 Replies

C# - Calling JQuery Function From Code Behind After Postback?

Feb 22, 2011

I m trying to call a JQuery .click funtion after page postback from code behind. I m using ASP.NET with C#.

View 2 Replies

C# - Call Codebehind Function Without Postback With Ajax?

Mar 23, 2011

I have a controller:

<asp:Button OnClick="MyFunction" runat="server" />

I want to be able to call MyFunction without the page reloading. Is this possible with ajax or something?

If so how would I do it?

View 5 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved