locked
repeater RRS feed

  • Question

  • User916635734 posted

    Hi

    I have 3 questions about the repeater

    1- is it possible to format the data instead of using raw data from the database, I can't seem to find a way, like in this example :

    <div class="div-table-col" style="font-size:10px"><%#DataBinder.Eval(Container.DataItem, "DateMessage")%></div>

    I tried to put it in a variable but the content is displayed anyway

    2- inside the repeater I have an <asp:checkbox tag and I wan't to dynamically assign them an Id. It doesn't seem to be possible. Thefollowing code :

    <div class="div-table-col"><center><asp:CheckBox ID="<%#DataBinder.Eval(Container.DataItem, "Id")%>" runat="server"/></center></div>

    gives me "The server tag is not well formed."

    3- When the container is empty what is the best way to know it so you can display a message like "the list in empty"

    Thank you

    Monday, January 16, 2012 3:26 PM

Answers

  • User-1305187887 posted

    1. You can format the data from Databinder. Refer these.

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

    http://stackoverflow.com/questions/275194/formatting-databinder-eval-data

    http://forums.asp.net/t/1086781.aspx

    2. In your code, <div class="div-table-col"><center><asp:CheckBox ID="<%#DataBinder.Eval(Container.DataItem, "Id")%>" runat="server"/></center></div>


    try giving ID='<%#DataBinder.Eval(Container.DataItem, "Id")%>' ... Use single Quotes instead of double quotes and your issue no 2 will be solved.

    3. You can have a label with visible = false. And while you bind your dataset to the repeater, You can check if the dataset's row count is empty. IF so, set Visible = true for the label and then Set the message in that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 16, 2012 11:08 PM
  • User916635734 posted

    Thank you for your input, MVC is something I will look into the future. However, for my current projet, I needed a way to do this asp.net style. For the benefit of other reader who might need something similar, here's how I formated raw data from the database

    first the repeater :

    <asp:repeater ID="rptExemple" runat="server" OnItemDataBound="rptExemple_ItemDataBound">
                    <ItemTemplate>
                        <b id="test" runat="server">Eval("Id")</b>                    
                    </ItemTemplate>
                </asp:repeater>

    then the code behind on itemdatabound

        protected void rptExemple_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {   
    //GetInt32(0) the 0 is the first field returned by the query if (((System.Data.Common.DbDataRecord)e.Item.DataItem).GetInt32(0) == 0) { ((System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("test")).InnerHtml = "ID = 0"; } else { ((System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("test")).InnerHtml = "ID > 0"; } } }

    Or you could use a struct and a list to the struct

    MyStruct myStruct = new MyStruct();
    List<MyStruct> listMyStruct = new List<MyStruct>
    
    while (dataReader.read())
    {
         myStruct.Id = dataReader[0];
         if (myStruct.id == 0)
         {
              myStruct.whatever = "ID = 0";
         }
         else
         {
              myStruct.whatever = "ID > 0";
         }
         listMyStruct.add(myStruct)
    }
    
    //bind the list to the repeater
    rpt.DataSource = listMyStruct;
    rpt.DataBind();
    
    
    //In the repeater simply output it
    <b><%#Eval("whatever")%></b>
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 15, 2012 1:28 PM

All replies

  • User-1305187887 posted

    1. You can format the data from Databinder. Refer these.

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

    http://stackoverflow.com/questions/275194/formatting-databinder-eval-data

    http://forums.asp.net/t/1086781.aspx

    2. In your code, <div class="div-table-col"><center><asp:CheckBox ID="<%#DataBinder.Eval(Container.DataItem, "Id")%>" runat="server"/></center></div>


    try giving ID='<%#DataBinder.Eval(Container.DataItem, "Id")%>' ... Use single Quotes instead of double quotes and your issue no 2 will be solved.

    3. You can have a label with visible = false. And while you bind your dataset to the repeater, You can check if the dataset's row count is empty. IF so, set Visible = true for the label and then Set the message in that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 16, 2012 11:08 PM
  • User3866881 posted

    Hello MoneyLoser:)

    Here's my addition——Agree with Deepesh Kumar S first!

    In fact,if you are using ASP.NET2.0,you can totally use Eval or Bind instead of DataBinder.Eval……Example looks like this following after you've finished binding to the Repeater by setting its public property "DataSource"。

    <div class="div-table-col">
    <center>
        <asp:CheckBox ID='<%#Eval("Id")%>' runat="server"/>
    </center>
    </div>
    Tuesday, January 17, 2012 9:11 PM
  • User916635734 posted

    Thanks for your answers guys

    I have another question that comes to my mind. My background is php and classic asp and I was wondering if you can always get away with their controls for the code behind. In the exemple above, I had many conditions depending on the data that came from the db that I decided to simply use a datareader and format a portion of my html in the code behind in c# and reponse.write it the aspx file. But I feel guilty about it so...

    Can you always avoid doing this with their control?

    Wednesday, January 18, 2012 11:34 PM
  • User3866881 posted

    Thanks for your answers guys

    I have another question that comes to my mind. My background is php and classic asp and I was wondering if you can always get away with their controls for the code behind. In the exemple above, I had many conditions depending on the data that came from the db that I decided to simply use a datareader and format a portion of my html in the code behind in c# and reponse.write it the aspx file. But I feel guilty about it so...

    Can you always avoid doing this with their control?

    Hello again:)

    Considering it that you are using the general asp and php coding……Because PHP or asp is quite different from that of design in asp.net……Well,it seems that you will prefer asp.net MVC。

    Saturday, January 21, 2012 8:54 PM
  • User916635734 posted

    Considering it that you are using the general asp and php coding……Because PHP or asp is quite different from that of design in asp.net……Well,it seems that you will prefer asp.net MVC

    Hello again :)

    I don't know if I should start a new thread or continue with this one but anyway, I guess my question should have been when using the repeater (or any other control for that matter) is there a way to test the data that comes from the db for instance:

    <div>
    <%# if (Eval("Id")==0){ %><b>print this </b><%}else{%><b>print that </b><%}%>
    </div>

    I know this is not working, but I am sure you can see what I am trying to accomplish :)

    Wednesday, January 25, 2012 1:14 PM
  • User3866881 posted

    <div> <%# if (Eval("Id")==0){ %><b>print this </b><%}else{%><b>print that </b><%}%> </div>

    Hello:)

    Code like what you show us above isn't a real ASP.NET,generally speaking,ADO.NET prefers data controls such as Repeater or something like this……So your codes should fit asp.net mvc instead of pure general ado.net+Repeater。A new thread can be sent at MVC

    Reguards!

    Thursday, January 26, 2012 4:50 AM
  • User916635734 posted

    Thank you for your input, MVC is something I will look into the future. However, for my current projet, I needed a way to do this asp.net style. For the benefit of other reader who might need something similar, here's how I formated raw data from the database

    first the repeater :

    <asp:repeater ID="rptExemple" runat="server" OnItemDataBound="rptExemple_ItemDataBound">
                    <ItemTemplate>
                        <b id="test" runat="server">Eval("Id")</b>                    
                    </ItemTemplate>
                </asp:repeater>

    then the code behind on itemdatabound

        protected void rptExemple_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {   
    //GetInt32(0) the 0 is the first field returned by the query if (((System.Data.Common.DbDataRecord)e.Item.DataItem).GetInt32(0) == 0) { ((System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("test")).InnerHtml = "ID = 0"; } else { ((System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("test")).InnerHtml = "ID > 0"; } } }

    Or you could use a struct and a list to the struct

    MyStruct myStruct = new MyStruct();
    List<MyStruct> listMyStruct = new List<MyStruct>
    
    while (dataReader.read())
    {
         myStruct.Id = dataReader[0];
         if (myStruct.id == 0)
         {
              myStruct.whatever = "ID = 0";
         }
         else
         {
              myStruct.whatever = "ID > 0";
         }
         listMyStruct.add(myStruct)
    }
    
    //bind the list to the repeater
    rpt.DataSource = listMyStruct;
    rpt.DataBind();
    
    
    //In the repeater simply output it
    <b><%#Eval("whatever")%></b>
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 15, 2012 1:28 PM