2012-08-27 14 views
0

使用JavaScript来启用禁用文本框并根据检查单选按钮计算价格。让ASP.NET中继器设置一个动态名称

我遇到的问题是我使用radiobutton的groupname来查找正确的文本框,并在计算中使用它的值。当中继器从我的itemtemplate创建代码时,它不会使用超级参数来创建名称。

我在该项目的标记名称是: 名= '<%#DataBinder.Eval的(的Container.DataItem, “idtag_domain”)%>' 它创建的名称是NAME = “Repeater1 $ ctl02 $文本框” 藏汉作为我提供的名称标签。 对于单选按钮的名称变为 Repeater1 $ ctl01 $ sverigemotrasism isnted只是 sverigemotrasism 的是有没有办法从创建名字阻止它......或者我该如何规避?

这是我的ItemTemplate

<ItemTemplate> 
    <asp:RadioButton id="RadioButton1" runat="server" GroupName='<%# DataBinder.Eval(Container.DataItem, "idtag_domain")%>' value="50" Text="Fri tillgång" onclick="calculatePrice();disableTB(this.name);" /> 
    <br /> 
    <asp:RadioButton id="RadioButton2" runat="server" GroupName='<%# DataBinder.Eval(Container.DataItem, "idtag_domain")%>' 
     Text="En artikel om dagen(30/mån)" value="25" onclick="calculatePrice();disableTB(this.name);" /> 
     <br /> 
    <asp:RadioButton id="RadioButton3" runat="server" GroupName='<%# DataBinder.Eval(Container.DataItem, "idtag_domain")%>' value="0" 
     Text="Skriv antalet artiklar" onclick="enableTB(this.name, this.checked)" /> 
     <br /> 
    <asp:TextBox ID="textbox" runat="server" name='<%# DataBinder.Eval(Container.DataItem, "idtag_domain") %>' Enabled="false" Width="106px" 
     onkeyup="calculatePrice()" style="background-color:#eeeeee" ></asp:TextBox> 
    </ItemTemplate> 

我的JavaScript干这种事!找出被点击的文本框。

function enableTB(tbname, checked) 
{ 
var textboxen = tbname; 
document.getElementById(textboxen).disabled = !checked; 
document.getElementById(textboxen).style.background = '#C4F8CC'; 
} 

function disableTB(tbname) 
{ 
var textboxen = tbname + 1; 
document.getElementById(textboxen).disabled = true; 
document.getElementById(textboxen).style.background = '#eeeeee'; 
} 

不幸的是这是我的源代码看起来像中继器已经创造了一切...... 中继数据后!

<table> 

    <input id="Repeater1_RadioButton1_0" type="radio" name="Repeater1$ctl01$sverigemotrasism" value="50" onclick="calculatePrice();disableTB(this.name);" /><label for="Repeater1_RadioButton1_0">Fri tillgång</label> 
    <br /> 
    <input id="Repeater1_RadioButton2_0" type="radio" name="Repeater1$ctl01$sverigemotrasism" value="25" onclick="calculatePrice();disableTB(this.name);" /><label for="Repeater1_RadioButton2_0">En artikel om dagen(30/mån)</label> 
     <br /> 
    <input id="Repeater1_RadioButton3_0" type="radio" name="Repeater1$ctl01$sverigemotrasism" value="0" onclick="enableTB(this.name, this.checked);" /><label for="Repeater1_RadioButton3_0">Skriv antalet artiklar</label> 
     <br /> 
    <input name="Repeater1$ctl01$textbox" type="text" id="Repeater1_textbox_0" disabled="disabled" class="aspNetDisabled" onkeyup="calculatePrice()" name="sverigemotrasism" style="width:106px;background-color:#eeeeee" /> 



    <tr> 
    <td> 




    </td> 
    </tr> 

    <input id="Repeater1_RadioButton1_1" type="radio" name="Repeater1$ctl02$handlaihop" value="50" onclick="calculatePrice();disableTB(this.name);" /><label for="Repeater1_RadioButton1_1">Fri tillgång</label> 
    <br /> 
    <input id="Repeater1_RadioButton2_1" type="radio" name="Repeater1$ctl02$handlaihop" value="25" onclick="calculatePrice();disableTB(this.name);" /><label for="Repeater1_RadioButton2_1">En artikel om dagen(30/mån)</label> 
     <br /> 
    <input id="Repeater1_RadioButton3_1" type="radio" name="Repeater1$ctl02$handlaihop" value="0" onclick="enableTB(this.name, this.checked);" /><label for="Repeater1_RadioButton3_1">Skriv antalet artiklar</label> 
     <br /> 
    <input name="Repeater1$ctl02$textbox" type="text" id="Repeater1_textbox_1" disabled="disabled" class="aspNetDisabled" onkeyup="calculatePrice()" name="handlaihop" style="width:106px;background-color:#eeeeee" /> 
+0

这是什么意思:问题是,当中继器让它的自己的力量松动?我不知道你在那里想说什么。 –

+1

可能重复的[ASP.NET INamingContainer - 可选的前缀](http://stackoverflow.com/questions/4461463/asp-net-inamingcontainer-optional-prefixes) – jrummell

+0

你正在使用哪个框架版本? –

回答

1

如果您使用的框架4.0,你可以使用静态的id为您的控件。尝试设置 ClientIDMode =静态

有时候你不想让你的ID值被分层嵌套,不过,取而代之的只是想呈现的是什么价值,你将其设置为ID。要启用此功能,您现在可以使用ClientIDMode = static,在这种情况下,呈现的ID将与您在控件上的服务器端上设置的ID完全相同。

如果你使用,你应该使用的getElementsByName代替getElementByIdname财产也许你要设置的文本框的ID,而不是名称。考虑到id属性必须对每个控件都是唯一的。

我不确定GroupName是否使用上述建议设置为静态。值得测试一下,如果不起作用,您可以使用相同的值设置CssClass属性,并将其用作搜索文本框的关键字。