Datagrid Question

G

Guest

I have an editable data grid that contains two drop down lists. one list is
dependent on the value selected in the other list. Very simply the 1st ddl
has 3 values, if value 2 is selected, the second ddl is enabled otherwise the
second ddl should be disabled.

What is the best way to do this? Normally I'd probably use javascript or
incur a postback but since it's in a datagrid, I'm not sure what will work.

Thanks
 
G

Guest

Mardy said:
I have an editable data grid that contains two drop down lists. one list is
dependent on the value selected in the other list. Very simply the 1st ddl
has 3 values, if value 2 is selected, the second ddl is enabled otherwise the
second ddl should be disabled.

What is the best way to do this? Normally I'd probably use javascript or
incur a postback but since it's in a datagrid, I'm not sure what will work.

Thanks

From a presentation perspective; I think both approaches will work. Though I
would change the two-values-only dropdown list to a checkbox and use
client-side JavaScript to hide or display the second dropdown list.

From a business logic perspective; are these 2 objects bound to 2 different
fields or do they translate to a value in one field?
 
J

jasonkester

I agree. This is what Javascript is for. There's no reason to bother
with a postback in this situation.

One caution, be careful to ensure you get the correct ClientIDs for all
your elements. ASP.NET does some ID mangling of controls living inside
of DataGrids, so you need to make sure that you store the ClientID for
your dropdown & checkbox someplace if they are marked runat=server.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
G

Guest

I'd like to avoid the postback so i placed a div around the ddl in the
datagrid, added the javascript to the page and added the
ddlResult.attributes.add.... code to the page_load sub.

I tested this absent the datagrid and it works fine but inside the datagrid
I get an error "Object reference not set to an instance of an object"
referring to the page_load sub where I have this line of code:
ddlResult.Attributes.Add("onchange", "return resultTest()")

That's probably related to what you described Jason but I don't know how to
get around this error or why the ddl in the datagrid is not visible to the
codebehind.

any ideas are greatly appreciated

Mardy
 
T

The Crow

//Add this line in your InitializeComponents method.

grid.ItemDataBound +=new DataGridItemEventHandler(grid_ItemDataBound);

private const int dropDownContainerColumnIndex = 1; //Change it to what
suits for your situation.

private void grid_ItemDataBound(object sender, DataGridItemEventArgs e)

{

if(e.Item.ItemType == ListItemType.EditItem)

{

DropDownList drp =
(DropDownList)e.Item.Cells[dropDownContainerColumnIndex].FindControl("id of
ddl in edititemtemplate");

drp.Attributes.Add.............

}

}
 
G

Guest

ok I think I see where I need to go. I'm using vb so I'm trying to convert
the code you sent based on limited knowledge.

Now that I have the additional code, I no longer get the null reference but
the display doesn't toggle. What about the div tags that I'm using in the
datagrid to hide the ddl? Are they not visible to the javascript since thay
are in the datagrid?
 
T

The Crow

nope. u should access that div by
var mydiv = document.getElementById('id_of_div');
 
T

The Crow

or u can access dropdown and hide it instead of div. if u want to access
dropdown, u should use ClientID property of dropdown. just build javascript
code in your code behind. as you build, you will write code such
script = "function hidedropdown() { var drp = document.getElementById('" +
drp.ClientID + "');.......................";

and you can include javascript code by
Page.RegisterClientScriptBlock("HideDropDown", script");
 
G

Guest

Hey Crow thanks for all the help. I believe that I'm close but still not
there.

Looks like the the onchange is working and firing the javascript correctly
but I get a javasript error that the ddl object is null. I've tried
everything from within the script block but can't seem to find the magic
words to get the javascript to see the grid controls. As much as I hate to
incur the brain damage looks lik I'll have to try writing script in the code
behind. Did I mention that I hate javascript?

Seriously thanks for all your help.
 
J

jasonkester

View source on the page. Does the ID on the select box match the ID in
your document.getElementById() call?

You'll probably get a lot further if you ignore most of what the
grandparent has been telling you. Javascript is best written on the
client side. ASP.NET gives you the ability to write it as a string
from the server, but it usually ends up biting you if you rely on it.

Open up a <script> block. Do a BUNCH of sanity check statements to
ensure that you have a reference to what you think you do:

<script>
alert(document);
alert(document.getElementById);
alert(document.getElementById("yourClientID"));
alert(document.getElementById("yourClientID").selectedIndex);
// etc...
</script>

If this blows up at any point, you'll know where to start looking. If
you still have trouble after doing this, save the source into an HTML
file and strip it down until it's just enough lines to demonstrate the
problem. If it's still not apparent what the problem is, post it back
here and we'll take a look at it.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top