Click here to Skip to main content
15,943,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

XML
<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>


and there is my Ascx.cs code

C#
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;
}



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

1 solution

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[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900