我正在開發一個使用C#的IE插件。我有一個js文件的JavaScript函數。我需要將js文件附加到C#並從C#調用js函數,將值發送給js,並且它必須返回一個值。添加js文件並從C#調用js函數#
JS文件 - sample.js:
function sample (str) {
//js code
}
C#文件:
private void button1_Click(object sender, EventArgs e){
//Need to call sample(str) and pass 'str' value to the js function.
}
PS:我嘗試下面的代碼,但得到的對象引用不設置到對象的實例
Page page = HttpContext.Current.CurrentHandler as Page;
page.ClientScript.RegisterClientScriptInclude("sample","sample.js");
page.ClientScript.RegisterStartupScript(typeof(Page),"Test","<script type='text/javascript'>sample('str');</script>");
可能重複[什麼是NullReferenceException,以及如何解決它?](http://stackoverflow.com/questions/4660142) /是什麼 - 是 - 一 - 的NullReferenceException和知識-DO-如果ix-it) – mybirthname
謝謝。但是,你能爲上述案例提出一個工作代碼嗎?代碼中有什麼錯誤? – abby