Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

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?
0
Mike Miller
Asked:
Mike Miller
  • 7
  • 6
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now