This is a migrated thread and some comments may be shown as answers.

how to get value of listbox item by item template

6 Answers 240 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Baris
Top achievements
Rank 1
Baris asked on 09 Mar 2014, 09:37 AM
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. 

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 10 Mar 2014, 03:12 AM
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.
0
Baris
Top achievements
Rank 1
answered on 10 Mar 2014, 03:15 PM
It doesn't work. I suppose one parameter can't be "Text". Are you sure at that?
0
Baris
Top achievements
Rank 1
answered on 10 Mar 2014, 03:24 PM
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>

0
Princy
Top achievements
Rank 2
answered on 11 Mar 2014, 04:00 AM
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.
0
Baris
Top achievements
Rank 1
answered on 11 Mar 2014, 07:38 AM
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 :)
0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Mar 2014, 08:17 AM
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.
Tags
ListBox
Asked by
Baris
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Baris
Top achievements
Rank 1
Share this question
or