473,320 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Disab;e selection of dropdownlist items (in a GridView)

Is there an example of using client side script to disable selection of some
items in a drop down that I could follow.

It would seem tricky as there are multiple drop downs in the GridView. I
would need to remember the last selected value for each drop down within the
grid. If the selected value is not permitted then reset to the remembered
value.

And then when a valid item is selected the remebered value for that drop
down would need to be updated.

Are there any links or examples that I could utilise?

Mar 29 '06 #1
4 1596
If you want to use that level of trickery, then you don't really want to be
using things liske the DataGrid control. You should be using HTML controlls
and doing all the ADO.net data stuff manually. That way you will have more
control over the way that your HTML renders and you can write more apropriate
javascript.

What exactly are you trying to acheive maybe there is a better way
altogether, for example using the validation controls.

"Nick Zdunic" wrote:
Is there an example of using client side script to disable selection of some
items in a drop down that I could follow.

It would seem tricky as there are multiple drop downs in the GridView. I
would need to remember the last selected value for each drop down within the
grid. If the selected value is not permitted then reset to the remembered
value.

And then when a valid item is selected the remebered value for that drop
down would need to be updated.

Are there any links or examples that I could utilise?

Mar 29 '06 #2
Off the top of my head....

<code>
var curValues =[];
function init()
{
var theGridView = document.getElementById("GridViewClientSideId");
arrDropDowns = theGridView.getElementsByTagName("select");
for( var i=0; i< arrDropDowns.length; i++)
{
var dropdown = arrDropDowns[i];
curValues[dropdown.ID] = dropdown.selectedIndex;
dropdown.onchange = checkPermittedValue;
}
}

function checkPermittedValue()
{
var val = this.options[this.selectedIndex].value;
if( isPermittedValue(val) )
{
curValues[this.Id] = this.selectedIndex;
return true;
}
this.selectedIndex = curValues[this.Id];
return false;
}
</code>

Run init from the document.onload function and it'll get the current
selected indexes into the current values hashtable and assign an event
handler to the onchange event of each drop down. If your
isPermittedValue function returns true, the remembered value is
updated, otherwise the dropdown reverts to its original selected index.

<disclaimer text="Bob is on his first cup of coffee, code not
guaranteed to work; for entertainment purposes only" />

The copy/paste HTML below seems to work, at least... don't know what
this will do to autopostback, so beware.

<code>
<html>
<head>
<script>
function init()
{
document.getElementById("fred").onchange = CheckPermittedValue;
}

function CheckPermittedValue()
{
if(this.selectedIndex == 0)
return true;
else
this.selectedIndex = 0;
return false
}
</script>
</head>
<body onload="init()">
<select id="fred">
<option>barry</option>
<option>harry</option>
</select>
</body>
</html>
</code>

Mar 29 '06 #3
This looks like a good sample - thanks. I have some questions

The grid view is converted to a table and in my example has the id -
ctl00_maincontent_ProjectListGridView

The number of drop downs varies dependent on the number of rows in the
table. Example of Ids include:

ctl00_maincontent_ProjectListGridView_ctl02_WorkPa ckage,
ctl00_maincontent_ProjectListGridView_ctl02_WorkPa ckage

How can this code refer to this table? On Page Load will I be able to get
this ID?

From this I the code does seem to be able to get the IDs some the rest looks
Ok.


"Flinky Wisty Pomm" wrote:
Off the top of my head....

<code>
var curValues =[];
function init()
{
var theGridView = document.getElementById("GridViewClientSideId");
arrDropDowns = theGridView.getElementsByTagName("select");
for( var i=0; i< arrDropDowns.length; i++)
{
var dropdown = arrDropDowns[i];
curValues[dropdown.ID] = dropdown.selectedIndex;
dropdown.onchange = checkPermittedValue;
}
}

function checkPermittedValue()
{
var val = this.options[this.selectedIndex].value;
if( isPermittedValue(val) )
{
curValues[this.Id] = this.selectedIndex;
return true;
}
this.selectedIndex = curValues[this.Id];
return false;
}
</code>

Run init from the document.onload function and it'll get the current
selected indexes into the current values hashtable and assign an event
handler to the onchange event of each drop down. If your
isPermittedValue function returns true, the remembered value is
updated, otherwise the dropdown reverts to its original selected index.

<disclaimer text="Bob is on his first cup of coffee, code not
guaranteed to work; for entertainment purposes only" />

The copy/paste HTML below seems to work, at least... don't know what
this will do to autopostback, so beware.

<code>
<html>
<head>
<script>
function init()
{
document.getElementById("fred").onchange = CheckPermittedValue;
}

function CheckPermittedValue()
{
if(this.selectedIndex == 0)
return true;
else
this.selectedIndex = 0;
return false
}
</script>
</head>
<body onload="init()">
<select id="fred">
<option>barry</option>
<option>harry</option>
</select>
</body>
</html>
</code>

Mar 29 '06 #4
You can write the clientId of the GridView to the page during the page
load. As an old skool zealot, I keep all my JScript in a separate file,
so I use a helper function that looks like this:

Note, this will only work in browsers which support the HTML DOM - IE
5+, FireFox or Opera will all work happily. If you want this to work in
older browsers, you're on your own.

Also note, this is a fairly MacGuyver-esque kludge and it's probably
not hard to break it with unusual situations, but it's worked so far :)
I keep meaning to write a handler so I can generate JS files on the
fly, but deadlines have prevented me so far.
<code>
var __elementRefs__ = [];
function getSingleElement(id, tagName, refresh)
{
if(undefined === __elementRefs__[id] || refresh)
{
var re = eval("/"+id+"$/");
var a =
null==tagName?document.body.childNodes:document.ge tElementsByTagName(tagName);
var i = a.length;
while(i-->0)
{
if(re.test(a[i].id))
{
__elementRefs__[id] = a[i];
return a[i];
}
}
return __elementRefs__[id] = null;
}
else
return __elementRefs__[id];
}
</code>

Pass it an id and a tagname and it will return the first match in your
page. It caches previously selected elements to avoid repeating the
whole eval/regex match. The tagname is optional, but speeds the process
up.

So in your case, do:
var gridView = getSingleElement("ProjectListGridView", "table");

Mar 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joe Delphi | last post by:
Hi, I am trying to populate dropdownlist items from the results of a database query. My code looks like this: Private Sub PopulateSubByBox(ByVal DBConn As SqlConnection) Dim DBComm As New...
0
by: satish | last post by:
Hi , I am automating internet. I am using mshtml and shdocvw.ddl for automations Can anyone tell me how to automate selection of items from list the e.g of html source is as given <select...
1
by: mitchman10 | last post by:
My Time table has TimeID,Employee,PayPeriod,ChargeCodeID,Hours My Chargecode table has ChargecodeID,c_Text I need an Editable datagrid that will show the TimeID,Employee,PayPeriod,C_Text in a...
0
by: sharonrao123 | last post by:
hello all, I have a parent gridview company and in this one a nested gridview people, Is it possible to allow the user to select one row or multiple rows from the people gridview using a check box...
0
by: baburk | last post by:
I am having dropdownlist inside gridview. When the dropdownlist selectedindex change an event should fire. What is the event for his. I also want to get the dropdownlist event fired row...
0
by: Moneypenny | last post by:
Hi there, I have a Gridview control which has a dropdownlist in every row. The dropdowns are populated dynamically from a database (so the content is different in each row. There is also a select...
0
by: minhtran | last post by:
Hi everyone I have a problem when I want to edit the value from nested dropdownlist in Gridview, Please, anyone can help me to solve this problem, a great appreciation from me in advance. here is...
2
by: RobertTheProgrammer | last post by:
Hi again, Okay, I decided the GridView is the way to go to solve my problem. But now I've got another problem. I've found plenty of examples about how to add a DropDownList item to the grid...
1
by: shahidrasul | last post by:
i want to bind a dropdownlist in gridview every row has a dropdownlist and then have multiple or single values. problem is that i want when user change value from dropdownlist, we want to show...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.