2. In your code, <divclass="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.
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"。
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?
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。
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 :)
<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。
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
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>
MoneyLoser
Member
13 Points
12 Posts
repeater
Jan 16, 2012 07:26 PM|LINK
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 :
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 :
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
deepeshsp
Participant
850 Points
144 Posts
Re: repeater
Jan 17, 2012 03:08 AM|LINK
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.
Thank You...
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: repeater
Jan 18, 2012 01:11 AM|LINK
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>MoneyLoser
Member
13 Points
12 Posts
Re: repeater
Jan 19, 2012 03:34 AM|LINK
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?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: repeater
Jan 22, 2012 12:54 AM|LINK
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。
MoneyLoser
Member
13 Points
12 Posts
Re: repeater
Jan 25, 2012 05:14 PM|LINK
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 :)
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: repeater
Jan 26, 2012 08:50 AM|LINK
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!
MoneyLoser
Member
13 Points
12 Posts
Re: repeater
Feb 15, 2012 05:28 PM|LINK
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>