ASP.NET Using Images instead HyperLinks - c#

I have a aspx page called bookscategory.aspx with this markup:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<p>Books list</p>
</HeaderTemplate>
<ItemTemplate>
<h3><asp:Literal ID="Literal1" runat="server" Text='<%# Eval("Category") %>'></asp:Literal></h3>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("Reviews") %>' ItemType="ELibraryModel.Review">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Item.Title%>' NavigateUrl='<%# "../books/bookdetails.aspx?bookId=" + Item.Id.ToString() %>'>
</asp:HyperLink><br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Code behind:
using (ELibraryEntities entities = new ELibraryEntities())
{
var allbooks = from books in entities.Books.Include("Reviews")
orderby books.Category
select new { books.Category, books.Reviews};
Repeater1.DataSource = allbooks;
Repeater1.DataBind();
}
Now on my Default.aspx page I have a book image:
<div class="prod_img">
<img src="../Images/asp.net_image.jpg" alt="" title="" border="0" /></a>
</div>
<div class="box_center">
<div class="prod_title">ASP.NET Book</div>
<p class="details">ASP.NET Book.</p>
- read more -
<div class="clear"></div>
</div>
Finally, when I click on this image I want to navigate to that exact review:
NavigateUrl='<%# "../books/bookdetails.aspx?ReviewId=" + Item.Id.ToString()
Everything works fine with asp:HyperLink-s but instead I want to use images.
I'm reading Beginning ASP.NET 4.5 in C# and VB and here you can see all reviews http://aspnet45.planetwrox.com/Reviews/All.aspx. I want to use Images instead HyperLinks.

HyperLink control already provides this capabilty - it has ImageUrl property, and when this is set, HyperLink appears as an image:
<asp:HyperLink runat="server" ID="HyperLink1"
NavigateUrl='<%# "../books/bookdetails.aspx?ReviewId=" + Item.Id.ToString() %>'
ImageUrl="~/url/to/image" />

Related

Add Repeater items to Listbox

I have a Repeater which contains Eval commands and a Button (Which I can't access) I would like to access this button and when clicked insert one of the Eval commands (Eg. Car Model) into a listbox I have on the same page.
<asp:Button ID="modelButton" CommandArgument='<%# Eval("Model") %>' Text='<%# "Add Model to List:" + Eval("Model") %>' runat="server" />
<asp:Repeater ID="Repeater2" runat="server" DataSourceID="AccessDataSource1" DataMember="DefaultView" OnItemDataBound="Repeater2_ItemDataBound">
<ItemTemplate>
<div>
<p><img src="carImages/<%#Eval("Artwork")%>" /></div>
<div class="col-xs-4">
<h4><%# Eval("Make")%> (<%# Eval("Year") %>)</h4>
<p><%# Eval("Model")%></p>
<p><%# Eval("Colour")%></p>
<p><%# Eval("Type")%></p>
<div>
<asp:Button ID="modelButton" CommandArgument='<%# Eval("Model") %>' Text='<%# "Add Model to List:" + Eval("Model") %>' runat="server" />
</div>
</a>
</div>
</ItemTemplate>
Just add your oncommand argument to the repeated button
<asp:Button ID="modelButton" CommandArgument='<%# Eval("Model") %>' OnCommand="CommandBtn_Click" Text='<%# "Add Model to List:" + Eval("Model") %>' runat="server" />
you can grab the eval in the behind code
void CommandBtn_Click(Object sender, CommandEventArgs e){
var command = e.CommandArgument;
// Do whatever with it here
}
That will get the evaluated model

handle asp.net imagebutton in listview

I have a webform that contains a listview like this:
<asp:ListView ID="SlidesListView" runat="server" OnItemCommand="SlidesListView_ItemCommand">
<LayoutTemplate>
<span runat="server" id="itemPlaceholder" style="display: inline-block" />
</LayoutTemplate>
<ItemTemplate>
<div class="SlideDiv">
<div>
<asp:ImageButton ID="ChangeDescriptionImageButton" runat="server" ImageUrl="~/Image/Change.png" CommandArgument="<%# Eval("ID") %>" CommandName="Change" AlternateText="Change" ToolTip="Change" />
<asp:Image ID="Image1" runat="server" CssClass="MiniSliderPic" ImageUrl='<%#Eval("ImagePath") %>' AlternateText='<%#Eval("Description") %>' ToolTip='<%#Eval("Description") %>' />
<p><%#Eval("Description").ToString().Replace(Environment.NewLine, "<br/>").Replace("\n", "<br />") %></p>
</div>
</div>
</span>
</ItemTemplate>
</asp:ListView>
and codebehind:
protected void SlidesListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
int ID = 0;
switch (e.CommandName)
{
case "Change":
ID = Convert.ToInt32(e.CommandArgument.ToString());
break;
}
}
but when I try to open this webform , I face this error:
An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
the server tag is not well formed
and asp:ImageButton tag get red.why?
Change this:
CommandArgument="<%# Eval("ID") %>"
To this:
CommandArgument='<%# Eval("ID") %>'
BTW, I don't think you can use imagebutton this way. It will fire it's won OnCommand rather than ListView's ItemCommand. Consider using a linkbutton with image inside it.
<asp:LinkButton ID="ChangeDescriptionLinkButton" runat="server"
CommandName="Change"
CommandArgument='<%# Eval("ID") %>' >
<img src="~/Image/Change.png" alt="Change" />Change
</asp:LinkButton>

How to to get hiddenfield value in ItemTemplate

Just want to ask how can I find hiddenfield in repeater because my problem I have button and I want to get the associate hiddenfield inside ItemTemplate because I get null value when I try to get hiddenfield value
<asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound">
<ItemTemplate>
<div class="resourcesResult">
<asp:HiddenField ID="hf_resID" runat="server" Value='<%# Eval("Id") %>' />
<a href='<%# Eval("pageID") %>'><%# Eval("name") %></a>
<br />
<asp:Literal ID="litSummary" runat="server" Text='<%# Eval("summary") %>'></asp:Literal>
<br />
<%-- <asp:Repeater ID="rp_tagsSkill" runat="server">
<ItemTemplate>
<h6>
<%# Eval("Description") %>
</h6>
</ItemTemplate>
</asp:Repeater>--%>
<asp:Repeater ID="rp_tagsTopics" runat="server">
<ItemTemplate>
<h6>
<%# Eval("Description") %>
</h6>
</ItemTemplate>
</asp:Repeater>
<div id="controls">
<asp:ImageButton ID="imgbtnBookmark" runat="server" OnClick="imgbtnBookmark_Click" />
<asp:DropDownList ID="ddlGroup" runat="server" DataSourceID="SqlDS_Groups" DataTextField="name" DataValueField="id" AppendDataBoundItems="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
<asp:ListItem Value="-1">Select Group</asp:ListItem>
protected void imgbtnBookmark_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Bookmark bm = new Bookmark();
HiddenField hiddenField = rptGroup.FindControl("hf_resID") as HiddenField;
bm.UserID =
Guid.Parse(Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString());
bm.Resoursce.ResourceID = Convert.ToInt32(hiddenField.Value);
Bookmark.Insert(bm);
}
Try this, change you button tag to pass id as CommandArgument value
<asp:ImageButton ID="imgbtnBookmark" runat="server"
OnClick="imgbtnBookmark_Click"
CommandArgument='<%# Eval("Id") %>'/>
in your button click event you can access id using
bm.Resoursce.ResourceID = Convert.ToInt32(e.CommandArgument.ToString());
Change rptGroup.FindControl("hf_resID") as HiddenField; to
e.Item.FindControl("hf_resID")....
I hope this link will help you .
jquery
function showid(dllval) {
var ID = $(dllval).parent().parent().find('[id*="hiddenID"]').val();
alert(ID)
}
asp.net
<asp:DropDownList ID="ddl" runat="server" onclick="showid(this);" >
</asp:DropDownList>
<asp:HiddenField ID="hiddenID" runat="server" Value='<%#Eval("ID")%>' />

Add class to last item [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
change repeater li item class if first or last
Im working on a .net website which I've never worked with before.
I have the following that outputs 4 divs...
<asp:Repeater ID="RT_Ranges" runat="server">
<ItemTemplate>
<div class="item">
<h4>
<%# Eval("Category_Name")%></h4>
<asp:Image ID="Image1" ImageUrl='<%# Eval("Img") %>' runat="server" Height="92" Width="79" />
<p>
<%# Eval("des") %></p>
<asp:HyperLink ID="HL_More" NavigateUrl='<%# Eval("nav") %>' runat="server">View More</asp:HyperLink>
</div>
</ItemTemplate>
</asp:Repeater>
I want to add a class to the last DIV outputted from the above, is this possible and if so how would I do it?
You have your answer here
Make it like:
<asp:Repeater ID="RT_Ranges" runat="server">
<ItemTemplate>
<div class="<%# GetItemClass(Container.ItemIndex) %>">
<h4>
<%# Eval("Category_Name")%></h4>
<asp:Image ID="Image1" ImageUrl='<%# Eval("Img") %>' runat="server" Height="92" Width="79" />
<p>
<%# Eval("des") %></p>
<asp:HyperLink ID="HL_More" NavigateUrl='<%# Eval("nav") %>' runat="server">View More</asp:HyperLink>
</div>
</ItemTemplate>
</asp:Repeater>
and on your code-behind:
protected string GetItemClass(int itemIndex)
{
if (itemIndex == this.ItemCount - 1)
return "CSS_for_last_item";
}
You can do it on the server:
in code behind after data is bound
or on the client:
using pure CSS (see :last-child selector)
using script (see jQuery :last-child selector)
You can't do it. At least this is not easy.
If you need this class for styling reasons then use the :last-child pseudoclass instead and if you need this in javascript then there are other methods of getting the last child.
You could use a Panel instead (which is rendered as div). Then you can use this code to get the last item and find the panel:
RepeaterItem lastRepeaterItem = RT_Ranges.Items[RT_Ranges.Items.Count - 1];
Panel lastPanel = (Panel)lastRepeaterItem.FindControl("PanelID");
lastPanel.CssClass = "yourclass";
Change class attribute value as follows,
<div class="<%# GetItemClass(Container.ItemIndex) %>">
And create GetItemClass method in code behid,
protected string GetItemClass(int itemIndex)
{
if (itemIndex == this.ItemCount - 1)
return "last";
else
return "other";
}
Also refer
Change repeater li item class if first or last
<asp:Repeater ID="RT_Ranges" runat="server">
<ItemTemplate>
<div <%# (Container.DataItemIndex != 0 && (Container.DataItemIndex+1) % 4 == 0) ? #"class='myClass'" : #"class='item'" %>>
<h4>
<%# Eval("Category_Name")%></h4>
<asp:Image ID="Image1" ImageUrl='<%# Eval("Img") %>' runat="server" Height="92" Width="79" />
<p>
<%# Eval("des") %></p>
<asp:HyperLink ID="HL_More" NavigateUrl='<%# Eval("nav") %>' runat="server">View More</asp:HyperLink>
</div>
</ItemTemplate>
</asp:Repeater>
Or using jQuery:
$("table.RT_Ranges div:last").attr("class", "last");

Pass value back to server in asp.net?

In an asp.net application, I have the follow code in the aspx page:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% foreach (var questionPaper in QuestionPapers) { %>
<div style="border-bottom: 1px solid; padding-bottom:20px">
<%= questionPaper.University %><br/>
<%= questionPaper.CourseName %>: <%= questionPaper.CourseCode %><br/>
<%= questionPaper.Type %><br/>
<%= questionPaper.Year %><br/>
<asp:Button ID="View_Paper" runat="server" OnClick="ViewPaper" Text="View Paper"/>
</div>
<% } %>
</asp:Content>
I want to pass questionPaper.ID back to the ViewPaper() event handler on the server side, how do I do that?
public void ViewPaper(object sender, EventArgs e)
{
}
My recommendation is to use a Repeater or a ListView control
Use a Repeater if the data will be used as read-only
Use a ListView if you want to page your results and/or allow the end user to perform CRUD operation over your data
For example, your code would look like this when using a Repeater and an ObjectDataSource controls:
ASPX
<asp:ObjectDataSource ID="ods" runat="server"
SelectMethod="GetQuestionPapers"
TypeName="Your_Namespace.PapersRepository">
</asp:ObjectDataSource>
<asp:Repeater runat="server" DataSourceID="ods" ID="r" OnItemCommand="r_ItemCommand">
<ItemTemplate>
<div style="border-bottom: 1px solid; padding-bottom:20px">
<asp:HiddenField runat="server" ID="paperID" Value='<%# Eval("PaperID") %>'/>
<asp:Label runat="server" ID="university" Text='<%# Eval("University") %>'/><br />
<asp:Label runat="server" ID="courseName" Text='<%# Eval("CourseName") %>'/>:<asp:Label runat="server" ID="courseCode " Text='<%# Eval("CourseCode ") %>'/><br />
<asp:Label runat="server" ID="paperType" Text='<%# Eval("Type") %>' /><br />
<asp:Label runat="server" ID="year" Text='<%# Eval("Year") %>' /><br />
<asp:Button ID="View_Paper" runat="server" Text="View Paper" CommandName="ViewPaperCommand"
/>
</div>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
ASPX code behind
protected void r_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "ViewPaperCommand":
var hidden = e.Item.FindControl("paperID") as HiddenField;
var myPaperID = hidden.Value;
break;
}
}
Papers repository class
namespace Your_Namespace
{
public class PapersRepository
{
public IEnumerable < QuestionPaper > GetQuestionPapers()
{
return QuestionPapers.AsEnumerable();
}
}
}
You could bind it to a control and then reference that control directly?
I know you have multiple questions papers being output, so that in itself is the issue. It's been a while since I have used webforms, but could you not use a repeater control which would give you a row context within which you could get the specific question paper id you are looking for?
Use a repeater. That kind of old skool ASP classic code has no place in ASP.Net projects. Here's an example: http://www.dotnetcurry.com/ShowArticle.aspx?ID=663
It is possible to customise a repeater to make a lightweight DataGrid or ListView equivalent too.

Categories

Resources