0

I have a repeater control:

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

then in the codebehind I register the script

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

then on the itemdatabound event add the attribute

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

If I just pass in any text box with client ID it finds it and I am able to display results in the alert but if I pass in any label I get the error message in the error console

Error: ctl00_ContentPlaceHolder1_ListTable1_repeater_ctl00_lblqty is not defined

Can anyone tell me how to resolve this?

Rob W
  • 341,306
  • 83
  • 791
  • 678

1 Answers1

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
rick schott
  • 21,012
  • 5
  • 52
  • 81
  • that makes no difference if I remove the .id part or not – user1067767 Nov 27 '11 at 10:55
  • See update, you weren't getting that far but t would have been another error, your first problem is single quotes on EditLabel params. – rick schott Nov 27 '11 at 11:00
  • I have change my code to your suggestion. Still no joy. I change it to txtbxqty.Attributes.Add("onblur", "javascript:Editlabel(" + txtbxqty.ClientID + ");") this displays the textbox value but as soon as I add the label it stops working – user1067767 Nov 27 '11 at 11:25
  • `innerText` is IE only. Use `lbl.innerHTML` instead, or see alternative [here](http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox). – Shadow The GPT Wizard Nov 27 '11 at 14:40
  • Updated.....done debugging, recommend jQuery, takes care of these silly mistakes. – rick schott Nov 27 '11 at 18:20