Url Routing Asp.net - 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..

Related

Bootstrap3 align asp.net textboxes side by side to the asp.net RadioButtonList specific list items

I am trying to align asp.net text boxes to the asp.net RadioButtonList specific list items using bootstrap 3. I have tried Align textbox to right of radiobuttonlist listitem using that and achieved this
but, I am getting text boxes very far from the RadioButtonList instead of side by side close to the radio button list items. Experts, please help me where I am going wrong.
Here is my code so far,
<div class="col-xs-12">
<asp:Label ID="Label1" runat="server" CssClass="forQuestions">What language do you feel MOST comfortable speaking?</asp:Label>
<div class="form-group col-xs-12">
<div class="radio radiobuttonlist col-xs-6">
<asp:RadioButtonList ID="RadioButtonList2" runat="server" CssClass="forOptions">
<asp:ListItem>English is my first language</asp:ListItem>
<asp:ListItem>French is my first language</asp:ListItem>
<asp:ListItem>I am comfortable with both English and Frech</asp:ListItem>
<asp:ListItem>I am comfortable with both English and Another Language, please specify</asp:ListItem>
<asp:ListItem>Other, please specify</asp:ListItem>
</asp:RadioButtonList>
</div>
<div class="col-xs-12 col-xs-6">
</div>
<div class="col-xs-12 col-xs-6">
</div>
<div class="col-xs-6 col-xs-3">
<asp:TextBox ID="TextBox3" runat="server" MaxLength="499" Text="choice4" Visible="true" Width="400" class="form-control input-xs">
</asp:TextBox>
</div>
<div class="col-xs-12 col-xs-6">
<asp:TextBox ID="TextBox4" runat="server" MaxLength="499" Text="choice5" Visible="true" Width="400" class="form-control input-xs">
</asp:TextBox>
</div>
</div>
</div>

get selected values in repeater control

i have a repeater control which shows the list of all available products to show to a customer. this repeater control might have 5 items each, and in each of the itemtemplate in the repeater i have a button which is add to cart. once the user clicks this button i want to get the "ID" of that particular product through VB. please note that sometimes in the aspx page i can easily pass the id to a href="" redirect like this
Dim productid As String = DirectCast(Repeatershowsproducts.Controls(0).FindControl("ID"), Label).Text
it always return me the 1st product ID in the list and i wanted it to select the particular one the user chose after clicking the add to cart button. the user my have clicked the Third product meanwhile its showing the ID of the first product
Heres my aspx code.
<asp:Repeater ID="Repeatershowproducts" runat="server">
<ItemTemplate>
<div class="prtm-block-content bg-primary overflow-wrapper">
<div style="margin-top: 15px;" class="left col-xs-3">
<img class="img-responsive img-circle display-ib" src="<%#Eval("productimgpath")%>" alt="Avatar" title="Sponsor Profile Photo">
</div>
<div style="margin-top: 15px;" class="right col-xs-9">
<p class="font-sm"><i class="fa fa-user"></i>FullName: <span class="font-sm mrgn-b-md fw-bold"><%#Eval("productfullname")%></span></p>
<p class="font-sm"><i class="fa fa-bitcoin"></i>Amount: <span style="word-wrap: break-word;" class="font-sm mrgn-b-md fw-bold"><%#Eval("productprice")%> BTC</span></p>
<p class="font-sm"><i class="fa fa-phone"></i>deleivery days: <span class="font-sm mrgn-b-md fw-bold"><%#Eval("productdelivery")%></span></p>
<p class="font-sm"><i class="fa fa-globe"></i>Product location: <span class="font-sm mrgn-b-md fw-bold"><%#Eval("productcountry")%></span></p>
<p class="font-sm">Status: <span class="font-sm mrgn-b-md fw-bold"><%#Eval("Status")%></span></p>
<asp:Label ID="lblproductid" runat="server" Text='<%#Eval("ID")%>'></asp:Label>
</div>
<center>
<div class="text-center center">
<asp:Button ID="Button1" OnClick="addtocart" CssClass="btn btn-lg fw-bold btn-primary btn-rounded" runat="server" Text="Add product to cart" />
</div>
</div>
</center>
</div>
<hr />
</ItemTemplate>
</asp:Repeater>
The easiest way is to change the button from a OnClick to a OnCommand and add a CommandArgument to the button.
<asp:Button ID="Button1" OnCommand="Button1_Command"
CommandArgument='<%# Eval("itemid") %>' runat="server" Text="Add product to cart" />
And then read that value in code behind.
Protected Sub Button1_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
Dim ProductID As Integer = Convert.ToInt32(e.CommandArgument)
End Sub

My ListView is raising events that don't exist

So I have a listview:
<asp:listview runat="server" ID="listview_search" itemPlaceholderID="itemListing" DataSourceID="ds_search">
<LayoutTemplate>
<div id="sort" runat="server">
<asp:LinkButton runat="server" id="sortByPrice" CommandName="sort" CommandArgument="price" >Sort by price</asp:LinkButton>
</div>
<div runat="server" id="itemListing"></div>
</LayoutTemplate>
<ItemTemplate>
<div class="container profilelistings">
<div class="row carousel-row">
<div class="col-xs-8 col-xs-offset-2 slide-row" style="min-height: 100px;">
<div id="carousel-1" class="carousel slide slide-carousel">
<div class="carousel-inner">
<div class="item active">
<asp:Image id="img_car" imageurl='<%# "images/" + Eval("ownerID") + "/" + Eval("photo1") %>' runat="server"></asp:Image>
</div>
</div>
</div>
<div class="slide-content">
<h5><%# Eval("make").ToString() + " " + Eval("model") + " (" + Eval("type") + ") " %></h5>
<h5><span class="price"><%# "£ " + Eval("price") %></span></h5>
</div>
<div class="slide-footer">
<span class="pull-right buttons">
<button class="btn btn-sm btn-default"><i class="glyphicon glyphicon-eye-open"></i> View more info </button>
</span>
</div>
</div>
</div>
</div>
</ItemTemplate>
</asp:listview>
When I click the sortByPrice LinkButton it gives an error "The ListView 'listview_search' raised event Sorting which wasn't handled." When the event does not exist in the ListView itself nor in the code.
Does anyone understand why it is doing this?

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>

Resources