我有一个转发器控件:
<asp:Repeater ID="repeater" runat="server" >
<ItemTemplate>
<tr>
<td><asp:CheckBox class="checkbox" id="chkbox" runat="server" /></td>
<td><asp:label ID="lblCode" runat="server" Text='<%#Eval("productCode")%>' /></td>
<td><asp:Label ID="lbldesc" runat="server" Text='<%# Eval("productDesc") %>' /></td>
<td><asp:TextBox ID="txtType" runat="server" Text='<%# Eval("productType") %>' /></td>
<td><asp:TextBox ID="txtqty" runat="server" Text='<%#eval("productQty") %>' size="10" />
<asp:label ID=lblqty runat="server" Text='<%#eval("productQty") %>' />
</td>
<td><asp:TextBox ID=txtPrice runat="server" Text='<%# eval("productprice") %>' class="text" Width="30px"/></td>
<td>
<asp:linkbutton ID="lnkEdit" runat="server"><img src="images/ico_edit_16.png" class="icon16 fl-space2" alt="" title="edit" /></asp:linkbutton>
<asp:linkbutton ID="LinkDelete" runat="server"><img src="images/ico_delete_16.png" class="icon16 fl-space2" alt="" title="delete" /></asp:linkbutton >
<asp:linkbutton ID="LinkSettings" runat="server"><img src="images/ico_settings_16.png" class="icon16 fl-space2" alt="" title="settings" /></asp:linkbutton >
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
然后在代码隐藏中我注册脚本
Dim csname2 As String = "TextOnblurScript"
If Not Page.ClientScript.IsClientScriptBlockRegistered(csname2) Then
Dim cstext2 As New StringBuilder()
cstext2.Append("<script type=""text/javascript""> function Editlabel(qtylbl,txtbox) {")
cstext2.Append("var lbl = document.getElementById(qtylbl.id);")
cstext2.Append("var txt = document.getElementById(txtbox.id);")
cstext2.Append("alert(txt.value + ' ' + lbl.value);")
cstext2.Append("} </")
cstext2.Append("script>")
Page.ClientScript.RegisterClientScriptBlock(Me.GetType, csname2, cstext2.ToString())
End If
然后在itemdatabound事件上添加属性
Protected Sub repeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repeater.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim lblcode As Label = e.Item.FindControl("lblcode")
Dim txtbxqty As TextBox = e.Item.FindControl("txtqty")
Dim lblqty As Label = e.Item.FindControl("lblqty")
Dim txtprice As TextBox = e.Item.FindControl("txtPrice")
txtbxqty.Attributes.Add("onblur", "javascript:Editlabel(" + lblqty.ClientID + "," + txtbxqty.ClientID + ");")
End If
End Sub
如果我只是传入任何带有客户端ID的文本框,它就会找到它并且我能够在警报中显示结果但是如果我传入任何标签,我会在错误控制台中收到错误消息
错误:未定义ctl00_ContentPlaceHolder1_ListTable1_repeater_ctl00_lblqty
谁能告诉我如何解决这个问题?
答案 0 :(得分:0)
//need single quotes on js function
txtbxqty.Attributes.Add("onblur", "javascript:Editlabel('" + lblqty.ClientID + "','" + txtbxqty.ClientID + "');")
//no need for id
cstext2.Append("var lbl = document.getElementById(qtylbl);")
cstext2.Append("var txt = document.getElementById(txtbox);")
cstext2.Append("alert(txt.value + ' ' + lbl.innerHtml);") //span don't have a value