var subsidylCost = document.getElementById("txtSubsidylCost.ClientID").value;
这样获取不到,因为txtSubsidylCost控件由Repeater控件包裹。
你要想获得Repeater里服务器控件(runat="server")的值 必须先知道Repeater里服务器控件ID的命名方式 。
首先Repeater里服务器控件ID的命名方式是 Repeater.ClientID_ctl行号_控件名。行号是从00开始的两位数00 01 02... 如 Repeater1_ctl00_lblBegin 表示Repeater里第一行lblBegin的ID。
代码如下:
<script type="text/javascript">
var repeaterId = '<%=Repeater1.ClientID %>';//Repeater的客户端ID
var rows = <%=Repeater1.Items.Count%>;//Repeater的行数
for (var i = 0; i < rows; i++) {
alert(document.getElementById(repeaterId + "_ctl" + getrownumber(i) + "_lblBegin").value);
alert(document.getElementById(repeaterId + "_ctl" + getrownumber(i) + "_lblDeadline").value);
}
function getrownumber(i) {
if (i > 10) {
return i;
}
else {
return '0' + i;
}
}
</script>
下面的例子是编辑Repeater行时求编辑行合计:
<asp:Repeater ID="rptReimburseDetail" runat="server" OnItemCommand="rptReimburseDetail_ItemCommand"
OnItemDataBound="rptReimburseDetail_RowDataBound">
<ItemTemplate>
<tr>
<td>
<asp:Literal ID="litStartTime" runat="server" Text='<%#Convert.ToDateTime(Eval("StartTime")).ToString("yyyy-MM-dd HH:mm:ss")%>'></asp:Literal>
<asp:TextBox ID="txtStartTime" runat="server" Text='<%#Convert.ToDateTime(Eval("StartTime")).ToString("yyyy-MM-dd HH:mm:ss")%>'
Visible="false" Width="90%" onfocus="WdatePicker({skin:'whyGreen',startDate:'%y-%M-%d 08:30:00',dateFmt:'yyyy-MM-dd HH:mm:ss'})"></asp:TextBox>
</td>
<td>
<asp:Literal ID="litEndTime" runat="server" Text='<%#Convert.ToDateTime(Eval("EndTime")).ToString("yyyy-MM-dd HH:mm:ss")%>'></asp:Literal>
<asp:TextBox ID="txtEndTime" runat="server" Text='<%#Convert.ToDateTime(Eval("EndTime")).ToString("yyyy-MM-dd HH:mm:ss")%>'
Visible="false" Width="90%" onfocus="WdatePicker({skin:'whyGreen',startDate:'%y-%M-%d %H:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss'})"></asp:TextBox>
</td>
<td>
<asp:Literal ID="litBusinessDays" runat="server" Text='<%#Eval("BusinessDays")%>'></asp:Literal>
<asp:TextBox ID="txtBusinessDays" runat="server" Text='<%#Eval("BusinessDays")%>'
Visible="false" onkeyup="CheckNewDecimal(this)" Width="90%"></asp:TextBox>
</td>
<td>
<asp:Literal ID="litStarting" runat="server" Text='<%#Eval("Starting")%>'></asp:Literal>
<asp:TextBox ID="txtStarting" runat="server" Text='<%#Eval("Starting")%>' Visible="false"
Width="90%" onkeyup='AutoDisplayTripCity(this);'></asp:TextBox>
</td>
<td>
<asp:Literal ID="litDestination" runat="server" Text='<%#Eval("Destination")%>'></asp:Literal>
<asp:TextBox ID="txtDestination" runat="server" Text='<%#Eval("Destination")%>' Visible="false"
Width="90%" onkeyup='AutoDisplayTripCity(this);'></asp:TextBox>
</td>
<td>
<asp:Literal ID="litSubsidylCost" runat="server" Text='<%#Eval("SubsidylCost")%>'></asp:Literal>
<asp:TextBox ID="txtSubsidylCost" runat="server" Text='<%#Eval("SubsidylCost")%>'
Visible="false" onkeyup="rptAutoDetailTotal(this)" Width="90%"></asp:TextBox>
</td>
<td>
<asp:Literal ID="litVehicleVesselCost" runat="server" Text='<%#Eval("VehicleVesselCost")%>'></asp:Literal>
<asp:TextBox ID="txtVehicleVesselCost" runat="server" Text='<%#Eval("VehicleVesselCost")%>'
Visible="false" onkeyup="rptAutoDetailTotal(this)" Width="90%"></asp:TextBox>
</td>
<td>
<asp:Literal ID="litAccommodationCost" runat="server" Text='<%#Eval("AccommodationCost")%>'></asp:Literal>
<asp:TextBox ID="txtAccommodationCost" runat="server" Text='<%#Eval("AccommodationCost")%>'
Visible="false" onkeyup="rptAutoDetailTotal(this)" Width="90%"></asp:TextBox>
</td>
<td>
<asp:Literal ID="litUrbanTrafficCost" runat="server" Text='<%#Eval("UrbanTrafficCost")%>'></asp:Literal>
<asp:TextBox ID="txtUrbanTrafficCost" runat="server" Text='<%#Eval("UrbanTrafficCost")%>'
Visible="false" onkeyup="rptAutoDetailTotal(this)" Width="90%"></asp:TextBox>
</td>
<td>
<asp:Literal ID="litOtherCost" runat="server" Text='<%#Eval("OtherCost")%>'></asp:Literal>
<asp:TextBox ID="txtOtherCost" runat="server" Text='<%#Eval("OtherCost")%>' Visible="false"
onkeyup="rptAutoDetailTotal(this)" Width="90%"></asp:TextBox>
</td>
<td>
<asp:Literal ID="litTotalCost" runat="server" Text='<%#Eval("TotalCost")%>'></asp:Literal>
<asp:TextBox ID="txtDetailTotal" runat="server" Width="90%" ReadOnly="true" Visible="false"
Text='<%#Eval("TotalCost")%>'></asp:TextBox>
</td>
<td>
<asp:Literal ID="litCurrency" runat="server" Text='<%#Eval("Currency")%>'></asp:Literal>
<asp:DropDownList ID="ddlCurrency" runat="server" Visible="false" Width="90%">
<asp:ListItem Text="--请选择--" Value=""></asp:ListItem>
<asp:ListItem Text="人民币" Value="人民币"></asp:ListItem>
<asp:ListItem Text="港币" Value="港币"></asp:ListItem>
<asp:ListItem Text="美元" Value="美元"></asp:ListItem>
</asp:DropDownList>
</td>
<td style="text-align: center">
<asp:LinkButton ID="btnDetailEdit" runat="server" Text="编辑" CommandName="Edit" CommandArgument='<%#Eval("ID")%>'></asp:LinkButton>
<asp:LinkButton ID="btnDetailDelete" runat="server" Text="删除" CommandName="Delete"
CommandArgument='<%#Eval("ID")%>'></asp:LinkButton>
<asp:LinkButton ID="btnDetailSave" runat="server" Text="保存" CommandName="Save" CommandArgument='<%#Eval("ID")%>'
Visible="false"></asp:LinkButton>
<asp:LinkButton ID="btnDetailCancel" runat="server" Text="取消" CommandName="Cancel"
CommandArgument='<%#Eval("ID")%>' Visible="false"></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
js代码如下:
function rptAutoDetailTotal(obj) {
//验证文本框中是否为数字
var reg = /^\d+\.*\d*$/i;
if (!reg.test(obj.value)) {
obj.value = isNaN(parseFloat(obj.value)) ? "" : parseFloat(obj.value);
}
//求和
if (obj.value != "") {
var detailTotal = 0;
var rptIndex = $('<%=hfrptIndex.ClientID %>').value;//编辑行索引
var subsidylCost = document.getElementById("ctl00_contentForm_rptReimburseDetail_ctl" + GetRowNumber(rptIndex) + "_txtSubsidylCost").value;
var vehicleVesselCost = document.getElementById("ctl00_contentForm_rptReimburseDetail_ctl" + GetRowNumber(rptIndex) + "_txtVehicleVesselCost").value;
var accommodationCost = document.getElementById("ctl00_contentForm_rptReimburseDetail_ctl" + GetRowNumber(rptIndex) + "_txtAccommodationCost").value;
var urbanTrafficCost = document.getElementById("ctl00_contentForm_rptReimburseDetail_ctl" + GetRowNumber(rptIndex) + "_txtUrbanTrafficCost").value;
var otherCost = document.getElementById("ctl00_contentForm_rptReimburseDetail_ctl" + GetRowNumber(rptIndex) + "_txtOtherCost").value;
detailTotal += isNaN(parseFloat(subsidylCost)) ? 0 : parseFloat(subsidylCost);
detailTotal += isNaN(parseFloat(vehicleVesselCost)) ? 0 : parseFloat(vehicleVesselCost);
detailTotal += isNaN(parseFloat(accommodationCost)) ? 0 : parseFloat(accommodationCost);
detailTotal += isNaN(parseFloat(urbanTrafficCost)) ? 0 : parseFloat(urbanTrafficCost);
detailTotal += isNaN(parseFloat(otherCost)) ? 0 : parseFloat(otherCost);
document.getElementById("ctl00_contentForm_rptReimburseDetail_ctl" + GetRowNumber(rptIndex) + "_txtDetailTotal").value = parseFloat(detailTotal);
}
}
function GetRowNumber(i) {
if (i > 10) {
return i;
}
else {
return '0' + i;
}
}