如何调用从C#code javascript函数落后于Asp.net按钮点击?

问题描述:

我要打电话给我的Facebook从code发送对话框功能背后:

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

JavaScript的code是:

The javascript code is :

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: '******'


    });
}

这里的扭曲是我想从动态生成的.aspx的按钮调用这个

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

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

我试过按钮点击此code

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?

添加它作为一个属性。

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

如果的OnClientClick 不工作,尝试的onClick 。不记得从我的头顶哪一个会工作。

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

请务必返回false 从JS函数prevent 回发

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

伪code:

- JS功能 -

--JS Function--

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

- 在aspx.cs页面添加按钮 -

--Adding button in aspx.cs page--

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

您不需要 btn_click 事件作为你在code有。您也不需要的RegisterStartupScript 因为你已经有JS文件的功能。 的RegisterStartupScript 使用时要JS功能从服务器端添加到页面。

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.