2011-12-19 153 views
0

我已經創建了一個WebUserControl,它包含三個html控件,兩個按鈕和一個輸入文本。這些控件中的每一個都有靜態標識,這是錯誤的,我不這麼認爲。控件事件使用javascript進行初始化,控制傳遞給客戶端後,告訴我他希望放置五個,如果此用戶控件在同一頁上,每個事件都有不同的參數。任何觸發的事件都會初始化所有用戶控件,因爲JavaScript採用TextBox用戶標識。WebUserControl動態內容?

下面是代碼:

  //Markup code in user control, each control have a static id 
      <input id="Text_Output" type="text" runat="server" onkeypress="return saveValue(event);" /> 

      <input id="Button_Add" type="button" runat="server" value="ˆ" 
       style="width:21px; height:14px;vertical-align:bottom;" /> 

      <input id="Button_Subtract" type="button" runat="server" value="ˇ" 
       style="width:21px; height:14px;vertical-align:top;" /> 

    //Javascript function Add() in user control 
    function Add(incValue, percValue) { 

    var outputValue = document.getElementById('<%=Text_Output.ClientID%>').value; 
    var incremeantValue = incValue; 
    var precsionValue = percValue; 

    if (outputValue.indexOf(".") > 0) { 
     var total = (parseFloat(outputValue) + parseFloat(incremeantValue)); 
     document.getElementById('<%=Text_Output.ClientID%>').value = parseFloat(total).toFixed(parseInt(percValue)).toString(); 
    } 
    else { 
     if (percValue == 0) { 
      document.getElementById('<%=Text_Output.ClientID%>').value = parseInt((parseFloat(outputValue) + parseFloat(incremeantValue)).toString()); 
     } 
     else { 
      var total = (parseFloat(outputValue) + parseFloat(incremeantValue)); 
      document.getElementById('<%=Text_Output.ClientID%>').value = parseFloat(total).toFixed(parseInt(percValue)).toString(); 
     } 
    } 
} 




//Markup code used in webpage 
//This button will connect(initialize) the events for the user control 
<input id="Button1" type="button" value="button" onclick="IntilizeControl()" /><br /> 
<My:UserInfoBoxControl ID="JSNumeric_Control" runat="server" /> 

    //Javasript InilizeControl() function 
    function IntilizeControl() { 

    var Button_Add = document.getElementById('<%=JSNumeric_Control.FindControl("Button_Add").ClientID %>'); 
    //Add(IncrementValue,PrecesionValue) 
    Button_Add.setAttribute("onclick", "Add(1,1)", '<%=JSNumeric_Control.FindControl("Text_Output").ClientID%>'); 

    var Button_Subtract = document.getElementById('<%=JSNumeric_Control.FindControl("Button_Subtract").ClientID%>'); 
    //Subtract(IncrementValue,PrecesionValue) 
    Button_Subtract.setAttribute("onclick", "Subtract(1,1)"); 

    var Text_Output = document.getElementById('<%=JSNumeric_Control.FindControl("Text_Output").ClientID%>'); 
    alert('<%=JSNumeric_Control.FindControl("Text_Output").ClientID%>'); 
    Text_Output.setAttribute("onkeyup", "check()"); 
    Text_Output.value = 0; 
} 

如果我添加其他用戶的控制,將用相同的一切,即使我已經改變了用戶控件的ID上的document.getElementById部分初始化,我希望每個用戶控件都擁有自己的客戶端ID,這樣每個用戶都可以按照自己的方式進行操作?

+0

我解決了它,我所做的是在用戶控件內更改我的JavaScript代碼以接受新參數controlClientID,並使用此ID代替Text_Output – 2011-12-19 14:12:01

回答

0
//Send the controlClientID from the source code. 
    function Add(incValue, percValue,controlID) { 
    //The controlID.name.replace(/\$/g, '_') will replace the $ sign by the _ character. 
    var outputValue = document.getElementById(controlID.name.replace(/\$/g, '_')).value; 
    var incremeantValue = incValue; 
    var precsionValue = percValue; 

    if (outputValue.indexOf(".") > 0) { 
     var total = (parseFloat(outputValue) + parseFloat(incremeantValue)); 
     document.getElementById(controlID.name.replace(/\$/g, '_')).value = parseFloat(total).toFixed(parseInt(percValue)).toString(); 
    } 
    else { 
     if (percValue == 0) { 
      document.getElementById(controlID.name.replace(/\$/g, '_')).value = parseInt((parseFloat(outputValue) + parseFloat(incremeantValue)).toString()); 
     } 
     else { 
      var total = (parseFloat(outputValue) + parseFloat(incremeantValue)); 
      document.getElementById(controlID.name.replace(/\$/g, '_')).value = parseFloat(total).toFixed(parseInt(percValue)).toString(); 
     } 
    } 
}