Insert in DropDownList within Repeater

J

jjack100

I have a DropDownList that is nested inside a Repeater. The datasource
of the DropDownList is declared in the aspx, not the codebehind. So we
have this:

<asp:Repeater ID="rptOptions" runat="server">
<ItemTemplate>
<%# Eval("option_name") %>

<asp:DropDownList ID="dlOptions" runat="server"
DataSource='<%#
GetOptionValuesDL((int)DataBinder.Eval(Container.DataItem,
"Item_ID"))%>'
DataTextField="option_display" DataValueField="option_value_id">

</asp:DropDownList>

</ItemTemplate>
</asp:Repeater>


Then in the codebehind we have the function GetOptionValuesDL which
returns a SqlDataReader which is databound to the DropDownList:


protected SqlDataReader GetOptionValuesDL(int intItemID)
{
ProductsDB options = new ProductsDB();
SqlDataReader dr = options.GetOptionValues(intItemID);
return dr;
}


That works. But now I want to insert a listitem as the default in each
created dropdown. Since this has to be done after the dropdownlists are
bound, where can I do it? I got close with the PreRender event of the
DropDownLists:


protected void dlOptions_PreRender(object sender, System.EventArgs e)
{

DropDownList dlClone =
(DropDownList)rptOptions.Items[0].FindControl("dlOptions");
dlClone.Items.Insert(0, "Select");

}


The problem: this obviously only works for the first created
DropDownList (Items[0]) -- I haven't been able to come up with a way to
programmatically find each DropDownList in this manner.

Any ideas?

Thanks

Jack
 
M

Mahesh

you can add default item to dropdownlist into way
1. after your dataset get fill add a datarow at position 0 in dataset.

2. after bind of dropdownlist add new listitem to dropdown list at 0th
position
 
J

jjack100

Mahesh,

Yeah, I know that. The point of my question, though, was how to do that
in this specific scenario.
 
M

Mahesh

right now i have no code so i guide you how to do it

1. after your dataset get fill add a datarow at position 0 in dataset.
declare function that fill dataset for example
dataser binddrplist("query")
{

add dummy row to dataset at 0th position

return dataset
}
now i think u should know when to call this function, on databound
event of repeater findcontrol and call the bind function.

2. after bind of dropdownlist add new listitem to dropdown list at 0th
position

on itemdatabount event of dropdownlist add dummy item to list at oth
position.
 
J

jjack100

I really have a syntax problem, not a concept problem. I have already
inserted the item at the desired position, I just don't know how to
insert it for each ddlist in the scenario I have: 1) I'm not using a
dataset. 2) the code I posted illustrates the scenario. So, yes, I
know that in theory if you use a dataset and insert a dummy row or
insert a listitem after the list is databound you can create the
desired listitem -- but that doesn't really apply to my question.

Can anyone look at the code I posted and offer any advice?

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

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,100
Latest member
MelodeeFaj
Top