DropDownList with Custom Content

  • Thread starter Thread starter EdisonCPP
  • Start date Start date
E

EdisonCPP

Hi,

I have a DetailsView with a template field with a dropdownlist added to it.
If the data coming into that field were put into a textbox, it would look
like:
Small|Medium|Large|X-Large.
I need to break that out into dropdownlistitems with each text being the
value also.
I've tried using a ObjectDataSource unsuccesfully. What would be a valid
way of handling this?

Thanks in advance!

Steven
 
Also to be clear....

The DetailsView is bound to a SqlDataSource, there is a nvarchar
field called Choice1 with things such as:
Small|Medium|Large|X-Large
-or-
Red|Black|Yellow|Blue
so instead of binding it to a textbox
Text='<%# Bind("Choice1") %>'
I need it to break it out into a DropDown. It's the same dataset
as the rest of the DetailsView.

Thanks again!
 
Hi Steven,

From your description you have a field called "Choice1" in your data table.
The data of that field is either "Small|Medium|Large|X-Large" or
"Red|Black|Yellow|Blue". You want to display the data via the DropDownList
in the DetailsView's ItemTemplate. Say the data is
"Small|Medium|Large|X-Large" you want to populate four items with text
"Small", "Medium", "Large", "X-Large". Is my understanding correct?

If so please try following code. In the DataBound event handler of the
DetailsView we can get the reference of the DropDownList and populate it
manually:

Aspx:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:AConnectionString %>"
SelectCommand="SELECT * FROM [Table1]"></asp:SqlDataSource>

<asp:DetailsView AllowPaging="true" ID="DetailsView1"
runat="server" AutoGenerateRows="False"
DataSourceID="SqlDataSource1" Height="50px" Width="125px"
ondatabound="DetailsView1_DataBound">
<Fields>
<asp:TemplateField><ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate></asp:TemplateField>
</Fields>
</asp:DetailsView>

Aspx.cs:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
DetailsView d=(DetailsView)sender;
DropDownList ddl = d.FindControl("DropDownList1") as
DropDownList;
if (ddl != null)
{
DataRowView drv = (DataRowView)d.DataItem;
string data = drv.Row["Choice1"].ToString();
string[] datalist = data.Split('|');
ddl.DataSource = datalist;
ddl.DataBind();
}
}

Please test my code to see if it works. If my understanding is wrong please
correct me and clarify the scenario.
Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "EdisonCPP" <[email protected]>
| References: <#[email protected]>
| Subject: Re: DropDownList with Custom Content
| Date: Fri, 17 Oct 2008 16:05:48 -0400
| Lines: 33
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
| X-RFC2646: Format=Flowed; Response
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: cpe-071-068-080-144.carolina.res.rr.com 71.68.80.144
| Path: TK2MSFTNGHUB02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTNGP04.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet:78090
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Also to be clear....
|
| The DetailsView is bound to a SqlDataSource, there is a nvarchar
| field called Choice1 with things such as:
| Small|Medium|Large|X-Large
| -or-
| Red|Black|Yellow|Blue
| so instead of binding it to a textbox
| Text='<%# Bind("Choice1") %>'
| I need it to break it out into a DropDown. It's the same dataset
| as the rest of the DetailsView.
|
| Thanks again!
|
|
| > Hi,
| >
| > I have a DetailsView with a template field with a dropdownlist added to
| > it.
| > If the data coming into that field were put into a textbox, it would
look
| > like:
| > Small|Medium|Large|X-Large.
| > I need to break that out into dropdownlistitems with each text being
the
| > value also.
| > I've tried using a ObjectDataSource unsuccesfully. What would be a
valid
| > way of handling this?
| >
| > Thanks in advance!
| >
| > Steven
| >
|
|
|
 
Allen,

Thanks! That worked perfectly.
The biggest thing I was missing was being able to get to the
data:

DataRowView drv = (DataRowView)d.DataItem;

Thanks for your help.

Steven
Hi Steven,

From your description you have a field called "Choice1" in your data
table.
The data of that field is either "Small|Medium|Large|X-Large" or
"Red|Black|Yellow|Blue". You want to display the data via the DropDownList
in the DetailsView's ItemTemplate. Say the data is
"Small|Medium|Large|X-Large" you want to populate four items with text
"Small", "Medium", "Large", "X-Large". Is my understanding correct?

If so please try following code. In the DataBound event handler of the
DetailsView we can get the reference of the DropDownList and populate it
manually:

Aspx:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:AConnectionString %>"
SelectCommand="SELECT * FROM [Table1]"></asp:SqlDataSource>

<asp:DetailsView AllowPaging="true" ID="DetailsView1"
runat="server" AutoGenerateRows="False"
DataSourceID="SqlDataSource1" Height="50px" Width="125px"
ondatabound="DetailsView1_DataBound">
<Fields>
<asp:TemplateField><ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate></asp:TemplateField>
</Fields>
</asp:DetailsView>

Aspx.cs:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
DetailsView d=(DetailsView)sender;
DropDownList ddl = d.FindControl("DropDownList1") as
DropDownList;
if (ddl != null)
{
DataRowView drv = (DataRowView)d.DataItem;
string data = drv.Row["Choice1"].ToString();
string[] datalist = data.Split('|');
ddl.DataSource = datalist;
ddl.DataBind();
}
}

Please test my code to see if it works. If my understanding is wrong
please
correct me and clarify the scenario.
Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
| From: "EdisonCPP" <[email protected]>
| References: <#[email protected]>
| Subject: Re: DropDownList with Custom Content
| Date: Fri, 17 Oct 2008 16:05:48 -0400
| Lines: 33
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
| X-RFC2646: Format=Flowed; Response
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: cpe-071-068-080-144.carolina.res.rr.com 71.68.80.144
| Path: TK2MSFTNGHUB02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTNGP04.phx.gbl
| Xref: TK2MSFTNGHUB02.phx.gbl
microsoft.public.dotnet.framework.aspnet:78090
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Also to be clear....
|
| The DetailsView is bound to a SqlDataSource, there is a nvarchar
| field called Choice1 with things such as:
| Small|Medium|Large|X-Large
| -or-
| Red|Black|Yellow|Blue
| so instead of binding it to a textbox
| Text='<%# Bind("Choice1") %>'
| I need it to break it out into a DropDown. It's the same dataset
| as the rest of the DetailsView.
|
| Thanks again!
|
|
| > Hi,
| >
| > I have a DetailsView with a template field with a dropdownlist added
to
| > it.
| > If the data coming into that field were put into a textbox, it would
look
| > like:
| > Small|Medium|Large|X-Large.
| > I need to break that out into dropdownlistitems with each text being
the
| > value also.
| > I've tried using a ObjectDataSource unsuccesfully. What would be a
valid
| > way of handling this?
| >
| > Thanks in advance!
| >
| > Steven
| >
|
|
|
 
Back
Top