如何从Asp.net按钮背后的c#代码调用javascript函数?

时间:2022-08-25 22:05:33

I want to call my facebook send dialog function from code behind :

我想从后面的代码调用我的facebook发送对话框功能:

The javascript code is :

javascript代码是:

function facebook_send_message(to) {
    FB.ui({
        app_id:'*****',
        method: 'send',
        name: "*****",
        link: '*****',
       to: <%=to %>,
        description:'sdf sdf sfddsfdd s d  fsf s ',
        redirect_uri: '******'


    });
}

The twist here is i want to call this from a dynamically generated Aspx button

我想从动态生成的Aspx按钮调用它

 Button btn = new Button();
 btn.CssClass = "btn-add";
 btn.Text = "Invite";
 btn.Click += new EventHandler(btn_Click);

I tried this code on the button click

我在按钮单击时尝试了这个代码

protected void btn_Click(object sender,EventArgs e)
{

    Page.ClientScript.RegisterStartupScript(Page.GetType(), "my", "function facebook_send_message(to);", true);

}

However it is not working Please help?

但是它不工作,请帮忙?

1 个解决方案

#1


6  

Add it as an attribute.

将其添加为属性。

btn.attributes.add("OnClientClick", "javaScript: return myfunction(););

If OnClientClick doesnt work, try onClick. Cant remember from top of my head which one will work.

如果OnClientClick不工作,请尝试onClick。我想不起来哪一个会管用。

Make sure to return false from the JS function to prevent Postback.

确保从JS函数返回false以防止回发。

Pseudo code:

伪代码:

--JS Function--

——JS函数

function myFunction() {
         alert('this is my fb function');
         return false;
}

--Adding button in aspx.cs page--

——在aspx添加按钮。cs页面——

Button b = new Button();
b.ID = "btnSomeButton";
b.Text = "Call fb button";
dvButton.Controls.Add(b);
b.Attributes.Add("onclick", "return myFunction();");

You dont need btn_click event as you have in your code. You also dont need RegisterStartupScript as you already have your function in JS file. RegisterStartupScript is used when you want to add JS function to the page from the server side.

您不需要像在代码中那样使用btn_click事件。您也不需要RegisterStartupScript,因为在JS文件中已经有了您的函数。当您想从服务器端向页面添加JS函数时,将使用RegisterStartupScript。

#1


6  

Add it as an attribute.

将其添加为属性。

btn.attributes.add("OnClientClick", "javaScript: return myfunction(););

If OnClientClick doesnt work, try onClick. Cant remember from top of my head which one will work.

如果OnClientClick不工作,请尝试onClick。我想不起来哪一个会管用。

Make sure to return false from the JS function to prevent Postback.

确保从JS函数返回false以防止回发。

Pseudo code:

伪代码:

--JS Function--

——JS函数

function myFunction() {
         alert('this is my fb function');
         return false;
}

--Adding button in aspx.cs page--

——在aspx添加按钮。cs页面——

Button b = new Button();
b.ID = "btnSomeButton";
b.Text = "Call fb button";
dvButton.Controls.Add(b);
b.Attributes.Add("onclick", "return myFunction();");

You dont need btn_click event as you have in your code. You also dont need RegisterStartupScript as you already have your function in JS file. RegisterStartupScript is used when you want to add JS function to the page from the server side.

您不需要像在代码中那样使用btn_click事件。您也不需要RegisterStartupScript,因为在JS文件中已经有了您的函数。当您想从服务器端向页面添加JS函数时,将使用RegisterStartupScript。