How To change attribute in html elements in asp.net - 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);

Related

use bootstrap-4-carousel in asp repeater

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.

Url Routing Asp.net

I have a blog page where blogs are fetched from database to my webpage.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="blog_post">
<div class="col-lg-3 col-md-3 col-sm-3">
<div class="">
<img src='<%# Eval("photo")%>' alt="Couple blog image" width="205px" height="150px" />
</div>
</div>
<div class="col-lg-9 col-md-9 col-sm-9">
<p class="posting_date"><%# Eval("date","{0:MMMM dd,yyyy}")%></p>
<h3><a href='ReadBlog/<%# Eval("title").ToString()%>'><%# Eval("title")%></a></h3>
<%#Eval("title") %>
<p class="blog_text">
<%# Eval("sdesc")%>
<p class="blog_text">
<a class="btn btn-danger" style="color:wheat" href='read_blog.aspx?id=<%# Eval("title")%>'>Read Post</a>
</p>
</p>
</br></br>
<hr/>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
on my Read_blog.aspx.cs page I have used...
if (!IsPostBack)
{
string myquery = Page.RouteData.Values["id"].ToString();
cls.gridbind(GridView1, "select * from tbl_blog_master where title='" + Request.QueryString["id"].ToString() + "' order by date desc");
}
I want to get the url like this
localhost/readblog/my-first-blog
help me please i am new to asp.net and have to learn a lot...
This is my first time on stackoverflow so sorry for my english..

Style the element in <HeaderTemplate> of Datalist

I am designing an ASP.NET page with using some data from SQL Server Database. I used <DataList> to call data and fill any place I want to in the page. My problem is, CSS seems not working for the element in <HeaderTemplate>. I searched some posts but I couldn't find an answer.
I tried to style <p> like you see in the code, and I tried to style the data I called with <span> too. Then I tried to use both of them at the same time as you see. None of them works.
Here is my code:
<div class="col-lg-4">
<img class="img-circle" src="../Images/icons/1.png" alt="Generic placeholder image" height="120" width="120" style="position:relative;">
<h2>Last News</h2>
<asp:DataList ID="DataList1" runat="server">
<HeaderTemplate><p style="text-align:center;"></HeaderTemplate>
<ItemTemplate><span style="text-align:center;"><%#Eval("news_header") %></span></ItemTemplate>
<FooterTemplate></p></FooterTemplate>
</asp:DataList>
<p><a class="btn btn-default" href="haberler.aspx" role="button">Devamını Oku »</a></p>
</div>
UPDATE and SOLUTION:
When I looked at the codes in browser while localhos was working, I saw the problem was table that Datalist creates. So I framed that table with div just out of Datalistcode and write the CSS class to make it work with all screen sizes. Here is the codes:
Asp.Net Side:
<div class="col-lg-4">
<img class="img-circle" src="../Images/icons/1.png" alt="Generic placeholder image" height="120" width="120" style="position:relative;">
<h2>Son Haberler</h2>
<div class="col-lg-12"><asp:DataList ID="DataList1" runat="server">
<HeaderStyle CssClass="deneme2"/>
<ItemTemplate>
<span><%#Eval("haber_baslik_tr") %></span>
</ItemTemplate>
</asp:DataList></div>
<br /><p><a class="btn btn-default" href="haberler.aspx" role="button">Devamını Oku »</a></p>
</div>
CSS Side:
.col-lg-12 table{
width:100%;
}
.col-lg-12 table tbody tr td{
text-align:center;
}
Use DataList.HeaderStyle
<HeaderStyle CssClass="MyHeaderClass">
</HeaderStyle>

asp:listbox loosing value on auto postback

I have an asp list box that is populated from the code behind
<asp:ListBox ID="UploadsApprovedCountries" EnableViewState="true" runat="server" Rows="1" AutoPostBack="True" OnSelectedIndexChanged="SelectedCountryChange"></asp:ListBox>
protected void Load_Countries()
{
ListItem ps = new ListItem("Please select", "");
ps.Value = "";
UploadsApprovedCountries.Items.Add(ps);
foreach (string cntry in Countries.CountriesArray())
{
UploadsApprovedCountries.Items.Add(new ListItem(cntry, cntry));
}
}
and in my Page_Load I call it like this
if (!IsPostBack)
{
Load_Countries();
}
and on autopostback calls this method
protected void SelectedCountryChange(object sender, EventArgs e)
{
filter = UploadsApprovedCountries.SelectedItem.Value;
Response.Write("line 589");
}
However nothing I seem to do ever actually calls this method. The filter string is never populated and "Line 589" is never wrote out.
Can anyone help out
the whole of my asp.net page
asp:Content ContentPlaceHolderID="ContentMainBody" ID="LocalMainBody" runat="server">
<h1> administration</h1>
<div class="clearboth"></div>
<div>
<div class="demo">
<div id="tabs">
<ul>
<li>Uploads awaiting approval</li>
<li>Uploads approved</li>
<li>Sign-ups awaiting approval</li>
<li>Sign-ups approved</li>
<li>Rejected</li>
</ul>
<div id="tabs-5">
<h2>Rejected submissions</h2>
<div id="rejectedSubmissions" runat="server">
</div>
</div>
<div id="tabs-1">
<h2>Uploads awaiting approval</h2>
<div id="matUnautherised" runat="server">
</div>
<div class="clearboth"></div>
</div>
<div id="tabs-3">
<h2>Uploads live on site</h2>
<asp:ListBox ID="UploadsApprovedCountries" EnableViewState="true" runat="server" Rows="1" AutoPostBack="True" OnSelectedIndexChanged="SelectedCountryChange" OnTextChanged="SelectedCountryChange"></asp:ListBox>
<div id="matAutherised" runat="server">
</div>
<div class="clearboth"></div>
</div>
<div id="tabs-2">
<h2>Sign-ups awaiting approval</h2>
<div id="signups" runat="server">
</div>
<div class="clearboth"></div>
</div>
<div id="tabs-4">
<h2>Sign-ups live on site</h2>
<div id="signupsapproved" runat="server">
</div>
</div>
</div>
</div>
</div>
I have just noticed that if I change my code in my listbox to this
<asp:ListBox ID="UploadsApprovedCountries" EnableViewState="true" runat="server" Rows="1" AutoPostBack="True" OnSelectedIndexChanged="SelectedCountryChange">
<asp:ListItem Value="test1" Text="test1"></asp:ListItem>
<asp:ListItem Value="test2" Text="test2"></asp:ListItem>
<asp:ListItem Value="test3" Text="test3"></asp:ListItem>
<asp:ListItem Value="test4" Text="test4"></asp:ListItem>
<asp:ListItem Value="test5" Text="test5"></asp:ListItem>
</asp:ListBox>
and don't populate it from the data source then it works as it should. Is there a difference between adding the items from the code behind and adding them directly like above?
simple really once you know how
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Load_Countries();
UploadsApprovedCountries.EnableViewState = true;
}

Twitter Bootstrap layout width hidden element

I'm working on an aspnet application and twitter bootstrap.
I have a situation where i need to hide a span based on a variable witch I can do correctly. The problem is that the hidden span keeps the width and breaks the next div.
<div class="row-fluid">
<div class='<%= (showTitle ? "span4" : "hide") %>'>
<label>Title</label>
<ASP:DropDownList ID="DpdLstTitle" runat="server" CssClass="span12"></ASP:DropDownList>
</div>
<div class='<%= (showTitle ? "span8" : "span12") %>'>
<label>Name</label>
<ASP:TextBox ID="TboxName" runat="server" MaxLength="80" CssClass="span12" />
</div>
</div>
Any suggestions ?
You will have to add in some css as bootstrap has added a margin to the second row
Probably:
margin-left: 2.127659574468085%;
So you'll have to override it. Insert
margin-left: 0;
Example HTML:
<div class="row-fluid">
<div class='<%= (showTitle ? "span4" : "hide") %>'>
<label>Title</label>
<ASP:DropDownList ID="DpdLstTitle" runat="server" CssClass="span12"></ASP:DropDownList>
</div>
<div class='<%= (showTitle ? "span8" : "span12 hiddenColBefore") %>'>
<label>Name</label>
<ASP:TextBox ID="TboxName" runat="server" MaxLength="80" CssClass="span12" />
</div>
</div>
Example CSS:
.hiddenColBefore {
margin-left: 0 !important;
}

Resources