473,396 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to use dynamic column name in DataBinder.Eval ?

I am creating a repater column programmatically, I can' t do with loops, please help me!


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <% 
  4. foreach (string strTest in getRepeaterItemColumnName())
  5. {
  6. %>                       
  7. <%# DataBinder.Eval(Container.DataItem, strTest) %>                    
  8. <%
  9. }
  10. %>
  11.  
  12.  
This syntax wrong because strTest does not local varible ! - what is correct syntax?


Thank You..
Dec 21 '07 #1
8 9907
Plater
7,872 Expert 4TB
Shouldn't this be:
Expand|Select|Wrap|Line Numbers
  1. <% 
  2. foreach (string strTest in getRepeaterItemColumnName())
  3. {                    
  4.    DataBinder.Eval(Container.DataItem, strTest)                    
  5. }
  6. %>
  7.  
Dec 21 '07 #2
Frinavale
9,735 Expert Mod 8TB
I am creating a repater column programmatically, I can' t do with loops, please help me!


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <% 
  4. foreach (string strTest in getRepeaterItemColumnName())
  5. {
  6. %>                       
  7. <%# DataBinder.Eval(Container.DataItem, strTest) %>                    
  8. <%
  9. }
  10. %>
  11.  
  12.  
This syntax wrong because strTest does not local varible ! - what is correct syntax?


Thank You..

Is this a PHP question?
Whenever I've used repeaters I've never had to loop through the DataSource.
I simply set the Repeater's DataSource and performed a DataBind...the ASP.NET technology took care of the rest...

Example of what it would look like:
(This is not going to work as is)
Expand|Select|Wrap|Line Numbers
  1.   <asp:Repeater ID="MyRepeater" runat="server">
  2.                     <ItemTemplate>
  3.                           <%# DataBinder.Eval(Container.DataItem, strTest) %>  
  4.                      </ItemTemplate>
  5.    </asp:Repeater>
  6.  
Expand|Select|Wrap|Line Numbers
  1.  Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  2.    MyRepeter.DataSource = _myStringTestArray
  3.     MyRepeater.DataBind()
  4. End Sub
-Frinny
Dec 21 '07 #3
Shouldn't this be:
Expand|Select|Wrap|Line Numbers
  1. <% 
  2. foreach (string strTest in getRepeaterItemColumnName())
  3. {                    
  4.    DataBinder.Eval(Container.DataItem, strTest)                    
  5. }
  6. %>
  7.  
Hi Plater

This code not correct, Because databinder method not working between <% %> block

Databinder metod working between <%# %> block "#"

Error : The name 'Container' does not exist in the current context
Dec 21 '07 #4
Is this a PHP question?
Whenever I've used repeaters I've never had to loop through the DataSource.
I simply set the Repeater's DataSource and performed a DataBind...the ASP.NET technology took care of the rest...

Example of what it would look like:
(This is not going to work as is)
Expand|Select|Wrap|Line Numbers
  1.   <asp:Repeater ID="MyRepeater" runat="server">
  2.                     <ItemTemplate>
  3.                           <%# DataBinder.Eval(Container.DataItem, strTest) %>  
  4.                      </ItemTemplate>
  5.    </asp:Repeater>
  6.  
Expand|Select|Wrap|Line Numbers
  1.  Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  2.    MyRepeter.DataSource = _myStringTestArray
  3.     MyRepeater.DataBind()
  4. End Sub
-Frinny

Hi
question is not PHP ! ( has been used wrong tag in a HTML editor)

I am know Repeater, DataSource, DataBind .Ado.NET etc.

My problem ( DataBinder.Eval(Container.DataItem, "column name") ) Manual writing string "column name" I want to dynamic column name I use getRepeaterItemColumnName function



Expand|Select|Wrap|Line Numbers
  1.   protected Array getRepeaterItemColumnName()
  2.     {
  3.         string[] array = { "tbl_time", "tbl_date", "tbl_status"};
  4.         return array;
  5.     }
  6.  
  7.  
Sorry I so so speaking English
Dec 21 '07 #5
Frinavale
9,735 Expert Mod 8TB
Hi
Expand|Select|Wrap|Line Numbers
  1.   protected Array getRepeaterItemColumnName()
  2.     {
  3.         string[] array = { "tbl_time", "tbl_date", "tbl_status"};
  4.         return array;
  5.     }
  6.  
  7.  
Sorry I so so speaking English
Here's an example of a table with dynamically created columns:
Expand|Select|Wrap|Line Numbers
  1. <table id="MyRepeaterTable" border="1" cellpadding="0" cellspacing="0"  height="<%= TableHeight() %>px" >
  2. <!-- TableHeight() is a property of my control-->
  3.            <tr>
  4.                <asp:Repeater ID="MyRepeater" runat="server">
  5.                     <ItemTemplate>
  6.                 <th id="<%# DataBinder.Eval(Container.DataItem, "HeaderID") %>">
  7.                             <%# DataBinder.Eval(Container.DataItem, "HeaderTitle") %>                     
  8.                 </th>
  9.                     </ItemTemplate>
  10.                 </asp:Repeater>
  11.            </tr>
  12.        </table>      
  13.  
Expand|Select|Wrap|Line Numbers
  1. Public Partial Class MyExampleOfRepeater
  2.     Inherits System.Web.UI.UserControl
  3.  
  4.     Private _columns As ArrayList
  5.     Private _columnNames() As String =  { "tbl_time", "tbl_date", "tbl_status"}
  6.     Private _height As String = "24"
  7.  
  8.     Public Property Height() As String
  9.         Get
  10.             return _value
  11.         End Get
  12.         Set(ByVal value As String)
  13.             _height = value    
  14.         End Set
  15.  
  16.     End Property
  17.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  18.         For i As Integer = 0 to _columnNames.Length-1
  19.             _columns.Add(New ColumnTitle(i.ToString,_columnNames(i)))
  20.         Next
  21.     End Sub
  22.  
  23.     Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  24.         MyRepeater.DataSource = _columns
  25.         MyRepeater.DataBind
  26.     End Sub
  27.  
  28.  
  29. Private Class ColumnTitle
  30.    Private _name As String
  31.    Private _id As String
  32.    Public ReadOnly Property HeaderID As String
  33.        'This property is used by the ASP call: <%# DataBinder.Eval(Container.DataItem, "HeaderID") %>
  34.        Get
  35.            Return _id
  36.        End Get
  37.     End Property
  38.        Public ReadOnly Property HeaderTitle As String
  39.            'This property is used by the ASP call: <%# DataBinder.Eval(Container.DataItem, "HeaderTitle") %>
  40.        Get
  41.            Return _name
  42.        End Get
  43.     End Property
  44.  
  45.     Public Sub New(ByVal id As String, ByVal title As String)
  46.         _id = id
  47.         _title = title
  48.     End Sub
  49.  
  50. End Class
  51.  
  52. End Class
  53.  
Please note that the above code has not been tested and is not guaranteed to work.
Dec 21 '07 #6
Hi Frinavale

Thanks for your message, latest post it is not working ! (I could not make work)

I am trying another metod


Expand|Select|Wrap|Line Numbers
  1.  
  2. <script runat="server" >
  3.  string _i = "";
  4. </script>
  5.  
  6.  <asp:Repeater ID="rptData" runat="server" OnItemCommand="rptData_ItemCommand">
  7.  
  8.      <ItemTemplate>
  9.                 <tr onmouseover="this.bgColor='#F4F4F4';" onmouseout="this.bgColor='#FFFFFF';">
  10.                     <% foreach (string aa in  getset())
  11.                        {
  12.                            _i = aa.ToString();
  13.                            Response.Write(aa.ToString()); // for test
  14.  
  15.                     %>
  16.                     <td><%# DataBinder.Eval(Container.DataItem, _i.ToString()) %></td>
  17.                     <% } %>
  18.                 </tr>
  19.             </ItemTemplate>
  20.  </asp:Repeater>
  21.  
  22.  

Expand|Select|Wrap|Line Numbers
  1.  protected  Array getset() // for test
  2.     {
  3.         string[] array = { "tbl_time", "tbl_date", "tbl_status"} };
  4.         return array;
  5.     }
  6.  
  7.  
-- What new
  • string _i = ""; (Because accessing protected variables for Databinding Expressions syntax)
  • I want to replace _i at runtime in foreach loops

--Problem
  • _i is not replaced at runtime (loop item by item)

Or
  • <td><%# DataBinder.Eval(Container.DataItem, getcolumnname(0))%></td>
  • 0 (int arguments) = Array item ( return array[0];)
  • Problem int arguments (How can you I do to redouble ++)

Do you have any ideas ?
Thx.
Dec 22 '07 #7
Frinavale
9,735 Expert Mod 8TB
Hi Frinavale

Thanks for your message, latest post it is not working ! (I could not make work)

I am trying another metod


Expand|Select|Wrap|Line Numbers
  1.  
  2. <script runat="server" >
  3.  string _i = "";
  4. </script>
  5.  
  6.  <asp:Repeater ID="rptData" runat="server" OnItemCommand="rptData_ItemCommand">
  7.  
  8.      <ItemTemplate>
  9.                 <tr onmouseover="this.bgColor='#F4F4F4';" onmouseout="this.bgColor='#FFFFFF';">
  10.                     <% foreach (string aa in  getset())
  11.                        {
  12.                            _i = aa.ToString();
  13.                            Response.Write(aa.ToString()); // for test
  14.  
  15.                     %>
  16.                     <td><%# DataBinder.Eval(Container.DataItem, _i.ToString()) %></td>
  17.                     <% } %>
  18.                 </tr>
  19.             </ItemTemplate>
  20.  </asp:Repeater>
  21.  
  22.  

Expand|Select|Wrap|Line Numbers
  1.  protected  Array getset() // for test
  2.     {
  3.         string[] array = { "tbl_time", "tbl_date", "tbl_status"} };
  4.         return array;
  5.     }
  6.  
  7.  
-- What new
  • string _i = ""; (Because accessing protected variables for Databinding Expressions syntax)
  • I want to replace _i at runtime in foreach loops

--Problem
  • _i is not replaced at runtime (loop item by item)

Or
  • <td><%# DataBinder.Eval(Container.DataItem, getcolumnname(0))%></td>
  • 0 (int arguments) = Array item ( return array[0];)
  • Problem int arguments (How can you I do to redouble ++)

Do you have any ideas ?
Thx.
I'm not that great with C# so I'm going to give you an example in VB.NET
You should not have a for loop within your repeater...the purpose of a repeater is to "Repeat" through all of the items in the data source that it's bound to.

Your code should look something like this:
Expand|Select|Wrap|Line Numbers
  1. <table border="1">
  2.     <tr onmouseover="this.bgColor='#F4F4F4';" onmouseout="this.bgColor='#FFFFFF';">
  3.  <asp:Repeater ID="rptData" runat="server">
  4.      <ItemTemplate>
  5.                    <td><%#DataBinder.Eval(Container.DataItem, "Name")%></td>
  6.             </ItemTemplate>
  7.  </asp:Repeater>
  8.         </tr>    
  9.     </table>
  10.  
Your server side code should look something like this (only in C# instead of VB)
Expand|Select|Wrap|Line Numbers
  1.  
  2.     Private arr() As String = {"tbl_time", "tbl_date", "tbl_status"}
  3.  
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.         Dim titles As New ArrayList
  6.         For Each s As String In arr
  7.             titles.Add(New ColumnTitle(s))
  8.         Next
  9.         rptData.DataSource = titles
  10.         rptData.DataBind()
  11.     End Sub
  12.  
  13.     Private Class ColumnTitle
  14.         Private _name As String
  15.         Public Property Name() As String
  16.             Get
  17.                 Return _name
  18.             End Get
  19.             Set(ByVal value As String)
  20.                 _name = value
  21.             End Set
  22.         End Property
  23.         Public Sub New(ByVal name As String)
  24.             _name = name
  25.         End Sub
  26.     End Class
  27.  
Please note that the "Name" attribute in the following ASP code:
<%#DataBinder.Eval(Container.DataItem, "Name")%>
Refers to the Public Property Name in the ColumnTitle class.
When you use this, you have to call a Property of the Object that you are using as your data source for your repeater. The .ToString() will not work in this ASP call either.

So, we create an object that has a property that returns a string so that it can be displayed in your table. The Object in this example is the ColumnTitle class. We use an array of ColumnTitles as the source for our repeater. This way we can grab the Container.DataItem (which is the ColumnTitle object) and grab the value of the property we wish to display (the Name property).

Try this and see if it works for you.

-Frinny
Dec 24 '07 #8
Expand|Select|Wrap|Line Numbers
  1. <%for (int c = 0; c < RepeatColumns.Count; c++)
  2.                           {
  3.                         %>
  4.                         <td style="border: solid 1pt black; border-right: none 0pt white; border-top: none 0pt white">
  5.                             <%#DataBinder.Eval(Container.DataItem, RepeatColumns[Count] + "Total SLA")%>
  6.                         </td>
  7.                         <td style="border: solid 1pt black; border-right: none 0pt white; border-top: none 0pt white">
  8.                             <%#DataBinder.Eval(Container.DataItem, RepeatColumns[Count] + "Met")%>
  9.                         </td>
  10.                         <td style="border: solid 1pt black; border-right: none 0pt white; border-top: none 0pt white">
  11.                             <%#DataBinder.Eval(Container.DataItem, RepeatColumns[Count] + "Not Met]")%>
  12.                         </td>
  13.                         <td style="border: solid 1pt black; border-right: none 0pt white; border-top: none 0pt white">
  14.                             <%#DataBinder.Eval(Container.DataItem, RepeatColumns[Count] + "NA")%>
  15.                         </td>
  16.                         <td style="border: solid 1pt black; border-right: none 0pt white; border-top: none 0pt white">
  17.                             <%#DataBinder.Eval(Container.DataItem, RepeatColumns[Count] + "Met % age]")%>
  18.                         </td>
  19.                         <%
  20.                             Count++;
  21.                         }
  22.                         %>
1. The RepeatColumns contains List of Months declared as a property in code behind
2. Count is an integer, declared as a property in code behind

And the out is as follows:



Try it.
Sep 22 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
1
by: Dmitri Shvetsov | last post by:
Hi All, Is it possible at all, and if yes, then how? Or it's very/extremely hard to do that? Maybe somebody knows...) I'm writing a Web App using VS2003/C#/MSSQL. I'm currently having about...
1
by: Mike P | last post by:
I have a Hyperlink Column which I need to convert to a Template Column so that I can pass multiple parameters. My current code is like this and works for a single parameter: ...
3
by: JenHu | last post by:
I have a datagrid (dgFundStatus) which displays a column. When the Transmit_Date is null, how can I show "Not transmit yet" instead of null? Thanks. Sub BindData() Dim strSql As New...
6
by: Red | last post by:
Hi all, I would like to ask how to combine 2 field into one column. For example I have field first name and last name. When I show it to the datagrid I want to show it as one column, for...
5
by: Ben Fidge | last post by:
I'm using DataList to present tabular data but am often having problems with some rows column alignment being out of synch with the rest of the rows. My DataList looks similar to this...: ...
0
by: Sathyaish | last post by:
I want to display a column in a datagrid such that each item/row in that column acts as a hyperlink. Further, when I click on the hyperlink, it doesn't redirect to another page or even to itself....
1
by: Agnes | last post by:
I got a grid list the record, column1 is "number" When the user press colunn 1 , i want to show out the pdf file whose filename is same as column1's contents How can I do that ?? thanks a lot <a...
1
by: SunshineInTheRain | last post by:
The following code is dynamic create dropdownmenu which data within pulled from database However, the code work well on IE but not on Firefox. On Firefox, the whole mouseover and mouseout function...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.