Databind and trying to get index in Listview - c#

Need a hand trying to get my rotating banner working properly on my website. I'm using the jquery cycle plugin which manages the rotation. Within my CMS I've got something called a smartform, which contains upto 6 pictures. The code below (something which I wrote by following a tutorial for the banner) works really well. However I would like to somehow get the index of the image and place it within the alt tags. What I am trying to achieve is the alt tag to say "Banner_(ImageIndexNumber)".
Hopefully someone can help, thanks all
C# Codebehind
private void BannerFill(int contentId)
{
try
{
uxBannerContentBlock.DefaultContentID = contentId;
uxBannerContentBlock.Fill();
string xml = uxBannerContentBlock.EkItem.Html;
SmartForm.RotatingBanner.BannerImage bannerGroup = (SmartForm.RotatingBanner.BannerImage)
Ektron.Cms.EkXml.Deserialize(typeof(SmartForm.RotatingBanner.BannerImage), xml);
List<BannerSlide> slides = GetBannerSlides(bannerGroup.Slides);
//Databind//
uxBannerRepeater.DataSource = slides;
uxBannerRepeater.DataBind();
}
catch { }
}
protected List<BannerSlide>
GetBannerSlides(SmartForm.RotatingBanner.BannerImageSlides[] bannerGroupSlides)
{
List<BannerSlide> bSlides = new List<BannerSlide>();
foreach (SmartForm.RotatingBanner.BannerImageSlides bgSlide in bannerGroupSlides)
{
bSlides.Add(new BannerSlide(bgSlide.Image.img.src));
}
return bSlides;
}
public class BannerSlide
{
//properties//
public string SlideImage { get; set; }
//constructor//
public BannerSlide(string slideImage)
{
SlideImage = slideImage;
}
}
Front end
<div class="slideshow">
<CMS:ContentBlock ID="uxBannerContentBlock" runat="server" Visible="false" />
<asp:Repeater runat="server" ID="uxBannerRepeater">
<ItemTemplate>
<img src="<%# DataBinder.Eval( Container.DataItem,"SlideImage") %>" alt="Banner_<%# Container.ItemIndex %>" />
</ItemTemplate>
</asp:Repeater>

alt='<%# "Banner_" + Container.ItemIndex %>'

Related

Asp repeater control

I have a problem with the repeater asp control.
I have a table on my database called course with columns CourseID, CourseName, CourseLink and another table called module with columns ModuleID, ModuleName. And another table called timetable with columns CourseID and ModuleID.
The way this works is when I click on HyperLink1,the paragraph bellow with the the same course id, it would changes the paragraph's style display from hidden to show (I used JavaScript to complete this function so it's not the problem here).
What I would like to know is, how can I list the ModuleNames That relate to that specif course ID.
I was thinking of using another repeater inside a repeater. However that just complicate things and to be hones really confuses me.
How can I get this to work, don't work about the sql because I am comfortable in creating a select command.
Here is the the ASP.NET forum.
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="DataSource1">
<ItemTemplate>
<p>
<a id="HyperLink1" runat="server" href='<%# Eval("courselink") %>'>
<%# Eval("CourseName") %>
</a> <br />
</p>
<p id='<%# Eval("CourseID") %>' style="display:none">
<%# Eval("ModulName") %>
</p>
<br />
</ItemTemplate>
</asp:Repeater>
Thank you for your time and I look forward to finding out what I can do to solve this problem.
You pretty much will need to use another repeater, the trick (if I remember correctly has I haven't used the legacy ASP.NET Web Forms since 2011) is that you hook into the ItemDataBound event in your code behind for the parent repeater and then use the Event Arguments to get the id that you associate to your child table, get that data, lookup the repeater by id from Repeater (I think it's the sender but there is some way to get at it) and bind the data.
I want to note this is much easier and cleaner with ASP.NET MVC and the use of ASP.NET Web Forms is generally discouraged.
It could be the worst this way
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="DataSource1">
<ItemTemplate>
<p>
<a id="HyperLink1" runat="server" href='<%# Eval("courselink") %>'>
<%# Eval("CourseName") %>
</a> <br />
</p>
<p id='<%# Eval("CourseID") %>' style="display:none">
<%# moduleName(Eval("id").ToString()) %>
</p>
<br />
</ItemTemplate>
public string moduleName(string id)
{
string returnValue = "<ul>";
using (SqlConnection con = new SqlConnection(DB))
{
using (SqlCommand com = new SqlCommand("SELECT * FROM module WHERE id = #id", con))
{
com.Parameters.AddWithValue("#id", id);
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
using (SqlDataReader dr = com.ExecuteReader())
{
while (dr.Read())
{
returnValue += "<li>"+ dr["modulename"].ToString()+"</li>";
}
}
con.Close();
}
}
}
returnValue += "</ul>";
return returnValue;
}
The best solution would be to create a strongly typed model for your presentation and don't use Eval, you would get something like this in your webform:
<asp:Repeater runat="server" DataSourceID="<%# Model %>">
<ItemTemplate>
<p>
<%# ((Course)Container.DataItem).Name %>
</p>
<p id='<%# ((Course)Container.DataItem).ID %>' style="display:none">
<asp:Repeater runat="server" DataSource="<%# ((Course)Container.DataItem).Modules %>">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><%# ((Module)Container.DataItem).Name %></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</p>
</ItemTemplate>
Then in your code behind create the following property:
public List<Course> Model
{
get
{
List<Course> courses = new List<Course>();
// Some example data, in your situation you should instantiate the classes based on the data from the database.
Course exampleCourse = new Course();
exampleCourse.ID = 1;
exampleCourse.Name = "Example course";
// Create example module 1
Module exampleModule1 = new Module();
exampleModule1.ID = 10;
exampleModule1.Name = "Example Module 1";
// Create example module 2
Module exampleModule2 = new Module();
exampleModule2.ID = 11;
exampleModule2.Name = "Example Module 2";
// add modules to the course
exampleCourse.Modules.Add(exampleModule1);
exampleCourse.Modules.Add(exampleModule2);
// add course to the courses
courses.Add(exampleCourse);
return courses;
}
}
Classes needed:
public class Course
{
public int ID { get; set; }
public string Link { get; set; }
public string Name { get; set; }
public List<Module> Modules { get; set; }
public Course()
{
this.Modules = new List<Module>();
}
}
public class Module
{
public int ID { get; set; }
public string Name { get; set; }
}
Last, in your page load, don't forget to databind:
protected void Page_Load(object sender, EventArgs e)
{
this.DataBind();
}

Image from PostBack not showing in asp.Net

i have this issue with a list of images:
in the red circle is an example with a broken link.
my issue is in the blue circle, where calling correctly my codebehind method and having the correct src associated with every image, the image it is not shown.
Any ideas please?
asp.net:
<asp:ListView ID="Screenshots" runat="server">
<LayoutTemplate>
<ul class="inner" style="display: inline;">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:Image src='<%#Eval("Source")%>' HeaderText="Image" ID="Image1" runat="server" class="col-xs-6 showgameinfo" style="width:50%"/>
</li>
</ItemTemplate>
</asp:ListView>
c# :
public class Screens
{
public Screens(string url)
{
this.url = url;
}
private string url;
public string Source
{
get { return url; }
set { url = value; }
}
}
List<Screens> ListToReturn = new List<Screens>();
foreach (FileInfo foundFile in filesInDir)
{
ListToReturn.Add(new Screens(foundFile.FullName));
}
Screenshots.DataSource = ListToReturn;
Screenshots.DataBind();

Showing Byte Image in repeater

I want to some images in asp repeater which comes from databas image table.
HTML
<asp:Repeater ID="rpt_" runat="server">
<ItemTemplate>
<li>
<img src="<%="data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Photo")) %>" alt="" />
</li>
</ItemTemplate>
</asp:Repeater>
C#
private void Load_()
{
ClassDo class_ = new ClassDo;
DataTable dt = class_.Ann().Tables[0];
rpt_.DataSource = dt;
rpt_.DataBind();
}
These are my codes, and get me error I cannot show images.
HTML
<img alt="" src="<%="data:image/jpg;base64," + Convert.ToBase64String(Class_._image) %>" />
C#
Class_._image = (byte[])dt.Rows[0]["Photo"];
It works like that somewhere else, but with repeater I cannot read it with Eval.
Is there any working way to show images?
Can you try this?
Create a function
public string GetImage(object img)
{
return "data:image/jpg;base64," + Convert.ToBase64String((byte[])img);
}
Then change your declaration like this
<asp:Image ImageUrl='<%# GetImage(Eval("Photo")) %>' />

ASPxGridView, ITemplate, and Eval

How can I create a template in c# for an AXPxGridViewDataTextColumn without any markup and with using Eval to display the DataItem value?
-The problem that I am having is that the string "<%#Eval("dataTableField1")%>" shows up in the GridView for every row instead of the appropriate values.
Here is an example of my attempt:
public override void DataBind()
{
...
GridViewDataTextColumn myCol = new GridViewDataTextColumn();
myCol.Caption = "col1";
myCol.FieldName = "dataTableField1";
myCol.DataItemTemplate = new ColumnDataItemTemplate();
theGridView.Columns.Clear();
theGridView.Columns.Add(myCol);
theGridView.DataSource = AdjustDataSource();
theGridView.DataBind();
...
}
public class ColumnDataItemTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
GridViewDataItemTemplateContainer Container = (container as GridViewDataItemTemplateContainer);
LiteralControl lit = new LiteralControl("<div id=\"hr\" style=\"height:100%\"><%#Eval(\"dataTableField1\")%></div>");
Container.Controls.Add(lit);
}
}
Here is an example of what I want to do with the row height taken from this link:
<dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="3">
<DataItemTemplate>
<div id="hr" style="height:100%">
<%#Eval("Description")%>
</div>
</DataItemTemplate>
</dx:GridViewDataTextColumn>
This documentation link shows similar examples of using templates in the markup but I want to do it in the code behind.
Here is a link on creating templates.
Thanks in advance,
Soenhay
Edit:
I was able to get the first element to properly display by moving the template assignment to the CustomColumnDisplayText event but all other elements displayed in the ASPxGridView show the Eval string.
I had 2 other solutions but I removed them as I think this is the best ( I would be grateful for any other suggestions/improvements ):
public override void DataBind()
{
...
GridViewDataTextColumn myCol = new GridViewDataTextColumn();
myCol.Caption = "col1";
myCol.FieldName = "dataTableField1";
myCol.DataItemTemplate = new ColumnDataItemTemplate();
theGridView.Columns.Clear();
theGridView.Columns.Add(myCol);
theGridView.DataSource = AdjustDataSource();
theGridView.DataBind();
...
}
public class ColumnDataItemTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
GridViewDataItemTemplateContainer Container = (container as GridViewDataItemTemplateContainer);
LiteralControl lit = new LiteralControl("<div id='hr' style='height:100%; font-size:x-large;'>" + DataBinder.Eval(Container.DataItem, Container.Column.FieldName) + "</div>");
Container.Controls.Add(lit);
}
}
At this link I have found a reason to not use the event:
"The CustomColumnDisplayText event should not be handled for template columns. "
This link helped with the DataBinder.Eval part.
Heres an example but mine is binding with a database, i usually use EditTemplte
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BSA" SortExpression="BSA">
<ItemTemplate>
<asp:Label ID="lblBSA" runat="server" Text='<%# Bind("BSA") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>

How to add 2 rows in Gridview footer

I am using a grid to display no of leads. In that I have to display Page wise total as well as Grand total. Is it possible to show it in 2 different rows in Footer?
Give me some suggestions. I have to add 8 columns in grid.
you can do this lots of ways, but one of them is to use TemplateField
here is format for your gridview (put your content in the cells)
...
<Columns>
<asp:TemplateField>
<FooterTemplate>
<table width="100%">
<tr><td><asp:Literal runat="server" ID="ltField1" Text='<%# Bind("field1") %>'></asp:Literal></td>
</tr>
<tr><td>><asp:Literal runat="server" ID="ltField2" Text='<%# Bind("field2") %>'></asp:Literal></td>
</tr>
</table>
</FooterTemplate>
...
You will have to create a custom GridView class by inheriting from the GridView type.
namespace CustomControls
{
public class CustomGridView : GridView
{
private string _pageTotal;
public string PageTotal
{
get { return _pageTotal; }
set { _pageTotal = value; }
}
private string _grandTotal;
public string GrandTotal
{
get { return _grandTotal; }
set { _grandTotal = value; }
}
public CustomGridView()
{
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.SetRenderMethodDelegate(CreateFooter);
}
base.OnRowCreated(e);
}
private void CreateFooter(HtmlTextWriter PageOutput, Control FooterContainer)
{
StringBuilder footer = new StringBuilder();
footer.Append("<td>" + this._pageTotal +"</td>");
footer.Append("</tr>");
footer.Append("<tr>");
footer.Append("<td>" + this._grandTotal + "</td>");
footer.Append("</tr>");
PageOutput.Write(footer.ToString());
}
}
}
Then use the 'Register' page directive to refer to your custom control.
<%# Register TagPrefix="cc" Namespace="CustomControls" %>
Add your control to the page, make sure ShowFooter is set to true.
<cc:CustomGridView ID="GridView1" ShowFooter="true"></cc:CustomGridView>
You can then set the 'PageTotal' and 'GrandTotal' properties.
GridView1.PageTotal = "5";
GridView1.GrandTotal = "10";

Categories