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

Setting an ASP.Net value = to a JS Function

Status
Not open for further replies.

ThatRickGuy

Programmer
Joined
Oct 12, 2001
Messages
3,841
Location
US
Hey guys, getting close to having this project wrapped up. One thing that I am not quite pleased with is the performance of my popup calendar though.

It uses Javascript to open:
Code:
function GetDate(CtrlName)    
{   
ChildWindow = window.open('Calendar.aspx?FormName=' + 	document.forms[0].name + '&CtrlName=' + 	CtrlName, "PopUpCalendar", 
	"width=250,height=270,top=200,left=200,toolbars=no,scrollbars=no,status=no,resizable=no");    
}

And to return the value to the parent:
Code:
function ReturnDate()
{
window.opener.document.forms["<%= strFormName %>"].elements	["<%= strCtrlName %>"].value = "<%= strSelectedDate %>" + " " + "<%= strSelectedTime %>";
window.close();
}

Which is all fine and good, except that the ASP.Net Calendar defaults to Todays date, even if the user has already entered a value and is modifying a record.

I can use JavaScript to get the value from the textbox on the parent form from the calendar:
Code:
function GetDate()
{
return (String(window.opener.document.forms["<%= strFormName %>"].elements["<%= strCtrlName %>"].value).substring(0,10));
}

But that gets the value on the client side, and the calendar control has to be set server side. So I need to check that value on load, if it's a valid date save it in some statefull way, then reload the page and set the calendar date.

The problem is, how can I get the value that is returned by that function into the state bag?

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Just goes to show, you beat your head against a brick wall long enough, you'll either learn patience, or break down the wall.

In this case, the wall lost.

So, here's what I did.

I changed the html links that were firing the javascript to open the new window into ImageButtons. I then set the OnClick to a sub in the CodeBehind which sets a session variable and register the java:
Code:
  Public Sub EndDate_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
    If Session.Item("DateTime") Is Nothing Then Session.Add("DateTime", Me.txtStartDate.Text)
    Session("DateTime") = Me.txtEndDate.Text

    Dim strJavaScript As String
    strJavaScript = "<script language='javascript'>" & _
                    "GetDate('txtStartDate');" & _
                    "</script>"
    Me.Page.RegisterClientScriptBlock("SetDate", strJavaScript)
  End Sub

I register the Java in the onclick even because I found that using the attributes.add method in the Page_Load caused the Java to fire before the OnClick sub ran:
Code:
Me.ibtnEndDate.Attributes.Add("onClick", "return GetDate('txtStartDate');")

Once the value was in the session variable I could set the calendar's default value with out a problem.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Looks good to me!

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top