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

Dynamic Datagrid Field

Status
Not open for further replies.

vituja123

Programmer
Jun 16, 2005
110
US
Hi All,

I am able to create dynamic fields inside a data grid (i needed a dropdownbox instead of text when a piece of data has a specific value).

But how can I read the value of a dynamic field???

I created it use this code which works fine:
Dim DLIST As New DropDownList

DLIST.Items.Add("0")
DLIST.Items.Add("25")
DLIST.Items.Add("50")
DLIST.Items.Add("75")
e.Item.Cells(1).Controls.Add(DLIST)
------------------------------------------

When I want to read the value on postback I tried this code but I keep geting a Casting error (cast not valid):

For Each dataGridItem As DataGridItem In DataGrid1.Items
Dim MinValue As String = CType(dataGridItem.Cells(1).Controls(1), DropDownList).SelectedIndex

Next
 
You need to get a referenc to the control by using FindControl.

dim ddl as dropdownlist

ddl = CType(e.Item.FindControl("DropdownlistName"), DropDownList)

minvalue = ddl.selectedindex

You need to name the control (ID) before you add it so that you can refernce it.

Jim
 
The FindControl method doesn't actually use the ID that is generated for the client, it uses the one that you assign it. e.g. you may give it an ID of "DDL1" and you can use FindControl to locate it, but if you look at the source of the page, it may have an ID of "DDL1_ctl0".


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I named the control e.Item.Cells(1).ID = "DList1"

The viewsource says its "DataGrid1:_ctl5:_ctl0".

Either way, If I use DList1 or DataGrid1:_ctl5:_ctl0 in the findcontrol, the return value is Nothing (Referenced object 'ddl' has a value of 'Nothing'.)

So can a dynamically created control be read after postback or is its value always nothing?
 
Dim DLIST As New DropDownList

DLIST.Items.Add("0")
DLIST.Items.Add("25")
DLIST.Items.Add("50")
DLIST.Items.Add("75")
DLIST.ID = "Some Name"
e.Item.Cells(1).Controls.Add(DLIST)
 
Ah, that's where it should go.

But when I do: ddl = CType(e.Item.FindControl("DropdownlistName"), DropDownList)

after postbox, the value of ddl.selectedItem is nothing like the control does not exist.
 
Yes. The control is added sucessfully and when I do a viewsource, it has a name of "DataGrid1:_ctl5:DList1"


Then the user presses a save button which of course does a postback.

In the save button I loop through all the values in the datagrid. But when I come upon this code:


dim ddl as dropdownlist
Dim MinValue as string

ddl = CType(dataGridItem.FindControl("DList1"), DropDownList)
MinValue = ddl.SelectedIndex

The ddl.SelectedIndex is Nothing. In fact everything about that control is set to Nothing.
 
Its not finding the control.
ddl = CType(dataGridItem.FindControl("JUNK"), DropDownList)

I set the findcontrol name to JUNK and I also get Nothing values.

It seems like even if the dynanmic control is rendered correctly, the postback loses the control.

 
What is the name of the control you specify in this line?
DLIST.ID = "Some Name"

And what event are you adding the control?

 
It's "DList1"

Dim DLIST As New DropDownList
LIST.ID = "DList1"
DLIST.Items.Add("0")
DLIST.Items.Add("25")
DLIST.Items.Add("50")
DLIST.Items.Add("75")
e.Item.Cells(1).Controls.Add(DLIST)

I've been reading some info that suggests dynamic fields created in a datagrid lose their viewstate when the page posts back. I hope that's not the case.
 
Not really. The dynamic control does not render, instead i get:
Control 'DList1_0' of type 'DropDownList' must be placed inside a form tag with runat=server.


There must be a better way to do this. Perhaps if I render the control as I did before but save is value in a label field using javascript???



 
FYI:

For those who might find this useful.

I was able to create the dynamic dropdownlist, then use client side javascript to populate a hidden text field on the page.

Every time the user selected a value from the dropdown list , the onchange javascript event would fire allowing me to take that value and send it to the hidden text field.

After that, if the user posted back the page, i was able to read the hiddent field value since it was alreay istanciated.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top