how to get value of listbox item by item template

7 posts, 1 answers
  1. Baris
    Baris avatar
    34 posts
    Member since:
    Jul 2013

    Posted 09 Mar 2014 Link to this post

    I have a listbox. And I am using item template. like that

        <telerik:RadListBox ID="lbOutputColumns" CssClass="RadListBox2" runat="server"
            SelectionMode="Multiple" AllowReorder="true" AutoPostBackOnReorder="true" EnableDragAndDrop="true"
            Skin="" Width="300px">
            <ItemTemplate>
                <div class="variables-list" style="width: 300px !important">
                    <span><%# Container.DataItem %></span>
                </div>
            </ItemTemplate>
            <Items>
                <telerik:RadListBoxItem Text="USA"></telerik:RadListBoxItem>
                <telerik:RadListBoxItem Text="Turkey"></telerik:RadListBoxItem>
                <telerik:RadListBoxItem Text="Greece"></telerik:RadListBoxItem>
            </Items>
        </telerik:RadListBox>

    When I used 

        <%# Container.DataItem %>

     listbox doesnt show me items text. how can I get text in radlistboxitem by using item template. 
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 09 Mar 2014 in reply to Baris Link to this post

    Hi Baris,

    Please do the following modifications in your ASPX page and before the template can use the ListBox item properties to bind the elements in the template, the application needs to explicitly bind the items by calling the DataBind method of RadListBox. Please take a look into this help documentation for further information.

    ASPX:
    ...         
      <ItemTemplate>
         <div class="variables-list" style="width: 300px !important">
             <span>
                 <%# DataBinder.Eval(Container, "Text")%>
             </span>
         </div>
       </ItemTemplate>
    ...

    C#:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lbOutputColumns.DataBind();
        }
    }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Baris
    Baris avatar
    34 posts
    Member since:
    Jul 2013

    Posted 10 Mar 2014 in reply to Princy Link to this post

    It doesn't work. I suppose one parameter can't be "Text". Are you sure at that?
  5. Baris
    Baris avatar
    34 posts
    Member since:
    Jul 2013

    Posted 10 Mar 2014 Link to this post

    Hi,

    "Text" parameter in <%# DataBinder.Eval(Container, "Text")%> doenst work because I bind the data at runtime. listview hasnt got items at aspx side. Like that:  
       
    My code behind:    

    var result = client.GetColumnNames(orcl);

                lbDSNominal.DataSource = result.Data;
                lbDSNominal.DataBind();
     

    my aspx side :  listview which I dragged item is like that :

                    <telerik:RadListBox ID="lbDSNominal" runat="server" Width="250px" BackColor="#2D1A44" SelectionMode="Multiple" AllowTransfer="true" AutoPostBackOnTransfer="true" TransferMode="Copy"
                                            AllowReorder="true" AutoPostBackOnReorder="true" EnableDragAndDrop="true" Skin="" CssClass="list contents_2" TransferToID="lbStrata">
                                            <ItemTemplate>
                                                <p style="font-size: 15px !important; width: auto; height: auto; color: white; padding-bottom: 0px !important; margin-bottom: 0px !important; margin-top: 0px !important;"><%# Container.DataItem %></p>
                                            </ItemTemplate>

                                        </telerik:RadListBox>

  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 Mar 2014 in reply to Baris Link to this post

    Hi Baris,

    Please have a look into the sample code snippet which works fine at my end.

    ASPX:
    <telerik:RadListBox ID="lbDSNominal" runat="server" Width="250px" BackColor="#2D1A44"
        SelectionMode="Multiple" AllowTransfer="true" AutoPostBackOnTransfer="true" TransferMode="Copy" AllowReorder="true" AutoPostBackOnReorder="true" EnableDragAndDrop="true">
        <ItemTemplate>
            <p style="font-size: 15px !important; width: auto; height: auto; color: white; padding-bottom: 0px !important;
                margin-bottom: 0px !important; margin-top: 0px !important;">
                 <%# DataBinder.Eval(Container.DataItem, "Cityname")%>
        </ItemTemplate>
    </telerik:RadListBox>

    C#:
    protected void Page_Load(object sender, EventArgs e)
    {
        String connstring = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connstring);
        SqlDataAdapter adapter = new SqlDataAdapter();
         adapter.SelectCommand = new SqlCommand("SELECT Cityname from City", conn);
        DataTable data = new DataTable();
        conn.Open();
        try
        {
            adapter.Fill(data);
            lbDSNominal.DataSource = data;
            lbDSNominal.DataTextField = "Cityname";
            lbDSNominal.DataBind();
        }
        finally
        {
            conn.Close();
        }
    }

    Let me know if you have any concern.
    Thanks,
    Princy.
  7. Baris
    Baris avatar
    34 posts
    Member since:
    Jul 2013

    Posted 11 Mar 2014 in reply to Princy Link to this post

    Hi, 
    I don't use SqlDataAdapter(). I use only String[] for datasource. So I couldn't use DSNominal.DataTextField = "Cityname"; Because string array hasnt got any DataTextField property. 

    Can you help me one more time :)
  8. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 11 Mar 2014 in reply to Baris Link to this post

    Hi Baris,

    Please try the following code snippet.

    ASPX:
    ...
    <%# DataBinder.Eval(Container.DataItem,"Name")%>
    ...

    C#:
    var departments = new[] {
            new { Name = "ACME Corporation" },
            new { Name = "Microsoft Corporation" },
            new { Name = "Google, Inc" }
    };
    lbDSNominal.DataSource = departments;
    lbDSNominal.DataBind();

    Please provide your full code i it doesn't help
    Thanks,
    Princy.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017