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

Get value of control built in template column 1

Status
Not open for further replies.

mirirom

Programmer
Jul 21, 2001
110
US
hi,

i'm working with a datagrid that's almost entirely composed of template columns (the implementation of it is unique enough that it's required). one of the template classes i've created defines a DropDownList control and adds it to the footer. this all works fine and well.

however, i'm a bit stumped as to how to go about getting the value of a selected item in the control. part of the problem is due to the fact that controll cannot be named prior to compiling. this is the default behavior of controls inside datagrids. interestingly enough, the ID property of the control gets the runtime assigned name of the control *added* to it. for instance, if in code i assign myDropDownList.ID = "foo", the ID of the control is really "foo:_controlNameAssignedDuringHTMLBuild". that being said, i can't use the Item.FindControl method to extract the value of the control.

one sloppy way around this is to use the dropdownlist's events and pass a function pointer to the template ctor (i'm doing this for the button handlers in the datagrid). for example, the datagrid is defined on myPage.aspx.cs. this same file defines a function foo() which will be called when the dropdownlist's selectedIndex is changed. when creating the template column with the dropdown, i pass a function pointer (a delegate in C# speak) to my template object's ctor and use it for assignment to the dropdownlist's SelectIndexChanged event.

the event handler can store the value to some type of state variable i guess.

this'll work, however, the extra (non-validating) postback plus the state memory are more baggage than i was hoping for. i'm guessing there's gotta be a cleaner, more direct way to do this.

any thoughts on this? help is greatly appreciated. thanks!



..:: mirirom ::..
 
that being said, i can't use the Item.FindControl method to extract the value of the control."

Actually I think it depends on where you call the function from. You can't call Page.FindControl, but you should actually be able to use DataGridItem.FindControl() because there is some name resolution that goes on.

The reason the ID gets the funky suffix has to do with the control being in a naming container, which automatically tweaks ids to keep them unique on a page (to maintain state and stuff).

Anyhow, something else you can do (which is what I always do) is something like this:

//enum makes for easy, maintainable indexing
private enum ControlIndices{ SomeControlIndex, MyDropDownListIndex };

//later in code
DropDownList myDropDown =
(DropDownList)Item.Cells[cellIndex].Controls[(int)ControlIndices.MyDropDownListIndex];

[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
hi Boulder, thanks for the reply.

i guess i'm confused about this then. if the controls are named during compiation, then you have no way of knowing what the string id is for the control. since i don't know the name of the dropdownlist, then how can i use it in the DataGridItem.FindControl method -- which expects a string representing the ID of the control.

i've tried assigning the control ID when the dropdownlist is created in the template column, however, additional text is always appended to what i assigned.

your advice got my gears grinding though. went back and disected the stuff i did for the DataBinding events in the template classes & produced this...
Code:
/* handler for the button in another template column.
   dropdownlist is ALWAYS in cells[1] of the footer and it's
   the only control in there */

string sMyValue;
public void cmdAddToMyGrid_Click(object sender, System.EventArgs e){
   Button b = (Button)sender;
   DataGridItem container = (DataGridItem)b.NamingContainer;
   DropDownList d 
      = (DropDownList)container.cells[1].Controls[0];
   sMyValue = d.SelectedItem.Value;
}

works.

thanks again for your help!






..:: mirirom ::..
 
Happy to be of assistance.

"if the controls are named during compiation, then you have no way of knowing what the string id is for the control."

Luckily, the ID rendered to the browser is actually accessible through the ClientID property of a control.

As far as the FindControl() thing goes, if you use it in the container housing the control (like a DataGridItem) then it takes off all the extra crap that INamingContainer and the Framework put on before searching. In other words, instead of looking for "blah:blah:controlID_54" (or whatever), it will just look for "controlID".

Here's an awesome concise demonstration of the concept:

[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top