0

我创建了一个 WebUserControl,它包含三个 html 控件、两个按钮和一个输入文本。这些控件中的每一个都有静态ID,我不这么认为是错误的。控件事件是使用javascript初始化的,在控件交付给客户端后告诉我,如果这个用户控件在同一页面上每个都有不同的事件参数,他想放五个。由于 javascript 采用 TextBox UserID,因此触发的任何事件都会初始化所有用户控件。

这是代码:

            //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;
}

如果我添加另一个用户控件,即使我在 document.getElementByID 部分更改了用户控件的 ID,它将使用相同的所有内容进行初始化,我希望每个用户控件都有自己的客户端 ID,以便每个用户都可以对其进行操作; 自己的方式?

4

1 回答 1

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();
        }
    }
}
于 2011-12-20T07:07:23.497 回答