2012-08-27 17 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屬性,並將其用作搜索文本框的關鍵字。