check bound column in client side javascript

G

Ganesh

Hi There,

I've a datagrid in my application, template column contains a check box
plus more columns from my table. when user clicks checkbox i call javascript
to make sure at any time one checkbox is clicked. it works fine.

Now i'd like to check another bound column datetime, when user click
checkbox i want to check whether the date is in current month and year, if
not just a alert to user to tell you cannot select this because not in
current month and date.

How can i do this, your help much appreciated

Thanks
 
M

Mark Rae [MVP]

I've a datagrid in my application, template column contains a check box
plus more columns from my table. when user clicks checkbox i call
javascript
to make sure at any time one checkbox is clicked. it works fine.

Presumably, you are fetching the collection of checkbox input controls using
something like document.getElementsByTagName...? This is fairly simple
because you don't need to know anything about each checkbox except whether
it's checked or not...
Now i'd like to check another bound column datetime, when user click
checkbox i want to check whether the date is in current month and year, if
not just a alert to user to tell you cannot select this because not in
current month and date.

This, however, is a little trickier because you now need to evaluate the
contents of a "specific" control. Added to that, it could be any control
which has a corresponding checkbox.

So, what follows is a technique for doing this - I'm assuming since you're
already doing some client-side JavaScript programming you can figure out the
specifics...

Firstly, you need to create a JavaScript function which will accept a DOM
object as an argument - this will be the textbox whose value property
contains the date you need to evaluate. The function will create a Date
object as the current date and then create another Date object from the
value property of the textbox argument. You will compare the two date
objects as required and return true or false as necessary:
http://www.google.co.uk/search?sour..._en-GBGB220GB220&q=JavaScript+date+validation

Call this something like:

function checkDate(DateTextBox)
{

}

Secondly, you need to tell your checkboxes to call this client-side
JavaScript function in response to their click event:
http://www.google.co.uk/search?hl=en&rlz=1T4GGIH_en-GBGB220GB220&q=JavaScript+checkbox+click&meta=

To do this, you need to use the DataGrid's RowDataBound event:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

For each row being bound to the DataGrid, you will need to find firstly the
TextBox which will contain the date and then the CheckBox to which you will
use the Attributes.Add method to tell it to call the JavaScript function
when it's clicked. E.g. if the CheckBox is in the first cell in each row and
the TextBox is in the second cell in each row:

e.Row.Cells[0].FindControl("MyCheckBox").Attributes.Add("onclick", "return
checkDate(e.Row.Cells[1].FindControl("DateTextBox").ClientID);");

N.B. their may be some syntactical errors in the above as I've just written
it from memory, but that's certainly how I'd do it...

HTH,
 
G

Ganesh

I've a datagrid in my application, template column contains a check box
plus more columns from my table. when user clicks checkbox i call
javascript
to make sure at any time one checkbox is clicked. it works fine.

Presumably, you are fetching the collection of checkbox input controls using
something like document.getElementsByTagName...? This is fairly simple
because you don't need to know anything about each checkbox except whether
it's checked or not...
Now i'd like to check another bound column datetime, when user click
checkbox i want to check whether the date is in current month and year, if
not just a alert to user to tell you cannot select this because not in
current month and date.

This, however, is a little trickier because you now need to evaluate the
contents of a "specific" control. Added to that, it could be any control
which has a corresponding checkbox.

So, what follows is a technique for doing this - I'm assuming since you're
already doing some client-side JavaScript programming you can figure out the
specifics...

Firstly, you need to create a JavaScript function which will accept a DOM
object as an argument - this will be the textbox whose value property
contains the date you need to evaluate. The function will create a Date
object as the current date and then create another Date object from the
value property of the textbox argument. You will compare the two date
objects as required and return true or false as necessary:http://www.google.co.uk/search?sourceid=navclient&aq=t&hl=en-GB&ie=UT...

Call this something like:

function checkDate(DateTextBox)
{

}

Secondly, you need to tell your checkboxes to call this client-side
JavaScript function in response to their click event:http://www.google.co.uk/search?hl=en&rlz=1T4GGIH_en-GBGB220GB220&q=Ja...

To do this, you need to use the DataGrid's RowDataBound event:http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gr...

For each row being bound to the DataGrid, you will need to find firstly the
TextBox which will contain the date and then the CheckBox to which you will
use the Attributes.Add method to tell it to call the JavaScript function
when it's clicked. E.g. if the CheckBox is in the first cell in each row and
the TextBox is in the second cell in each row:

e.Row.Cells[0].FindControl("MyCheckBox").Attributes.Add("onclick", "return
checkDate(e.Row.Cells[1].FindControl("DateTextBox").ClientID);");

N.B. their may be some syntactical errors in the above as I've just written
it from memory, but that's certainly how I'd do it...

HTH,



Hi Mark,

Thanks a lot, that was really useful. I'll try this now.
 
M

Mark Rae [MVP]

Thanks a lot, that was really useful. I'll try this now.

No worries.

Incidentally, is there any reason that you're using DataGrid and not
GridView? Are you still using VS.NET 2003...?
 
G

Ganesh

Yes we do use vs.net 2003 . Company had already started developement before
2005 release. still they want to use the same one. I don't see they have any
plan to update at the moment.

Thanks
 
M

Mark Rae [MVP]

Yes we do use vs.net 2003 . Company had already started developement
before 2005 release. still they want to use the same one. I don't see they
have any plan to update at the moment.

Hmm - OK... That's all well and good, but isn't really helping your career
very much...

2005 is already over 18 months old, 2008 isn't too far away now:
http://msdn2.microsoft.com/en-us/vstudio/aa700831.aspx

Presumably, your company intends to upgrade before mainstream support for
2003 ends:
http://support.microsoft.com/lifecycle/search/?sort=PN&alpha=Visual+Studio&Filter=FilterNO

It would be a shame to get left behind...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top