Link to home
Start Free TrialLog in
Avatar of Howard Bash
Howard BashFlag for United States of America

asked on

DataGrid cell change detection

I am working with a web form that contains the DataGrid component.  I added a calendar form that I launch to allow the user to specify the date value for a column on the grid.  Well after some amount of noodling around,  I am able to launch the calendar and have it set the value of a hidden field on the calling form to the selected date.  How can I get the returned value loaded into the cell in question?  I have an update command on the row and can grab the value and add it to the grid then, but that is really too late.  Any thoughts about this would be appreciated.

Avatar of CS_Lewis
CS_Lewis

Here's a VB example of what I think you're trying to do.  When the user clicks on the box you want to edit, the javascript will open up the calendar page and pass the form name and the id of the text box.

'This code goes into the grid's itemdatabound event
Private Sub dgDAPS_ItemDataBound

     Dim txtEdit As TextBox = CType(e.Item.FindControl("txtData"), TextBox)

     txtEdit.Attributes.Add("onclick", "javascript:calendar_window=window.open('Calendar.aspx?formname=myForm." & txtEdit.ClientID & "','DatePicker','width=303,height=190,left=360,top=180'); calendar_window.focus();")

End Sub


'This code will go in your calendar popup page. It will return the date value back to the text box that you clicked on in the datagrid.

Protected WithEvents Literal1 As System.Web.UI.WebControls.Literal          'Class Variable

Public Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim script As String
              script = "<script language='javascript'> " & "window.opener." & _
              HttpContext.Current.Request.QueryString("formname") & ".value='" & _
              Format(Calendar1.SelectedDate, "MM/dd/yy").ToString & "'; window.close(); </script>"
        Literal1.Text = script
End Sub
Avatar of Howard Bash

ASKER

The code looks like it may get me there.  Thanks.  One question about it though.  What is this Literal1 used for?

Thanks again this does look promising.
A literal control basically just returns static text to the page.  When the page does its postback and opens up again, it will run this Javascript (which returns the date value and closes the popup).  You could probably do RegisterStartupScript and accomplish the same thing.

This seems odd.  How does the javascript code get executed and why?
ASKER CERTIFIED SOLUTION
Avatar of CS_Lewis
CS_Lewis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial