use bootstrap-4-carousel in asp repeater - asp.net

I try to use bootstrap-4-carousel as the example in the link
bootstrap-4-carousel in asp: repeater so the image will bring form database. I try to select the image in normal function as follow
SqlDataAdapter da;
da = new SqlDataAdapter(#"select * from ImageTable
order by NEWID()", constr);
DataTable dt = new DataTable();
da.Fill(dt);
rptImages.DataSource = dt;
rptImages.DataBind();
and also with paging like follow:
public int CurrentPageEN
{
get
{
if (this.ViewState["CurrentPageEN"] == null)
return 0;
else
return Convert.ToInt16(this.ViewState["CurrentPageEN"].ToString());
}
set { this.ViewState["CurrentPageEN"] = value; }
}
private void doPagingEN()
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndexEN");
dt.Columns.Add("PageTextEN");
for (int i = 0; i < pdsEN.PageCount; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}
dlPagingEN.DataSource = dt;
dlPagingEN.DataBind();
}
protected void BindDataEN()
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd;
con.Open();
cmd = new SqlCommand(#"select * from imageTable order by NEWID()", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
pdsEN.DataSource = dt.DefaultView;
pdsEN.AllowPaging = true;
pdsEN.PageSize = 4;
pdsEN.CurrentPageIndex = CurrentPageEN;
// Repeater1.DataSource = pdsEN;
// Repeater1.DataBind();
doPagingEN();
}
protected void dlPaging_ItemCommandEN(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("lnkbtENagingEN"))
{
CurrentPageEN = Convert.ToInt16(e.CommandArgument.ToString());
BindDataEN();
}
}
and this is aspx code
<style>
.blog .carousel-indicators {
left: 0;
top: auto;
bottom: -40px;
}
/* The colour of the indicators */
.blog .carousel-indicators li {
background: #a3a3a3;
border-radius: 50%;
width: 8px;
height: 8px;
}
.blog .carousel-indicators .active {
background: #707070;
}
</style>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row blog">
<div class="col-md-12">
<div id="blogCarousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<asp:Repeater ID="dlPagingEN" runat="server">
<ItemTemplate>
<li data-target="#blogCarousel" data-slide-to='<%#Eval("PageTextEN") %>'>
<asp:LinkButton ID="lnkbtENagingEN" runat="server" CommandName="lnkbtENagingEN" CommandArgument='<%#Eval("PageIndexEN")%>'></asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
</ol>
<!-- Carousel items -->
<div class="carousel-inner">
<div class="carousel-item active">
<div class="row">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="col-md-3">
<a href="#">
<img src='<%#Eval("ImagePaths")%>' alt="Image" style="max-width: 100%;">
</a>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<!--.row-->
</div>
</div>
<!--.carousel-inner-->
</div>
<!--.Carousel-->
</div>
</div>
</div>
but whatever I did the slider not work as the example in the link.
So please if anyone has a solution help me.
Thanks for everyone

I have found an asp.net and Bootstrap carousal solution
The trick is for the "IF" to identify the first index as "ACTIVE" then the others are added without the "ACTIVE"
<div id="carousel" class="carousel slide" data-ride="carousel">
<div class=" container carousel-inner">
<asp:Repeater ID="rpt_Propiedades" runat="server">
<ItemTemplate>
<div class="carousel-item <%# (If(Container.ItemIndex = 0, "active", ""))%>">
<%# Eval("Nombre Columna") %>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</div>

Basically what's happening is that you have one "slide" for the carousel, and are putting all the images into that one slide.
Now, the link provided at the top of the question has 3 small images per "slide" of the carousel. That's a bit more complicated in regards to the bound control, so let's step back and just get 1 image per slide to start.
To get 1 image, per slide, you'd want to modify the repeater aspx to work like this:
<div class="carousel-inner">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="carousel-item">
<a href="#">
<img src='<%#Eval("ImagePaths")%>' alt="Image" style="max-width: 100%;">
</a>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
The point there is that each slide is the div with the class of "carousel-item." By including it inside the ItemTemplate of the repeater that means that every image would be a single unique slide.
Now, for the dlPagingEn repeater, you will want to get rid of the linkbutton that's embedded in it. The concept is that all images for the slides should be included on the page, and the dlPagingEn control would allow for client-side access to the various slides. You shouldn't need to perform a round-trip to the server for this behavior.
Now for the bad news. Getting that Repeater to spit out the code for 3 images per slide like in the provided link is significantly harder. Probably the easiest way, and which I've alluded to in the repeater code shown below, is to include a function in the ItemTemplate. What that function needs to do is to keep track of the data item record number and for every 3rd image close the row and carousel-item divs and then re-open them.
<div class="carousel-inner">
<asp:Repeater ID="Repeater1" runat="server">
<headerTemplate>
<div class="carousel-item">
<div class="row">
</headerTemplate>
<ItemTemplate>
<%# BuildCarouselItemSeperator() %>
<div class="col-md-3">
<a href="#">
<img src='<%#Eval("ImagePaths")%>' alt="Image" style="max-width: 100%;">
</a>
</div>
</ItemTemplate>
<footerTemplate>
</div>
</div>
</footerTemplate>
</asp:Repeater>
</div>
Now, if you can modify the SQL (either the query or the table itself), so that each row of the returned dataset that you're binding to the repeater has 3 distinct image records instead of one, this does become a lot easier. In that case, you would use the below style of repeater declaration:
<div class="carousel-inner">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="carousel-item">
<div class="row">
<div class="col-md-3">
<a href="#">
<img src='<%#Eval("ImagePaths_A")%>' alt="Image" style="max-width: 100%;">
</a>
</div>
<div class="col-md-3">
<a href="#">
<img src='<%#Eval("ImagePaths_B")%>' alt="Image" style="max-width: 100%;">
</a>
</div>
<div class="col-md-3">
<a href="#">
<img src='<%#Eval("ImagePaths_C")%>' alt="Image" style="max-width: 100%;">
</a>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
In truth, for getting the 3 images per slide, you may actually have a bit more luck building the HTML using StringBuilder in the code-behind and then putting that into a literal control than attempting to do it using the Repeater.

Related

Trying to find a way to dynamically add items to a check list box

I have a check box list that I'm populating with values from a table in my database. I've added the ability for users to add items to the check box list through a modal popup:
<section class="submodal submodalWindow" id="popupFour" style="margin-top: -142px; height: 388px; width: 400px; border: solid; margin-left: -627px; top: 42%; left: 68%">
<section class="submodalWrapper" style="height: 342px">
<h1 class="h3 mb-0 text-gray-800">Add a Document</h1>
<hr />
<div class="card pmd-card">
<div class="card-body">
<!-- Basic Information -->
<div class="details-tab">
<div style="margin-bottom:10px">
<label class="pmd-list-subtitle">Document</label>
<asp:TextBox class="form-control" runat="server" ID="tbDocument"></asp:TextBox>
</div>
<asp:UpdatePanel runat="server" ID="UpdatePanel33">
<ContentTemplate>
<asp:LinkButton runat="server" ID="btnPacketAdd" OnClick="btnPacketAdd_Click" class="d-none d-sm-block btn btn-sm btn-success shadow-sm"><i class="fas fa-plus-circle fa-sm text-white-50" style="padding-right:10px"></i>Add</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:LinkButton ID="LinkButton6" runat="server" class="d-none d-sm-inline-block btn btn-lg btn-success shadow-sm modalButton" Style="margin-left:95px; margin-top:20px"><i class="fas fa-backward fa-sm text-white-50"></i>Back</asp:LinkButton>
</div>
</div>
</section>
<a class="subcloseBtn">CLOSE X</a>
</section>
My issue is that when I try to add a new item through the modal popup, it does not update the checkbox list right away. I would have to close the application and run it again for the newly added items to appear. I've already set AutoPostBack=True as per some posts online, however, I may be missing something here as this is my first time doing this. The code below is how I'm storing the items to my table:
Protected Sub btnPacketAdd_Click(sender As Object, e As EventArgs) Handles btnPacketAdd.Click
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HRRecruitmentDBConn").ToString())
' Create a command object.
Dim cmd As New SqlCommand()
' Assign the connection to the command.
cmd.Connection = conn
cmd.CommandText = "INSERT INTO tblCredentialingPacket (Packet_Item, Status) VALUES (#Packet_Item, #Status)"
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("#Status", 1)
cmd.Parameters.Add("#Packet_Item", SqlDbType.NVarChar, 150).Value = tbDocument.Text
conn.Open()
cmd.Connection = conn
cmd.ExecuteNonQuery()
End Using
End Sub
I figured it out. I created a function called BindCheckBoxList() which binds the datasource to my checkboxlist so whenever I add a new item from the modal popup it automatically updates the checkbox list.

How to apply dynamic class in repeater?

I'm trying to create a repeater which contains elements with a dynamic class, as well as dynamic background color, and icons (which is put at run-time).
I have a repeater
<asp:Repeater ID="repdashboard" runat="server">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<div class="col-lg-3 col-md-6">
<div if("<%#Eval("id") %>"= 1 ? " class=panel-primary )>
<div class="panel-heading">
<div class="row">
<div class="col-xs-3">
<i class="fa fa-comments fa-5x"></i>
</div>
<div class="col-xs-9 text-right">
<div class="huge">26</div>
<div>'<%#Eval("Tag") %>'!</div>
</div>
</div>
</div>
<a href="#">
<div class="panel-footer" style="color: #337ab7;">
<span class="pull-left">View Details</span>
<span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
<div class="clearfix"></div>
</div>
</a>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
I want to apply class on behalf of dynamic value in div
<div if("<%#Eval("id") %>"= 1 ? " class=panel-primary )>
when
`<%#Eval("id") %> = 1`
then apply class= panel panel-primary
and when
<%#Eval("id") %> = 2 then apply class=panel panel-green
You cannot just write an if somewhere in the html and expect it to work. You need a code block.
<div class="<%# Convert.ToInt32(Eval("id")) == 1 ? "panel panel-primary" : "panel panel-green" %>">
If you want a lot more evaluations to generate a class, I would recommend a method in code behind.
public string AddClass(int id)
{
if (id == 1)
return "panel panel-primary";
else if (id == 2)
return "panel panel-green";
else
return "panel other";
}
And then in the Repeater
<div class="<%# AddClass(Convert.ToInt32(Eval("id"))) %>">

How To change attribute in html elements in asp.net

this is my asp codes in my page
<asp:GridView BorderStyle="None" BorderColor="White" BorderWidth="0px" CellPadding="0" AlternatingRowStyle-BackColor="" ID="grdProducts" Width="100%" runat="server" AutoGenerateColumns="False" ShowHeader="False" AllowPaging="True" DataSourceID="sqldsProducts">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<!--begin col-lg-6 col-md-6 -->
<li class="list_item col-lg-12 col-md-12 portfolio">
<div class="recent-item">
<figure class="portfolio_1">
<div class="bwWrapper touching medium">
<asp:Image ID="Image1" ImageUrl='<%# Eval("PImg") %>' runat="server" />
</i>
</div>
<figcaption class="item-description">
<h5 id="PTitle"><%# Eval("PTitle") %></h5>
<p id="PDesc"><%# Eval("PDesc") %></p>
<div class="go_link">
Read More
</div>
</figcaption>
</figure>
</div>
</li>
<!--end col-lg-6 col-md-6 -->
<div style="height:20px;"></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<pagerstyle cssclass="pagination" HorizontalAlign="Center" />
</asp:GridView>
for example i want to add some css attributes for this code
h5 id="PTitle"<%# Eval("PTitle") %>/h5 but i dont know how to find this id and add attributes
You can do this using jQuery. Make sure you have jQuery embedded in your page.
Then you can access PTitle element using jQuery selector and assign CSS to it as follows
$( document ).ready(function() {
$("#PTitle").css('background-color','#ff0000');
});
EDIT:
If you have ASPX page
<h5 id="PTitle" runat="server">Test</h5>
Then Code Behind:
PTitle.Style.Add("display", "none");
Depends on when you want to add the Styles. Here are few options.
If you want to add it in design time just add a style attribute as you would for other html elements.
<h5 id="PTitle" style="font-size:12px"><%# Eval("PTitle") %></h5>
If you want to do it from code behind (server side) you need to add the runat="server" attribute to the element first. In this case you won't add the style attribute in design time.
<h5 id="PTitle" runat="server"><%# Eval("PTitle") %></h5>
Then you can access this control in code behind using it's id as follows.
for (int i = 0; i < grdProducts.Rows.Count; i++)
{
HtmlGenericControl PTitle = (HtmlGenericControl)grdProducts.Rows[i].FindControl("PTitle");
if (PTitle != null)
{
PTitle.Style.Add("font-size", "12px");
}
}
Or, you could use the Attributes property as follows.
for (int i = 0; i < grdProducts.Rows.Count; i++)
{
HtmlGenericControl PTitle = (HtmlGenericControl)grdProducts.Rows[i].FindControl("PTitle");
if (PTitle != null)
{
PTitle.Attributes.Add("style", "font-size:12px");
}
}
If you need to do this in client side make use you JavaScript/ JQuery.
Okay here's all I needed to solve my problem:
LiteralControl litc=new LiteralControl();
litc.Text = "<style type='text/css'> #PTitle{text-align:right} #PDesc{text-align:right} </style>";
this.Page.Header.Controls.Add(litc);

Creating asp buttons with different text depending on value of a field in repeater

For my .NET project I have to output data from an access file to a repeater and generate buttons next to each item in order for customers to rent the items displayed. I've been trying to find a way to generate buttons with different text depending on the value of a field in the repeater. More specifically, I'd like the button to say "rent for £3.50" if price_band is A and "rent for £2.50" if price_band is B. Up to now I've tried several approaches to no avail. Any help would be appreciated, Thanks!
<ItemTemplate>
<div class="list-group-item active">
<div class="row">
<div class="col-md-3">
<p class="list-group-item-text"><img src="<%# Eval("image")%>" /></p>
</div>
<div class="col-md-4">
<h4 class="list-group-item-heading"><%# Eval("title")%></h4>
<p class="list-group-item-text"><strong>Director: </strong><%# Eval("director")%></p>
<p class="list-group-item-text"><strong>Cast: </strong><%# Eval("cast")%></p>
<p class="list-group-item-text"><strong>Genre: </strong><%# Eval("genre")%></p>
<p class="list-group-item-text"><strong>Year Released: </strong><%# Eval("year_released")%></p>
<p class="list-group-item-text"><strong>Rating: </strong><%# Eval("rating")%></p>
<p class="list-group-item-text"><strong>IMDB Score: </strong><%# Eval("imdb_rating")%></p>
<p class="list-group-item-text"><strong>Price Band: </strong><%# Eval("price_band")%></p>
</div>
<div class="col-md-1">
<% if (Session["customer"] != null) { %>
<%if("price_band" == "A"){%>
<asp:Button ID="btnRent" runat="server" Text="Rent for £3.50"/>
<% } %>
<%else if("price_band" == "B"){%>
<asp:Button ID="btnRent1" runat="server" Text="Rent for £2.50"/>
<% } %>
<% }
else { %>
<asp:Button ID="btnLoginWarning" runat="server" Text="Must be logged in to rent"/>
<%} %>
</div>
</div>
</div>
</ItemTemplate>

ASP.NET make a panel visible on click of hyperlink (whilst also cuasing postback for page navigation)

I may be asking the impossible but let me set out my problem:
I have a menu in a MasterPage which uses images and mouseover mouseout events for design purposes.
On one of the menu options I need to display a set of sub menus options on the click of the parent menu item. The menu item itself also needs to navigate to a specified url.
I was originally trying to use an AJAX accordion panel but as I only had one accordion panel it was always displaying the sub menu items and was not collapsing.
I have also tried putting the options in a div and setting the display via javascript. This worked but then was overwritten once the page navigation postback occurred.
Here is the source:
<%# Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%# Register Src="LeadHeader.ascx" TagName="LeadHeader" TagPrefix="uc1" %>
<%# Register Src="~/LeadFooter.ascx" TagName="LeadFooter" TagPrefix="uc2" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var revert = new Array();
var inames = new Array('home', 'whoweare', 'whatwedo','ourapproach', 'ourvalues', 'contact');
// Preload
if (document.images) {
var flipped = new Array();
for(i=0; i< inames.length; i++) {
flipped[i] = new Image();
flipped[i].src = "images/"+inames[i]+"2.jpg";
}
}
function over(num) {
if(document.images) {
revert[num] = document.images[inames[num]].src;
document.images[inames[num]].src = flipped[num].src;
}
}
function out(num) {
if(document.images) document.images[inames[num]].src = revert[num];
}
function ShowHide(elementId)
{
var element = document.getElementById(elementId);
if(element.style.display != "block")
{
element.style.display = "block";
}
else
{
element.style.display = "none";
}
}
function UpdateText(element)
{
if(element.innerHTML.indexOf("Show") != -1)
{
element.innerHTML = "Hide Details";
}
else
{
element.innerHTML = "Show Details";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
<uc1:LeadHeader ID="LeadHeader" runat="server" />
</asp:ContentPlaceHolder>
<div id="nav">
<div class="menu-item">
<a href="Default.aspx">
<img src="Images/home.jpg" alt="home" id="home" onmouseover="over(0)" onmouseout="out(0)"
class="right" /></a>
</div>
<div class="menu-item">
<a href="AboutUs.aspx">
<img src="Images/whoweare.jpg" alt="who we are" id="whoweare" onmouseover="over(1)"
onmouseout="out(1)" class="right" /></a>
</div>
<%-- <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:Accordion ID="Accordion1" runat="server" AutoSize="None" FadeTransitions="true"
TransitionDuration="350" FramesPerSecond="40" RequireOpenedPane="false" >
<Panes>
<cc1:AccordionPane runat="server">
<Header>--%>
<div class="menu-item">
<a href="WhatWeDo.aspx">
<img src="Images/whatwedo.jpg" alt="what we do" id="whatwedo" onmouseover="over(2)"
onmouseout="out(2)" class="right" onclick="ShowHide('divDetails');UpdateText(this);" /></a></div>
<%--/Header>
<Content>--%>
<div id="divDetails" style="display:none;">
Management Development<br />
Leadership Development<br />
Personal Development<br />
Team Building & Facilitation<br />
One to One Coaching
</div>
<%-- </Content>
</cc1:AccordionPane>
</Panes>
</cc1:Accordion>
--%>
<div class="menu-item">
<a href="OurApproach.aspx">
<img src="images/ourapproach.jpg" alt="our approach" id="ourapproach" onmouseover="over(3)"
onmouseout="out(3)" /></a>
</div>
<div class="menu-item">
<a href="OurValues.aspx">
<img src="images/ourvalues.jpg" alt="our values" id="ourvalues" onmouseover="over(4)"
onmouseout="out(4)" /></a>
</div>
<div class="menu-item">
<a href="ContactUs.aspx">
<img src="images/ContactUs.jpg" alt="contact us" id="contactus" onmouseover="over(5)"
onmouseout="out(5)" /></a>
</div>
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server">
<uc2:LeadFooter ID="LeadFooter" runat="server" />
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
If I read this correctly you need the nav buttons to direct you to another page and open a sub-menu to display further options.
The easiest way to do this would be to create the entire sub menu on the master page and hide it in your global style sheet. Each page could then contain a line of css to show the appropriate panel no JavaScript required.
A more efficient way would be to use this.Master.Page.FindControl("myPanelId") to manupulate the necessary item server-side.
I can't visualize exactly what you are doing but I have used jQuery accordion menus to do something similary. My server side code built a nested unordered list with links and the appropriate images. I let the hover event switch the accordion panel so that I could still click on any of the links to go to the corresponding page. If this is closer to what you want I can give you a general idea of the code / css necessary.
Hope that helps.

Resources