Link to home
Start Free TrialLog in
Avatar of pamela rizk
pamela rizkFlag for Lebanon

asked on

javascript functions

dear all ,

i have a web user control that contains a combobox in asp web application.
inside this web user control i have a javascript function that return what is the value selected from the combo
on the other hand i have created a web page where i dragged inside 3 web user controls.
i realize that once page go the browser one html file will be created where javascript function exist 3 times for each control.
how can i have a javascript function in the user control with a certain id ion order to avoid this problem.
thank you in advance
Avatar of pamela rizk
pamela rizk
Flag of Lebanon image

ASKER

fort more information below is combo used in the web user control
<asp:DropDownList ID="cbo_currenciesofamount" runat="server" CssClass="DropDownCurveStyle" Width="100%"
                onfocus="Change_cbo(this, event)" onblur="Change_cbo(this, event)" 
                onchange="JavaScript:cbo_currenciesofamount_IndexChange();">
            </asp:DropDownList>

Open in new window


and cbo_currenciesofamount_IndexChange is teh javascript method inside the user control:
 
function cbo_currenciesofamount_IndexChange() {
        var Sp_cbo_currenciesofamount = document.getElementById('<%=cbo_currenciesofamount.ClientID%>');
        var Sp_cbo_currenciesofamount_value = parseInt(Sp_cbo_currenciesofamount.options[Sp_cbo_currenciesofamount.selectedIndex].value);
        alert(Sp_cbo_currenciesofamount_value);
    }

Open in new window

check this, you can include this multiple times without any issues
<select ID="cbo_currenciesofamount1" runat="server" CssClass="DropDownCurveStyle" Width="100%" onfocus="Change_cbo(this, event)" onblur="Change_cbo(this, event)" onchange="cbo_currenciesofamount_IndexChange(this, event);">
  <option>11</option>
  <option>12</option>
  <option>13</option>
</select>
<select ID="cbo_currenciesofamount2" runat="server" CssClass="DropDownCurveStyle" Width="100%" onfocus="Change_cbo(this, event)" onblur="Change_cbo(this, event)" onchange="cbo_currenciesofamount_IndexChange(this, event);">
  <option>21</option>
  <option>22</option>
  <option>23</option>
</select>
<select ID="cbo_currenciesofamount3" runat="server" CssClass="DropDownCurveStyle" Width="100%" onfocus="Change_cbo(this, event)" onblur="Change_cbo(this, event)" onchange="cbo_currenciesofamount_IndexChange(this, event);">
  <option>31</option>
  <option>32</option>
  <option>33</option>
</select>

Open in new window


function Change_cbo(cb, event) {
  var cbv = parseInt(cb.options[cb.selectedIndex].value);
  //alert(cbv);
  console.log("Change_cbo - " + cb.selectedIndex + " > " + cbv);
}

function cbo_currenciesofamount_IndexChange(cb, event) {
  var cbv = parseInt(cb.options[cb.selectedIndex].value);
  //alert(cbv);
  console.log("cbo_currenciesofamount_IndexChange - " + cb.selectedIndex + " > " + cbv);
}

Open in new window

https://jsfiddle.net/HainKurt/7vade2s3/
and put that js into say myuc.js

use this code on page_load of uc

        ' Define the name, type and url of the client script on the page.
        Dim csname As String = "myuc"
        Dim csurl As String = "~/myuc.js"
        Dim cstype As Type = Me.GetType()

        ' Get a ClientScriptManager reference from the Page class.
        Dim cs As ClientScriptManager = Page.ClientScript

        ' Check to see if the include script is already registered.
        If (Not cs.IsClientScriptIncludeRegistered(cstype, csname)) Then
            cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl))
        End If

Open in new window


this way, it will be registered once...

although adding it multiple times (the version I posted) will not create any issues...
and dont use anything like

<%=myControl.ClientId%>

in your js file, they will not work!
Avatar of Mayur Agarwal
Mayur Agarwal

Write a function in such a way that it accepts controlId instead of hardCoding in the function itself.
to mr hainkurt
i dont need to add 3 cbo in my web form i need a solution for adding 3 user control in a web form
is teher any solution to get control id in javascript common file?
to mr magur,
how to do that?
i dont need to add 3 cbo in my web form i need a solution for adding 3 user control in a web form 
is teher any solution to get control id in javascript common file?

Open in new window


I posted the solution...

1. create a js file
2. put those modified functions inside
3. register it (it will be added once)

whats the problem here?
Demo is to show how the end result will be...
Avatar of Julian Hansen
I suspect the solution here is quite easy however I must admit I am not clear on the requirements.

If you could show us with actual data examples so we can see what the required result will be it will help to clarify the issue.
check my posts again

ID: 42333646
ID: 42333650

you need:

1. modify your js functions as above
2. create a js file and put code inside and place somewhere in your web site
3. use the registration code on page_load of your user control...
ASKER CERTIFIED SOLUTION
Avatar of pamela rizk
pamela rizk
Flag of Lebanon image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it worked perfectly