Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help looping through and populating text fields

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
I have an asp.net gridview (not sure if that even matters) which displays an undetermined number of rows. Each row has an input field (textbox) for a date. I need the ability to allow the user to enter a date in the input field in the first row, then click a link that triggers a javascript call that will populate each remaining row's date field with the value that the user entered in the first row. What I've already done is programmatically add an attribute to each row called 'index'. Which is numbered sequentially starting from zero. I was thinking this would be an easy way to populate the remaining input fields, where their index value was not equal to zero.

I just need help with the looping through the rows and population of the fields. I think I'm close but I'm not sure.

Here's the JS:
Code:
function fillAll(theGrid, rowIdx)
    {
        var tbl = document.getElementById(theGrid);
        
        for(var i = 1; i< tbl.rows.length;i++)
        {
            if (document.getElementById('index').value != 0 )
            {
                document.getElementById('<%=txtRequestDate.ClientID %>').value = document.getElementById(rowIdx).value;
            }
        }
    }
And the link that calls the JS:
Code:
javascript:fillAll(&#39;gvCheckout&#39;, &#39;" +((GridViewRow)Container).FindControl("txtRequestDate").ClientID + "&#39;)
...which passes the hardcoded name of the gridview, and the dynamic name of the first row's date field.

Any help is greatly appreciated.
 
I think you're confusing attributes on the TR and IDs, as you're doing document.getElementById('index') for each iteration instead of looking for an 'index' attribute on each row (then again, do you need this?)...

Why not just look over all rows, do a 'getElementsByTagName' on that row to get the first input (or whichever index it is), and then set its value?

P.S. Can you post real client-side code instead of unparsed server-side code if you need further help? We have no idea what the browser sees...

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
I was finally able to figure this out...here's the working code for anyone who may need it in the future.

The JS:
Code:
function fillAll(frstTxtBox)
    {
        // get the value of the first date input field
        var frstBoxVal = document.getElementById(frstTxtBox).value;
        // get all of the input elements
        var inputElements = document.getElementsByTagName("input");
        
        for (i = 0; i < inputElements.length; i++) 
        {
            // if it's a text box and has an 'index' attribute witht a value greater than zero...
            if (inputElements[i].type == "text" && inputElements[i].index > 0) 
            {
                inputElements[i].value = frstBoxVal;
            }
        }
    }

The ASP.NET:
Code:
<asp:TextBox ID="txtRequestDate" Text='' Width="75" runat="server"></asp:TextBox>
<asp:HyperLink ID="hlFill" NavigateUrl='<%# "javascript:fillAll(&#39;" +((GridViewRow)Container).FindControl("txtRequestDate").ClientID + "&#39;)"%>' Text="Copy to All" Visible="false" runat="server" />

The C# code-behind:
Code:
protected void gvCheckout_RowCreated(Object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 1; i < e.Row.Cells.Count - 1; i++)
                {
                    ((TextBox)e.Row.FindControl("txtRequestDate")).Attributes.Add("index", e.Row.RowIndex.ToString());
                    if (e.Row.RowIndex == 0)
                    {
                        e.Row.FindControl("hlFill").Visible = true;
                    }
                }
                
            }
            
        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top