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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript inside a datgrid 1

Status
Not open for further replies.

MrPink1138

Programmer
Jul 10, 2003
34
US
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?
 
you can add the javascript function to the dropdownlist on the itemdatabound event of the datagrid bind....

Code:
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..."
 
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....
 
>>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
 
Very true vbkris.. wasn't completly thinking in JS mode. Like your solution...
 
>>wasn't completly thinking in JS mode

NEITHER WAS I!!! ;)

there IS an approach in JS

u could use the forms collection likeso:


TheElem=document.forms[0].elements
for(i=0;i<TheElem.length;i++)
{
if(typeof(TheElem)=="select")
{
alert("ListBox / DropdownList!")
}
}

Known is handfull, Unknown is worldfull
 
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.
 
I am now passing the actual control name but what do I place in the javascript section:


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

javascript function
<script language="javascript">
function CheckParent(vlu)
{
alert(vlu);
}
</script>
 
I think i solved it. The following code actually works:

javascript client code:
function CheckParent(vlu)
{
document.all(vlu).checked=true;
}
--------------------------------------
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)
 
i would suggest that u use DOM.

how are these checkboxes placed???

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top