HaikMnatsakanyan
HaikMnatsakanyan

Reputation: 303

How i can call JavaScript function from asp.net server side if script in user control

I have a ASP.NET page with 1 user controls registered.

i have one server control

<asp:LinkButton ID="vidoUpdatebtn" OnClick="vidoUpdatebtn_Click" runat="server">LinkButton</asp:LinkButton>

in .cs i handle

 protected void vidoUpdatebtn_Click(object sender, EventArgs e)
    {
        //do something
        ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "doSomeThing()", true);

    }

in user control i have function doSomeThing()

 function doSomeThing() {
         alert("TestReady");
     }

when i click on LinckButon registred function dosen't work

Upvotes: 2

Views: 11223

Answers (3)

Eduard Dumitru
Eduard Dumitru

Reputation: 3262

Write this in PageLoad (the only modifications from what you were writing are the moment in time of the registration -- PageLoad not Click, Click is too late and the face that you must write the actual implementation of the javascript function here):

    protected void Page_Load(object sender, EventArgs e) {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", 
@"function doSomeThing() {
    alert(""TestReady"");
}"
        , true);
    }

and this in the asp:LinkButton tag (you must specify the OnClientClick attribute):

<asp:LinkButton ID="vidoUpdatebtn" runat="server" OnClick="vidoUpdatebtn_Click" OnClientClick="doSomeThing()" >LinkButton</asp:LinkButton>

And it should work.

Upvotes: 3

Grant Clements
Grant Clements

Reputation: 984

If you want client side code to run on the button click, you could use the OnClickClick property instead of the OnClick event. That'll also avoid the need for a postback and will give immediate feedback to the user.

e.g.

<asp:LinkButton ID="vidoUpdatebtn" OnClientClick="alert('TestReady'); return false;" runat="server">LinkButton</asp:LinkButton>

Upvotes: 0

coder
coder

Reputation: 13250

Have you tried this way:

Add Script manager on your aspx page

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

Codebehind:

Page.ClientScript.RegisterStartupScript(this.GetType(),"myscript", "doSomeThing()",true);

Upvotes: 0

Related Questions