Programmatically load dropdownlist into datagrid

J

Julia Hu

Hi,
I have a datagrid, and in different rows I need to
programmatically bind different type of controls and load
data into these controls. For example,in the first row I
need to bind data into a textbox, and in the second row I
need to bind data into a dropdownlist...It all depends on
the data I select from the database.

I cannot use TemplateColumn because it has to be the same
type of control for one column.

Thanks a lot,

Julia
 
K

Karl

Julia,
Try something like this:

<asp:TemplateColumn HeaderText="Hotel Type">
<ItemTemplate>
<asp:placeHolder ID="plc" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>

I'm not sure if you are using c# or vb.net, and what you are binding too,
but assuming you are in vb.net and binding to a dataset/table, your
ItemDatabound event should look something like:

Private Sub Grid_ItemDataBound(ByVal sender As Object, ByVal e As
DataGridItemEventArgs) Handles grid.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim plc As PlaceHolder = CType(e.Item.FindControl("plc"), PlaceHolder)
If Not plc Is Nothing Then
Dim dr As DataRowView = CType(e.Item.DataItem, DataRowView)
Select Case CInt(dr("type"))
Case 1
Dim txt As New TextBox
txt.Text = CStr(dr("value"))
plc.Controls.Add(txt)
Case 2
Dim ddl As New DropDownList
plc.Controls.Add(ddl)
End Select
End If
End If
End Sub

As you can see, we try to find the placeholder contorl, if it exists, we get
a reference to the current row we are using, we can use the values in the
row (here, I used a mythical "Type" column) to figure out what type of
control goes into the placeholder.

Karl
 
J

Julia Hu

Karl,

Thank you for your quick response and appreciate your help!
I will try it and let you know if it works.

Julia
 
J

Julia Hu

Karl,
The code works. And now I need to retrieve the data in
the dropdownlist which embeded in the placeholder
control. The following is my code does this, however it
returns nothing.

plcAI = CType(grid.Items(i).Cells(1).FindControl
("plcAI"), PlaceHolder)
Dim ddl As DropDownList = CType(plcAI.FindControl
("ddlAI"), DropDownList)

thanks again
 
K

Karl

What's null, plcAI or ddl?

Karl

Julia Hu said:
Karl,
The code works. And now I need to retrieve the data in
the dropdownlist which embeded in the placeholder
control. The following is my code does this, however it
returns nothing.

plcAI = CType(grid.Items(i).Cells(1).FindControl
("plcAI"), PlaceHolder)
Dim ddl As DropDownList = CType(plcAI.FindControl
("ddlAI"), DropDownList)

thanks again
 
J

Julia Hu

As you told me, I loaded controls in ItemDataBound event,
and how can I re-add them?
code like this:
Private Sub grdAI_ItemDataBound(ByVal sender As Object,
ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
grdAI.ItemDataBound
Dim i As Integer
Dim plcAI As PlaceHolder = CType
(e.Item.FindControl("plcAI"), PlaceHolder)

If Not plcAI Is Nothing Then
Dim dr As DataRowView = CType
(e.Item.DataItem, DataRowView)
Select Case CInt(dr("FieldType"))
Case 6 'ListView type
Dim ddlAI As New DropDownList
ddlAI.ID = "ddlAI"

ddlAI.Items.Add("Big")
ddlAI.Items.Add("Small")

plcAI.Controls.Add(ddlAI)


Case Else
Dim txt As New TextBox
txt.Text = ""
plcAI.Controls.Add(txt)
End Select
End If

End Sub
 
J

Julia Hu

If I reload controls again in postback, the selected item
of the dropdownlist will be lost. How do I keep it?
 
K

Karl

You have two solutions, neither is particularly nice.

One of those solution is to rebuild the controls. The other one is to
access their value directly from Request.Form() Your problem with the
request.Form is figuring out what the name of the damn thing will be (it
won't be random - far from it actually). It'll be something like grid.id +
":" + row.id +... well not nearly that, but something along the lines of
parentId appended to childId.

The other solution is, as I mentioned, to rebuild the controls. This
solution has its own ugliness...if the above method will work for you, I
suggest you try it out. Otherwise, you'll need to store values in the
ViewState that says which controls were loaded, then on page_load, read the
viewstate, reload everything, and you'll be able to access them. You can
clean things up a lot by refactoring the creationg code.

For example, instead of adding the ddl when case 6, simply call a method,
which your page_load can also call.
case 6 'listvie type
AddListView("ddlAI")

Anyways, try the first solution (or maybe something else)...if ti doesn't
work and you need more help with the 2nd one, let me know.

Karl
 
K

Karl

If it gets re-added in the page_load, it'll happen before the Begin
ProcessPostData Second Try which will preserve the value...If you do it in
the button click, you'll lose it.

Karl
 
J

Julia Hu

I tried both ClientID and UniqueID and the UniqueID
works!! I got the SelectedValue, and how can I get the
SelectedText??
 
J

Julia Hu

Really appreciate your assistance!
I tried both ClientID and UniqueID and the UniqueID
works!! I got the SelectedValue, and how can I get the
SelectedText??
 
K

Karl

You can't directly....this gets uglier, doesn't it? If you rebuild you can,
if you don't you just have access to the value...although from the value you
can obvious get the text either by hitting your database or cache..

Karl
 
J

Julia Hu

Yes,just one more database call.
I still have 2 questions:
First,the UniqueID will never change if I keep the same
columns structure in the datagrid?
Second,if I use your 2nd solution to reload data,can I
put in Page_PreRender instead of Page_Load? And can I
still use the FindControl method to find "plc" and "ddl"
like this
plcAI = CType(grid.Items(i).Cells(1).FindControl
("plcAI"), PlaceHolder)
Dim ddl As DropDownList = CType(plcAI.FindControl
("ddlAI"), DropDownList) ??

thank you very much

-----Original Message-----
You can't directly....this gets uglier, doesn't it? If you rebuild you can,
if you don't you just have access to the
value...although from the value you
 
K

Karl

it isn't an extra call if you cache the data...which is obviously something
you should consider at this point.

I believe that the id's won't change if the structure is the same, and you
specify the same ID for the ones you have control of.

Page_PreRender I believe so, but remember that your placeholder won't exist
until AFTER you bind the data to the grid. So before you do anything in
page_load you need your grid to be bound to it's source. Unless you
absolutely need the UI re-constructed after you post, I'd really recommend
you try and get away with the Request.Form. You might also have luck
searching for similar solution *dynamically added controls* or something.

Finally, if you post again to this newsgroup in a new thread, I won't reply,
so you might get the input of someone else and find a better answer (I say I
won't reply 'cuz if the other helpers are like me, they rarely reply if they
see that a post has a reply). Of course we can keep this thread going :)

Karl
 
J

Julia Hu

Thank you very much for your help!
I probably will have questions about this PlaceHolder
control in the near future, and I'd like to keep this
thread going and hope you can give me some advice.
I guess this is the only way to contact you directly 'cuz
I don't have your email address.

Regards,
Julia
 
K

Karl

if you check the details from this thread my email address is there (with a
bunch of REMOVEs) 'cuz I'm at home..but it's better to do is on here, might
help other people :)

karl
 
J

Julia Hu

As you told me,the UniqueID is composed of grid ID,row ID
and ddl ID,but why the row ID is always the actual row +
1? The grid doesn't have a header. For example,the ddl in
the first row of my grid is:
grdAiEditGrid:grdAI:_ctl2:ddlAI

thanks
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top