Solutions Guy - a simple way to get your business going
Saturday, January 10, 2009
Having little experience with javascript, I came across the issue of being able to access the value of a field that was not an asp.net textbox. With javascript being executed client side, the server really has no knowledge of the value. This can cause a problem when trying to integrate javascript with your website. In my case, I had a javascript pop-up calendar that would allow a user to select a date, once again all client side code. In order to get the value of the selected date in the c# code-behind, I had to do the following:

1. Use the below line of code to create a hidden field on the webpage; I called mine hdnElement. Also, create an input box for the Javascript, mine was called txbxOccDate.

input type="hidden" runat="server" id="hdnElement"

input id="txbxOccDate" name="txbxOccDate" onfocus="putMethodHere(this);"
type="text"

2. Now you have a field that is hidden on the page, but still has no value, so the next step is to set the hidden field value to equal the selected value of the calendar. I used this javascript:

document.getElementById('hdnElement').value =
document.getElementById('txbxOccDate').value;


3. Once you set the value of your javascript box to the hidden value, it's time to move to the code behind. I had a button that was pushed by the user and had my code in the button_click event, but this code needed to be moved to the page_load event. This occurs because the values are captured and posted back to the page, so the page will load again and all of the input values are available.

4. First, I needed to check to make sure the calendar value was captured, so this line of code should do the trick;

protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(hdnElement.Value))
{
}
}


5. Last, don't forget to clear the values of the fields once you're finished. And that's it!

Labels: , , ,

 

0 Comments:

Post a Comment

<< Home

Subscribe to
Posts [Atom]

© 2008 Solutions Guy, All Rights Reserved  |  801.228.0312  |   Email Us