【问题标题】:Let ASP.NET repeater set a dynamic name让 ASP.NET 中继器设置一个动态名称
【发布时间】:2012-08-22 06:07:13
【问题描述】:

我使用 javascript 来启用禁用文本框并根据选中的单选按钮计算价格。

我遇到的问题是我使用单选按钮的组名来找到正确的文本框,并在计算中使用它的值。当转发器从我的 itemtemplate 创建代码时,它不使用提供的参数来创建名称。

我在项目标签中的名字是: name='' 它创建的名称是 name="Repeater1$ctl02$textbox" 以及我提供的名称标签。 对于单选按钮,它们的名称变为 Repeater1$ctl01$sverigemotrasism 不公正 超移性 有没有办法阻止它创建名称......或者我该如何规避它?

这是我的项目模板

    <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" />

【问题讨论】:

  • 这是什么意思:问题是当中继器让我们自己的权力松动时?我不知道你想说什么。
  • 您使用的是哪个框架版本?
  • 在行数据绑定事件中创建您的单选按钮。通过这种方式,您将对命名有更多的控制权。

标签: javascript asp.net


【解决方案1】:

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

不过,有时您不希望 ID 值分层嵌套,而只是希望呈现的 ID 是您设置的任何值。要启用此功能,您现在可以使用 ClientIDMode=static,在这种情况下,呈现的 ID 将与您在控件的服务器端设置的完全相同。

如果您使用name 属性,您应该使用getElementsByName 而不是getElementById 可能您想设置文本框ID 而不是名称。考虑到id 属性对于每个控件必须是唯一的。

我不确定GroupName 使用上述建议设置是否会是静态的。值得测试一下,如果不行,你可以把CssClass属性设置成相同的值,作为key来搜索文本框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多