Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Javascript inside a datgrid

Javascript inside a datgrid

Javascript inside a datgrid

I have a dropdown menu and when the selected index changes, I want to call a javascript function to check the value and then disply/hide a 2nd dropdown.  None of this would be difficult except for the fact that this takes place inside of a datagrid so my dropdown names change to something like dgrdInteractions:_ctl4:add_material and I don't know how to best refer to them in my javascript.

I know I could use the OnSelectedIndexChanged function but I'd rather use Javascript and avoid a page refresh every time the dropdown changes.  Any thoughts?

RE: Javascript inside a datgrid

you can add the javascript function to the dropdownlist on the itemdatabound event of the datagrid bind....


Private Sub dtGoals_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dtGoals.ItemDataBound
 If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
 'Initialize all the controls for the item
  Dim ddRating As DropDownList = CType(e.Item.FindControl("ddRating"), DropDownList)
  ddRating.Attributes.Add("onchange", "javascript:onDDchange('" & ddRating.ClientID & "','" & txtWeight.Text & "','" & txtOldRating.ClientID & "');return false;")
    End If
  End Sub

the function onDDchange is my javascript function that the dropdownlist will call on change...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."

RE: Javascript inside a datgrid

You would have to use the javascript equivilent of FindControl().  Get a reference to the row in the grid that the dropdown is changed in, then find the dropdown list (by the name you assigned), then grab the value etc....

RE: Javascript inside a datgrid

>>You would have to use the javascript equivilent of FindControl().  

thats very difficult as the acutal ids that are generated are not the ids that we set server side. especially for datagrid controls...

Known is handfull, Unknown is worldfull

RE: Javascript inside a datgrid

Very true vbkris.. wasn't completly thinking in JS mode.   Like your solution...

RE: Javascript inside a datgrid

>>wasn't completly thinking in JS mode


there IS an approach in JS

u could use the forms collection likeso:

   alert("ListBox / DropdownList!")

Known is handfull, Unknown is worldfull

RE: Javascript inside a datgrid

I have a simular issue. I have two checkboxes in my data grid.  When the user presses one, I want the other to automatically receive a check.

Sub CheckBoxSelector(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
        'First, make sure we are dealing with an Item or AlternatingItem
        If e.Item.ItemType = ListItemType.Item Or _
              e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim chkD As CheckBox = New CheckBox
            chkD = CType(e.Item.FindControl("DamageCheck"), CheckBox)
            chkD.Attributes.Add("onchange", "CheckParent();")
    End Sub

CheckParent is going to be my client script, but I'm not sure what to put in there.

RE: Javascript inside a datgrid

I am now passing the actual control name but what do I place in the javascript section:

 Dim JScriptValue As String = ("CheckParent('" & chkD.UniqueID & "');")
            chkD.Attributes.Add("onclick", JScriptValue)

javascript function
        <script language="javascript">
        function CheckParent(vlu)

RE: Javascript inside a datgrid

I think i solved it. The following code actually works:

javascript client code:
    function CheckParent(vlu)
vb asp.net code:
 Dim JScriptValue1 As String
            'For the damage checkbox, also check the return checkbox with
            'client javascript
            JScriptValue1 = ("CheckParent('" & chkD.UniqueID & "');")
            x = InStr(JScriptValue1, "Damage")
            Mid$(JScriptValue1, x, 6) = "Return"
            chkD.Attributes.Add("onclick", JScriptValue1)

RE: Javascript inside a datgrid

i would suggest that u use DOM.

how are these checkboxes placed???

Known is handfull, Unknown is worldfull

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close