4

I have used ScriptManager.RegisterStartupScript() method in order to show an alert when particular thing happens in back end.It works fine in page load method but not in particular method which is called when a specific button is clicked . I Couldn't find a solution because in another page it works fine in both page load and the method.

Script Manager RegisterStartupScript Method

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('msg');", true);

HTML

<asp:HiddenField runat="server" ClientIDMode="Static" ID="PostBackController"/>
<button class="some-class" id="btnSave" runat="server" onclick="btnSave_clientClick();">SAVE</button>

Javascript

function btnSave_clientClick() {

     // code

     if (some_condition) {

        $('#PostBackController').val("btn_save");
        __doPostBack();

     }
}

Page Load Method

protected void Page_Load(object sender, EventArgs e)
{
    if (PostBackController.Value == "btn_save")
    {
        uploadDocSave_ServerClick();
    }
}

Method Wish should be called when button Clicked

protected void uploadDocSave_ServerClick()
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('msg');", true);
}
6
  • add breackpoint on uploadDocSave_ServerClick() and check if its getting called or not? Commented Jan 2, 2015 at 10:30
  • I already did. of course, it is getting called. Commented Jan 2, 2015 at 10:34
  • try wrting a return; statement after the line.. it seems there are other codes being executed after the script is registered Commented Jan 2, 2015 at 11:09
  • Still not working :( Commented Jan 2, 2015 at 11:36
  • Look at that link please. I hope it helps you. Commented Jan 2, 2015 at 14:40

6 Answers 6

7

My deepest condolences for OP if you still have to work with Webforms. This is how you can solve it in a minimal way while reducing traffic:

Codebehind sample:

public partial class About : Page, IPostBackEventHandler
{
    protected void Page_Init(object sender, EventArgs e)
    {
        // Unless the button is serverside clicking it will reload the page
        // registering the page like this prevents a page reload.
        var scriptManager = ScriptManager.GetCurrent(Page);
        scriptManager?.RegisterAsyncPostBackControl(Page);
    }

    /// <inheritdoc />
    public void RaisePostBackEvent(string eventArgument)
    {
        var javascriptCode = $"alert('server method called and triggered client again. {DateTime.Now.ToString("s")}');";

        // if your key isn't changed this script will only execute once
        ScriptManager.RegisterStartupScript(udpMain, typeof(UpdatePanel), Guid.NewGuid().ToString(), javascriptCode, true);

        // updating the updatepanel will inject your script without reloading anything else
        udpMain.Update();
    }
}

Webforms sample:

<script type="text/javascript">
    function clientFunction(sender, args) {

        alert('client function called');

        <%= Page.ClientScript.GetPostBackEventReference(Page, "callServerFunction") %>

        args.preventDefault();
    }
</script>

<asp:UpdatePanel runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional" ID="udpMain">
    <ContentTemplate>
        <h2><%: Title %>.</h2>
        <h3>Your application description page.</h3>
        <p>Use this area to provide additional information.</p>
        <button onclick="clientFunction(); return false;">raise server function</button>
    </ContentTemplate>
</asp:UpdatePanel>

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

If you Used Update Panels Then You can Use:

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "javascriptFunction();", true);

Other Wise You can Use

ClientScript.RegisterStartupScript
        (GetType(),Guid.NewGuid().ToString(), "javascriptFunction();",true);

1 Comment

Yes I use firebug. but there is no error in console or anywhere. This is weird.
1

Try this:

Client side:

Use Literal Control
<asp:Literal ID="IMsg" runat="server" Text=""/>

Server side:

string msg = "<script>alert('Change successfully');</script>";
IMsg.Text = msg;

I think it will help you. It works fine in my projects.

1 Comment

Why did you post that as an image?
0

Try to remove line containing:

__doPostBack();

in the JavaScript code.

1 Comment

Run the app in Firefox with Firebug Console On - to see if there are any errors reported. There maybe some JavaScript code from outside blocking execution of your alert function.
0

Sorry I am late at the party. Use this overload of RegisterStartupScript method, and set the first parameter the button on which you want to click to register the javascript function. Like:

ScriptManager.RegisterStartupScript(btnSave, this.GetType(), "alert", "alert('msg');", true);

Cheers!

Comments

0

THE REALLY SIMPLE WAY

You can wire up a <button runat="server"></button> to a server event using the onserverclick attribute:

HTML:

<button class="some-class" id="btnSave" onserverclick="uploadDocSave_ServerClick" runat="server">SAVE</button>

C# Handler:

protected void uploadDocSave_ServerClick(Object sender, EventArgs args)
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('msg');", true);
}

WITH CLIENT SIDE VALIDATION

If you want to add some sort of JavaScript check before postback, you can do that from the onclick attribute:

HTML:

<button class="some-class" id="btnSave" onclick="return btnSave_clientClick();" onserverclick="uploadDocSave_ServerClick" runat="server">SAVE</button>

JavaScript:

function btnSave_clientClick() {

     // code

     if (some_condition) {

        return true; // allows the control to do a postback

     }
}

C# Handler:

protected void uploadDocSave_ServerClick(Object sender, EventArgs args)
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('msg');", true);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.