views:

93

answers:

3

i have a control i created behind code

    Dim txtdate1 As New TextBox
    txtdate1.ID = "txtdate1"
    cell.Controls.Add(txtdate1)

Cell is a header table cell i added to a gridview.

I need to access this in javascript

var txtdate1 = document.getElementById('txtdate1');

OR

var txtdate1 = document.getElementById('<%=txtdate1.clientID');

How can this be done?

This is how the cell is created

Dim cell As New TableCell
Dim row As New GridViewRow(-1, -1, DataControlRowType.Separator, DataControlRowState.Normal)
    row.BackColor = System.Drawing.ColorTranslator.FromHtml("#B5C7DE")
    row.ForeColor = Drawing.Color.White
    If GridView1.Rows.Count <> 0 Then
        GridView1.Rows(0).Parent.Controls.AddAt(0, row)
    Else

    End If

    row.Controls.Add(labelcell)
    row.Cells.Add(cell)
A: 

You should be able to use the built-in $find function of MS Ajax in conjunction with ClientID.

var txtdate1 = $find('<%=txtdate1.ClientID %>');
ichiban
i get the following:Name 'txtdate1' is not declared
Eric
The error is correct, because the control is not declared on the page. You're dealing with a dynamic control here, so you need to do a dynamic lookup.
Y Low
@Y Low- How would i use dynamic lookup in this situation? I've never hear of it.
Eric
You need to use cell.FindControl("txtdate1") to find the control you're looking for in the cell child control collection. (Check my answer)
Y Low
+1  A: 

Try:

var txtdate1 = $find('<%= cell.FindControl("txtdate1").ClientID %>');

Or when you create the control dynamically in code, save the client id to a property of the page and then render the property.

First create a class property:

Dim txtID As String        'This should be a class property

Then when you create the control dynamically, save the ClientID:

Dim txtdate1 As New TextBox
txtdate1.ID = "txtdate1"
cell.Controls.Add(txtdate1)
Me.txtID = txtdate1.ClientID  'Save the client id to a property

Then in javascript you can load the control like this:

var txtdate1 = $find('<%= txtID %>');
Y Low
the issue with this is that cell is dynamically created too!I'll edit my question again.
Eric
Then just use a page variable to save the ClientID, (second part of my answer)
Y Low
i get this error now:'...txtid' is not accessible in this context because it is 'Private'.
Eric
what do you mean create txtid as a class property? maybe that's what I'm doing wrong
Eric
Make it Protected
Y Low
I did something a little different. I set the clientid value in a hidden field. I am able to access the Hidden field via javascript but Oddly, the clientid of the textbox is the same as its id. However,It is still not finding the textbox. any idea what is up??
Eric
The ClientID will only be fully generated once you added the control to its parent (after the cell.Controls.Add() call) of course the parent control must be added to ITS parent control and so on all the way up to the Page. (In your case make sure that when you add textbox to cell, cell should already be added to row and row should already be added to the grid and the grid should already be added to the page)
Y Low
A: 

You've almost got it. Actually, I'm surprised if your second line would compile.

var txtdate1 = document.getElementById('<%= cell.FindControl("txtdate1").ClientID %>');

By the way, folks, there is a difference between Javascript and jquery, and the OP never indicated the presence of jquery.

harpo
the issue with this is that cell is dynamically created too!I'll edit my question again.
Eric
Only you have indicated the presence of jquery...
Chris Missal
I'm referring to the other two answers, which replace a native function with $find.
harpo
Why do you think $find is a jquery function? It's actually an ASP.NET AJAX function.
Y Low
Sorry, should have been more clear, Y Low reiterates my same thoughts.
Chris Missal