如何从ascx页面调用javascipt函数? [英] How to Call javascipt function from ascx page?

查看:64
本文介绍了如何从ascx页面调用javascipt函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .ascx页面上写了一个javascript函数,我在同一页面的按钮点击(ascx.cs)上调用它。这个函数返回一个字符串值,我分配 hiddenfield ,但实际上该函数正常工作,因为它从 alert(macAddress) JS中的语句给我客户端的macaddress 警告,但该值不是复制到hiddenfield。(在asp:panel中写入HiddenField控件) 。

但同样这个完整的场景在aspx页面中正常工作。我不明白为什么这个脚本不能在ascx页面上工作/执行。

你能告诉我我怎么能这样做?



有我的Javascript代码



I have a javascript function written on .ascx page, which i am calling on the button click of same page(ascx.cs). this function return a string value that i am assigning to hiddenfield, But in actual the function working properly as it is giving me alert for macaddress of Client from alert(macAddress) Statement in JS, but the value is not copying to hiddenfield.(HiddenField contol written inside the asp:panel).
but on the Same hand this complete scenario work properly in aspx page. I am not getting why this script not working/executeing in ascx page.
Can you guys suggest me how could i do this?

There is my Javascript code

<script language="javascript" type="text/javascript">
                        function networkInfo() {

                            var wmi = new ActiveXObject("WbemScripting.SWbemLocator");
                            var service = wmi.ConnectServer(".");


                            e = new Enumerator(service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"));


                            for (; !e.atEnd(); e.moveNext()) {
                                var s = e.item();
                                var macAddress = unescape(s.MACAddress);

                            }
                            alert(macAddress);
                            document.getElementById("macaddress").value = macAddress;
                        }
    </script>





并且有我的Ascx.cs代码





and there is my Ascx.cs code

protected void btnLogin_Click(object sender, EventArgs e)
    {
        //Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMacFunction", "networkInfo()", true);
        ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Panel), "CallMacFunction", "networkInfo()", true);
        string MacAddress = macaddress.Value;
}







此代码在aspx页面中正常工作但在ascx页面中无效。

所以请给我一些解决方案。




This code working fine in aspx page but not working in ascx page.
So please suggest me some solution.

推荐答案

有很多方法可以做到这一点。你可以在任何地方包含javascript并为该点击设置一个事件。



如果javascript只是该用户控件的一部分,你只希望它被包含在内使用用户控件,您可以将其放入Web资源并从那里访问它。



http://bchavez.bitarmory.com/archive/2008/07/28/understanding-scriptresource-and-webresource -in-asp.net.aspx [ ^ ]
There are many ways to do this. You can just include javascript anywhere and setup an event for that click.

If the javascript is a part of that user control only, and you only want it to be included with the user control, you can put it in a web resource and access it from there.

http://bchavez.bitarmory.com/archive/2008/07/28/understanding-scriptresource-and-webresource-in-asp.net.aspx[^]


这篇关于如何从ascx页面调用javascipt函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆