12

I want to bind dropdownlist to List<MyIem>, in code behind.

 <asp:DropDownList ID="listCategories"  runat="server" Height="20px"   CssClass="CategoryDropList" SelectedValue='<%# Bind("ParentId") %>' AutoPostBack="false" Width="300px">      

Without using ObjectDataSource !

How can I Bind it to the dropdown list? In what event?

Also SelectedValue='<%# Bind("ParentId") %>' should work! (I mean the dropdownlist binding should occur before this!)

abatishchev
  • 98,240
  • 88
  • 296
  • 433
markiz
  • 2,164
  • 6
  • 34
  • 47
  • 1. Could you clarify the ASP.NET version you are working on? 2. I got it to work, but to be sure, I need to see your code because your question provides too few details. – Cerebrus Jun 02 '09 at 11:11
  • Second response is much better... please mark it as the answer – nikib3ro Aug 29 '11 at 22:40

3 Answers3

5

Made an example which will set the dropdown in the DataBound event.
Here is the markup
The way to use the ddl, is to find it with findcontrol() during DataBound event.
When you have the control in the DataBound event, you can also bind the dropdown to your List<>
Hope this helps.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>

        </div>
        <asp:FormView ID="FormView1" runat="server" ondatabound="FormView1_DataBound">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem>One</asp:ListItem>
                    <asp:ListItem>Two</asp:ListItem>
                    <asp:ListItem>Three</asp:ListItem>
                </asp:DropDownList>

            </ItemTemplate>
        </asp:FormView>
        </form>
    </body>
    </html>

Here is the code behind:

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            list.Add("Some");
            list.Add("Other");

            FormView1.DataSource = list; //just to get the formview going

            FormView1.DataBind(); 

        }

        protected void FormView1_DataBound(object sender, EventArgs e)
        {
            DropDownList ddl = null;
            if(FormView1.Row != null)
                ddl = (DropDownList) FormView1.Row.FindControl("DropDownList1");
            ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue("Two"));
        }
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Kb.
  • 7,240
  • 13
  • 56
  • 75
  • But I also need to populate DropDownList in code behind, and not like One – markiz May 24 '09 at 16:41
  • 1
    Don't know if this is important but dropdownlist is in EditTemplate of FormView. – markiz May 24 '09 at 16:52
  • You could populate the DropDownList in the DataBound event. Once you have the ddl reference: Get the data as List, set the ddl.DataSource to the list, and do ddl.DataBind(). If you are in EditTemplate, make sure you have unique IDs of the ddl across edit an item template. Hope you make it work. – Kb. May 24 '09 at 20:07
  • I can't populate it in DataBound Event because it will be to late for SelectedValue='<%# Bind("ParentId") %>' to work. – markiz May 24 '09 at 20:46
  • I see your point. One suggestion could be to could populate in DataBound and set SelectedValue in DataBound event in code behind and not in the form. – Kb. May 25 '09 at 06:21
  • But if I won't have SelectedValue='<%# Bind("ParentId") %>' on the form, will I be able to Bind the selected value back to the database? (When I click on update button in FormView) – markiz May 25 '09 at 06:57
  • If two way databind doesn't work (as it does in Winforms - do not think ASP.Net has two-way databinding), you could do FindControl when you press the update button. – Kb. May 27 '09 at 06:52
  • @markiz: Here is an article about two-way binding: http://msdn.microsoft.com/en-us/magazine/cc163505.aspx – Kb. May 30 '09 at 07:49
4

You can populate the DropDownList with another DataSource, assuming the valid values are in the database. Check out this video:

http://msdn.microsoft.com/en-us/data/cc546554.aspx

It's using an EntityDataSource instead of an ObjectDataSource, but the principle should still work.

If you want a "(none)" type option for null, see section "Converting Null in Template Fields" on this page:

http://msdn.microsoft.com/en-us/library/ms366709.aspx

Specifically:

<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2"
    DataTextField="Name" DataValueField="EmployeeID"
    SelectedValue='<%# Bind("ReportsTo") %>' AppendDataBoundItems="True">
        <asp:ListItem Selected="True" Value="">(none)</asp:ListItem>
</asp:DropDownList>

Notice the the "AppendDataBoundItems" attribute and the "asp:ListItem" element.

Pixel
  • 374
  • 5
  • 15
1

well i was facing a similar problem. i noticed that you were trying to add it to and not which can be a major cause that you did not see it.

i have however worked on both the solutions provided above and found this worked for me :-

<EditItemTemplate>
<asp:DropDownList ID="ddlStream" runat="server" SelectedValue='<%# Bind("Stream") %>'>
                    <asp:ListItem>Common</asp:ListItem>
                    <asp:ListItem>Mechanical</asp:ListItem>
                    <asp:ListItem>Electronics</asp:ListItem>
                    </asp:DropDownList>
</EditItemTemplate>

<ItemTemplate>
<asp:Label runat="server" id="something" text='<%# Eval("Stream")%>'/>
</ItemTemplate>

hope this helpes you.

haxpak
  • 105
  • 2
  • 8