Client js for a datagrid drop down

VBAHole22

Contributor
Joined
Oct 21, 2003
Messages
432
Location
VA
I have an editable data grid. When the user clicks the edit button I change a textbox to a dropdown so they can select a new value. I also add a js attribute to the save button so they get prompted for confirmation before commiting the save.

This works well for me as I can inform the user of the state they are changing the value FROM before they commit. What I can't figure out is how I can get a handle on what they are changing the value TO before they commit. This is because my js gets bound with the grid, before the user chooses. I could put an auto post back on the ddl but that would be a little cumbersome I think for the user.
If I could just get a handle on the ddl in js I could get the selected value. Trouble is the ddl doesn't maintain the id I give it in html. It gets appended with the .NET ctl_ syntax so I can't use js

document.getElementById("ddl");

Is there another way to do this?
 
how about you have your javascript function called with the 'this' as an arugument:

Visual Basic:
'In ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
    Dim dgi as DataGridItem = DirectCast(e.Item, DataGridItem)
    Dim cntl as Control = e.Item.Cells(0).FindControl("DropDownList1")

    If Not cntl Is Nothing Then
        DirectCast(cntl, DropDownList).Attributes.Add("onmousedown", "MyJavaScriptSetCurrentValue(this)")
        DirectCast(cntl, DropDownList).Attributes.Add("onchange", "MyJavaScriptConfirmFunction(this)")
    End If
End If

Now you javascript would be something like:
Code:
var curValue;

function MyJavaScriptSetCurrentValue(cntl)
{
      curValue = cntl.value;
}
function MyJavaScriptConfirmFunction(cntl, currentValue)
{
      if(confirm('Are you sure?'))
         return;
      else
         cntl.value = curValue;
}

***Note this may not be the best method but it will get the job done...you might want to take the time to do something a little more robust like setting a hidden input tag to the current value that has a similiar name, or something like that.

Hope this helps.
 
Back
Top