Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Javascript function from server side [closed]

On a button click i am calling a server side function in which i am calling a Javascript function like

Page.ClientScript.RegisterStartupScript(
    this.GetType(), 
    "scriptsKey", 
    "<script type=\"text/JavaScript\" language=\"javascript\">test();</script>");

But Javascript function is not calling.

like image 991
user2798259 Avatar asked Oct 04 '13 07:10

user2798259


2 Answers

You can call the function from code behind like this :

MyForm.aspx.cs

protected void MyButton_Click(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "AnotherFunction();", true);
}

MyForm.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>My Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    function Test() {
        alert("hi");
        $("#ButtonRow").show();
    }
    function AnotherFunction()
    {
        alert("This is another function");
    }
</script>
</head>
<body>
<form id="form2" runat="server">
<table>
    <tr><td>
            <asp:RadioButtonList ID="SearchCategory" runat="server" onchange="Test()"  RepeatDirection="Horizontal"  BorderStyle="Solid">
               <asp:ListItem>Merchant</asp:ListItem>
               <asp:ListItem>Store</asp:ListItem>
               <asp:ListItem>Terminal</asp:ListItem>
            </asp:RadioButtonList>
        </td>
    </tr>
    <tr id="ButtonRow"style="display:none">
         <td>
            <asp:Button ID="MyButton" runat="server" Text="Click Here" OnClick="MyButton_Click" />
        </td>
    </tr>
    </table> 
</form>

like image 64
Md Ashaduzzaman Avatar answered Oct 16 '22 07:10

Md Ashaduzzaman


ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "javascript:test();", true);
like image 24
Thura Avatar answered Oct 16 '22 06:10

Thura