Referencing controls in an ASP.NET Repeater using JavaScript

I'm currently using the following code to populate a textbox with values from other text boxes

<html>
<head>
<script>
function populate_it(){
  var d = document;
  d.getElementById("tbResult").value = d.getElementById("tbEntry1").value+"-"+d.getElementById("tbEntry2").value;
}
</script>
</head>
<body>
tbEntry1: <input type="text" name="tbEntry1" id="tbEntry1" oninput="populate_it()"><br/>
tbEntry2: <input type="text" name="tbEntry2" id="tbEntry2" oninput="populate_it()"><br/>
tbResult: <input type="text" name="tbResult" id="tbResult">
</body>
</html>

Open in new window


My textboxes are now all in a repeater nested in a GridView like so:

<asp:GridView ID="GridView1" runat="server" >
        <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
tbEntry1: <input type="text" name="tbEntry1" id="tbEntry1" oninput="populate_it()"><br/>
tbEntry2: <input type="text" name="tbEntry2" id="tbEntry2" oninput="populate_it()"><br/>
tbResult: <input type="text" name="tbResult" id="tbResult">
 </ItemTemplate>
<asp:Repeater>
</asp:GridView>

Open in new window


Obviously my script is no longer picking up. How do I change this so that I can find the controls in the repeater?
Mike MillerSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ersoy HasanCommented:
You wont be able to pick up the controls because Ids in the DOM must be unique. Better define a class in the inputs and select them by Document.getElementsByClassName() (https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByClassName) and give your inputs unique ids so you will know what you have selected.
0
Ersoy HasanCommented:
Here is a way to do it :
<!-- wrap your html inputs in a div with classname and give your inputs a class name -->
<div class="entry">
    tbEntry1: <input type="text" class="tbEntry1" name="tbEntry1" id="tbEntry1" oninput="populate_it(this)"><br/>
    tbEntry2: <input type="text" class="tbEntry2" name="tbEntry2" id="tbEntry2" oninput="populate_it(this)"><br/>
    tbResult: <input type="text" class="tbResult" name="tbResult" id="tbResult">
</div>

Open in new window

//and your function now should use getElementsByClassName
function populate_it(el){
    //traverse up to the div
    var d = el.parentElement;
    // select childs 
    d.getElementsByClassName("tbResult")[0].value = d.getElementsByClassName("tbEntry1")[0].value+"-"+d.getElementsByClassName("tbEntry2")[0].value;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike MillerSoftware EngineerAuthor Commented:
Second option doesnt work. In my repeater class on databound, I've added:

            Dim GI As TextBox = f.Item.FindControl("tbEntry1")
            Dim SN As TextBox = f.Item.FindControl("tbSEntry2")

            GI.Attributes.Add("oninput", "populate_it('" & GI.ClientID & "')")
            SN.Attributes.Add("oninput", "populate_it('" & SN.ClientID & "')")

Open in new window


What would be the proper script method now?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Mike MillerSoftware EngineerAuthor Commented:
nevermind, that wont work either, huh?
0
Ersoy HasanCommented:
GI.Attributes.Add("class", "tbEntry1");
GI.Attributes.Add("oninput", "populate_it(this)")
SN.Attributes.Add("class", "tbEntry2");
SN.Attributes.Add("oninput", "populate_it(this)")

Open in new window

and make the changes I showed you in the previous comment.

P.S.
You don't even have to do it in codebehind just change the html in the repeater. as I showed you.
0
Ersoy HasanCommented:
here is a fiddle with the second option.
http://jsfiddle.net/qlayer/mjvuL26z/
0
Mike MillerSoftware EngineerAuthor Commented:
HTML side:

<script type="text/javascript">
    window.populate_it = function populate_it(el) {
        //traverse up to the div
        var d = el.parentElement;
        // select childs 
        d.getElementsByClassName("tbResult")[0].value = d.getElementsByClassName("tbEntry1")[0].value + "-" + d.getElementsByClassName("tbEntry2")[0].value;
    }
</script>


        <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="OnRepeaterDataBound">
    <ItemTemplate>
    <div class="entry">
        <asp:TextBox ID="tbEntry1" runat="server" class="tbEntry1" name="tbEntry1" oninput="populate_it(this)"></asp:TextBox><br />
        <asp:TextBox ID="tbEntry2" runat="server" class="tbEntry2" name="tbEntry2" oninput="populate_it(this)"></asp:TextBox><br />
        <asp:TextBox ID="tbResult" runat="server" class="tbResult" name="tbResult"></asp:TextBox><br />
</div>
</ItemTemplate>
    </asp:Repeater>

Open in new window



CodeBehind:

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            BindData()
        End If
    End Sub
    Sub BindData()
        Dim ds As New Data.DataSet
        Dim da As New Data.SqlClient.SqlDataAdapter("SELECT * FROM tblWorkOrders WHERE ProjectID = '1'", connn)
        da.Fill(ds, "employees")
        Repeater1.DataSource = ds.Tables("employees")
        Repeater1.DataBind()
    End Sub
    Protected Sub OnRepeaterDataBound(s As Object, f As RepeaterItemEventArgs)
        If f.Item.ItemType = ListItemType.AlternatingItem Or f.Item.ItemType = ListItemType.Item Then
            Dim GI As TextBox = f.Item.FindControl("tbEntry1")
            Dim SN As TextBox = f.Item.FindControl("tbEntry2")

            GI.Attributes.Add("oninput", "populate_it('" & GI.ClientID & "')")
            SN.Attributes.Add("oninput", "populate_it('" & SN.ClientID & "')")
        End If
    End Sub

Open in new window


When I ViewSource:

    <div class="entry">
        <input name="Repeater1$ctl00$tbEntry1" type="text" id="Repeater1_ctl00_tbEntry1" class="tbEntry1" name="tbEntry1" oninput="populate_it(&#39;Repeater1_ctl00_tbEntry1&#39;)" /><br />
        <input name="Repeater1$ctl00$tbEntry2" type="text" id="Repeater1_ctl00_tbEntry2" class="tbEntry2" name="tbEntry2" oninput="populate_it(&#39;Repeater1_ctl00_tbEntry2&#39;)" /><br />
        <input name="Repeater1$ctl00$tbResult" type="text" id="Repeater1_ctl00_tbResult" class="tbResult" name="tbResult" /><br />
</div>

Open in new window


What am I doing wrong??
0
Ersoy HasanCommented:
This has to be removed:
GI.Attributes.Add("oninput", "populate_it('" & GI.ClientID & "')")
SN.Attributes.Add("oninput", "populate_it('" & SN.ClientID & "')")

Open in new window

or changed to this:
GI.Attributes.Add("oninput", "populate_it(this)")
SN.Attributes.Add("oninput", "populate_it(this)")

Open in new window

0
Mike MillerSoftware EngineerAuthor Commented:
I actually did like you said and just removed the code behind. Here's my code now:

HTML:

<head runat="server">
    <title></title>

<script type="text/javascript">
    window.populate_it = function populate_it(el) {
        //traverse up to the div
        var d = el.parentElement;
        // select childs 
        d.getElementsByClassName("tbResult")[0].value = d.getElementsByClassName("tbEntry1")[0].value + "-" + d.getElementsByClassName("tbEntry2")[0].value;
    }
</script>

</head>
<body>
    <form id="form1" runat="server">
  
        <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
    <div class="entry">
        <asp:TextBox ID="tbEntry1" runat="server" class="tbEntry1" name="tbEntry1" oninput="populate_it(this)"></asp:TextBox><br />
        <asp:TextBox ID="tbEntry2" runat="server" class="tbEntry2" name="tbEntry2" oninput="populate_it(this)"></asp:TextBox><br />
        <asp:TextBox ID="tbResult" runat="server" class="tbResult" name="tbResult"></asp:TextBox><br />
</div>
</ItemTemplate>
    </asp:Repeater>
    </form>
</body>
</html>

Open in new window


code behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            BindData()
        End If
    End Sub
    Sub BindData()
        Dim ds As New Data.DataSet
        Dim da As New Data.SqlClient.SqlDataAdapter("SELECT * FROM tblWorkOrders WHERE ProjectID = '1'", connn)
        da.Fill(ds, "employees")
        Repeater1.DataSource = ds.Tables("employees")
        Repeater1.DataBind()
    End Sub

Open in new window


ViewSource:

    <div class="entry">
        <input name="Repeater1$ctl00$tbEntry1" type="text" id="Repeater1_ctl00_tbEntry1" class="tbEntry1" name="tbEntry1" oninput="populate_it(this)" /><br />
        <input name="Repeater1$ctl00$tbEntry2" type="text" id="Repeater1_ctl00_tbEntry2" class="tbEntry2" name="tbEntry2" oninput="populate_it(this)" /><br />
        <input name="Repeater1$ctl00$tbResult" type="text" id="Repeater1_ctl00_tbResult" class="tbResult" name="tbResult" /><br />
</div>

    <div class="entry">
        <input name="Repeater1$ctl01$tbEntry1" type="text" id="Repeater1_ctl01_tbEntry1" class="tbEntry1" name="tbEntry1" oninput="populate_it(this)" /><br />
        <input name="Repeater1$ctl01$tbEntry2" type="text" id="Repeater1_ctl01_tbEntry2" class="tbEntry2" name="tbEntry2" oninput="populate_it(this)" /><br />
        <input name="Repeater1$ctl01$tbResult" type="text" id="Repeater1_ctl01_tbResult" class="tbResult" name="tbResult" /><br />
</div>

Open in new window


Still no luck
0
Ersoy HasanCommented:
what browser are you trying this  (because getElementsByClassName wont work on ie8) ?
I am trying your code in fiddle and it works. (see http://jsfiddle.net/qlayer/mjvuL26z/1/)
0
Mike MillerSoftware EngineerAuthor Commented:
Awww man...that was it. IE8 is the default browser for my IDE. Just tried it in Chrome and it worked fine. Sorry about that! Thanks so much for sticking with me.
0
Mike MillerSoftware EngineerAuthor Commented:
FYI..

This isn't working if I put the controls in a table inside the DIV unless they are all in the same TD
Question here:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28552285.html
0
Mike MillerSoftware EngineerAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.