在.NET应用程序中,我创建了一个用户控件,它使用转发器动态创建行(所以我不能只选择ID,甚至客户ID),所以通常我&# 39;已经跳到包含控件和触发事件的父行,该行基于该行中的选择器。
对于这个例子,我在更改下拉列表索引时触发了一个事件。此事件将根据“启动年”下拉列表中选择的年份启用/禁用文本框。在这种情况下,我执行循环,从开始年份开始(页面上的隐藏字段),并循环直到开始年份+ 15,并相应地禁用/启用。应禁用所选发射年之前的任何年份。
以下是控件:
以下是代码。但首先,让我简单介绍一下我真正想要实现的目标。这可能是一种更简单的方法,我只是一个jQuery新手。
首先,我得到刚刚解雇的下拉列表的父行:
var parentRow = $this.closest('tr');
然后我想使用年份索引获取第n个类型的文本框,并相应地禁用/启用。这显然根本不起作用,表现得非常缓慢,令我感到困惑。
$(parentRow.attr("id") + "input[type=text]type:nth-of-type(" + (i - startYear) + ")").attr('disabled', '');
以下全部功能:
function LaunchChanged(ddl) {
var $this = $(ddl);
var parentRow = $this.closest('tr');
//alert("Value is " + ddl.value + "\n Old Value is " + ddl.oldvalue);
if ($this.attr("id").indexOf('Year') != -1) {
if ($this.val() == -1) {
DisableTextBoxes(parentRow);
ClearTextBoxes(parentRow);
} else {
var startYear = $("input[type=hidden][id*='hfStartYear']").val();
for (var i = startYear; i <= startYear + 15 ; i++) {
if (i >= $this.val()) {
$(parentRow.attr("id") + "input[type=text]type:nth-of-type(" + (i - startYear) + ")").attr('disabled', '');
} else {
$(parentRow.attr("id") + "input[type=text]:nth-of-type(" + (i - startYear) + ")").attr('disabled', 'disabled');
}
}
}
}
//ShowModalPopup();
}
按请求,.NET转发器代码如下所示:
<asp:Repeater runat="server" ID="rptTotalSales" ItemType="CHIPS.Entities.SalesControl">
<ItemTemplate>
<tr runat="server" ID="row">
<td>
<span runat="server" ID="expandButton" class="pointer" Visible="False">+</span>
<asp:CheckBox runat="server" Checked='<%#Item.IsActivated%>' ID="cbActivated"/>
<asp:Label runat="server" ID="lblRegionName" Text='<%#Item.RegionName%>' Visible="False"></asp:Label>
<asp:Label runat="server" ID="lblSubRegionName" Text='<%#Item.SubRegionName%>' Visible="False"></asp:Label>
</td>
<td><asp:DropDownList Enabled="<%#Item.IsActivated%>" onfocus="this.oldvalue = this.value;" onchange="LaunchChanged(this);this.oldvalue = this.value;" runat="server" ID="ddlLaunchMonth" AppendDataBoundItems="True"/></td>
<td><asp:DropDownList Enabled="<%#Item.IsActivated%>" onfocus="this.oldvalue = this.value;" onchange="LaunchChanged(this);this.oldvalue = this.value;" runat="server" ID="ddlLaunchYear" AppendDataBoundItems="True"/></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear1)%>' ID="txtSalesYear1"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear2)%>' ID="txtSalesYear2"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear3)%>' ID="txtSalesYear3"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear4)%>' ID="txtSalesYear4"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear5)%>' ID="txtSalesYear5"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear6)%>' ID="txtSalesYear6"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear7)%>' ID="txtSalesYear7"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear8)%>' ID="txtSalesYear8"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear9)%>' ID="txtSalesYear9"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear10)%>' ID="txtSalesYear10"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear11)%>' ID="txtSalesYear11"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear12)%>' ID="txtSalesYear12"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear13)%>' ID="txtSalesYear13"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear14)%>' ID="txtSalesYear14"></asp:TextBox></td>
<td><asp:TextBox Enabled="<%#Item.IsActivated%>" runat="server" Text='<%#:String.Format("{0:f4}", Item.SalesYear15)%>' ID="txtSalesYear15"></asp:TextBox></td>
</tr>
<asp:HiddenField runat="server" Value='<%#Item.RegionID%>' ID="hfRegionID"/>
<asp:HiddenField runat="server" Value='<%#Item.SubRegionID%>' ID="hfSubRegionID"/>
<asp:HiddenField runat="server" Value='<%#Item.LaunchMonth%>' ID="hfLaunchMonth"/>
<asp:HiddenField runat="server" Value='<%#Item.LaunchYear%>' ID="hfLaunchYear"/>
<asp:HiddenField runat="server" Value='<%#Item.Gate0ActualDate%>' ID="hfGate0ActualDate"/>
<asp:HiddenField runat="server" Value='<%#Item.HasChild%>' ID="hfHasChild"/>
<asp:HiddenField runat="server" Value='<%#Item.ProjectID%>' ID="hfProjectID" />
<asp:HiddenField runat="server" Value='<%#Item.StartYear%>' ID="hfStartYear" />
</ItemTemplate>
</asp:Repeater>