Repeater Control in ASP.NET - asp.net

Please help me to display images from database to webpage. This is my source code, but it shows error creating repeater control
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" OnItemCommand ="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Image ID="myImage" ImageUrl='<%# "~/ImageHandler.ashx?BImID="+Eval("img_id") %>' runat="server" alt=" " ;style="height:200px;width:200px;border:1px solid gray"/><asp:Literal ID="litSeparator" runat="server"></asp:Literal>
</ItemTemplate>
</asp:Repeater>

I think the problem is in this "ImageUrl='<%# "~/ImageHandler.ashx?BImID="+Eval("img_id") %>'".
Try to reorganize yor code like this:
ImageUrl='<%# GetImageUrl(Eval("img_id")) %>'
and then in code behind define the method GetImageUrl like this:
protected string GetImageUrl(object id)
{
return "~/ImageHandler.ashx?BImID=" + id;
}
Regards,
Uroš

Related

Passing CommandArguement to LinkButton from variable

I have seen many resources on SO that say that I can use following syntax to pass value to CommandArguement of `LinkButton'
<%forearch(var comment in Comments){%>
<asp:LinkButton ID="del" CommandArguement='<%= comment.CommentId%>' onCommand="delete_click" Text="Delete"/>
<%}%>
But when I write this in my ascx file and click on the link the value passed to command argument is "<%=comment.CommentId%>" instead of commentId itself. Please guide what am I doing wrong?
Edit 1
based on answers and comments, I have moved to use repeater instead of foreach and plain code. Here is the code I have come up with
<asp:Repeater ID="commRepeater" SelectMethod="GetPageComments" runat="server">
<ItemTemplate>
<p>
<%#Eval("Comment") %>
<%if(Page.User.Identity.IsAuthenticated && Page.User.Identity.GetUserId() == Eval("UserId")){ %>
<span>
<asp:LinkButton Text="Edit" runat="server" ID="EditLink" CommandArgument='<%#Eval("CommentId")%>' OnClick="Update_Comment" />
<asp:LinkButton Text="Delete" runat="server" ID="DeleteLink" CommandArgument='<%#Eval("CommentId")%>' OnClientClick="if (!confirm('Are you sure you want delete?')) return false;" OnCommand="Delete_Comment" />
</span>
<%} %>
</p>
</ItemTemplate> </asp:Repeater>
you can see that I am trying to show the edit and delete links if user is logged in and his Id matches with user who commented but it tells me that I can on use Eval in databound controls. how would I hide/show edit/delete links conditionally within repeater
You could simply use codebehind, for example in Page_Load:
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
del.CommandArgument = comment.CommentId;
}
}
Maybe a better approach would be to use the Comments-collection(which seems to be a list or array of a custom class) as DataSource of a Repeater(or other web-databound control). Then you can add the LinkButtons to the Itemtemplate.
You can then either use ItemCreated or ItemDataBound events of the repeater in codebehind or inline ASP.NET tags to bind the CommandArgument.
For example:
CommandArguement='<%# DataBinder.Eval( Container.DataItem, "CommentId" ) %>'
What you are doing currently is not recommended and is highly error prone. You can easily achieve this with ASP.NET Repeater control like this:-
<asp:Repeater ID="MyRepeater" runat="server">
<ItemTemplate>
<asp:LinkButton ID="del" CommandArguement='<%# Eval("CommentId") %>'
OnCommand="del_Command" Text="Delete" runat="server" />
</ItemTemplate>
</asp:Repeater>
In Page_Load simply bind it:-
if (!Page.IsPostBack)
{
MyRepeater.DataSource = CommentsRepository();
MyRepeater.DataBind();
}
Or Else if you are have ASP.NET 4.5 then use strongly type Data Bound controls like this:-
<asp:Repeater ID="MyRepeater" runat="server" ItemType="MyNamespace.Comment"
SelectMethod="MyRepeater_GetData">
<ItemTemplate>
<asp:LinkButton ID="del" CommandArguement='<%# Item.CommentId %>'
OnCommand="del_Command" Text="Delete" runat="server" />
</ItemTemplate>
</asp:Repeater>
And you method in code behind should be something like this(just for Demo):-
public IEnumerable<MyNamespace.Comment> MyRepeater_GetData()
{
return new List<Comment>
{
new Comment { CommentId =1, Name= "foo"},
new Comment { CommentId =2, Name= "bar"},
};
}

image url Concatenate string

i want to achievet the following Url
ImageUrl='~/products_pictures/(imageId)_middle.jpg
im using gridview and datalist
im trying the follwoing combination but it not working
<asp:Image ID="Image1" ImageUrl='~/products_pictures/<%#Eval("Id")%>_middle.jpg' runat="server" /></td>
<asp:Image ID="Image1" ImageUrl=<%"~/products_pictures/"%><%#Eval("Id")%><%"_middle.jpg"%> runat="server" />
I would use String.Format for this. It makes concatenation much easier:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# String.Format("~/products_pictures/{0}_middle.jpg", Eval("ID"))%>'
this should work:
</td><asp:Image ID="Image1" ImageUrl="~/products_pictures/<%#Eval("Id")%>_middle.jpg" runat="server" /></td>
if not debug from this:
</td><%#Eval("Id")%></td>
ells you can try
<%# ((objectName)Container.DataItem).Id%>
((DataRowView)Container.DataItem)["Id"]
This might work
<asp:Image ID="Image1" runat="server" ImageUrl="~/products_pictures/<%#Eval("id") %>_middle.jpg"/>
If not you can put a asp:Literal, and a HiddenField to store id and in GridView.RowDataBound Event you can add image as a text to the literal
The server tag is not well formed because of the quotes
put single quotes on the outside and try again
ImageUrl='~/products_pictures/<%#Eval("Id")%>_middle.jpg'

Set asp:LinkButton text in markup

How would it be possible to set text of ASP.NET LinkButton like below:
<asp:LinkButton id="LinkButton_Select" runat="server" Text='
<p><%# DataBinder.Eval(Container.DataItem, "Start")%></p>
<p><%# DataBinder.Eval(Container.DataItem, "End")%></p>
'/>
Try this
<asp:LinkButton id="LinkButton_Select" runat="server" Text='<%# "<p>"+ DataBinder.Eval(Container.DataItem, "Start")+"</p> <p>"+DataBinder.Eval(Container.DataItem, "End")+"</p>"%>'/>
Why not just do the below:
<p><asp:LinkButton id="LinkButton_Select" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Start")%>'/><p>
<p><asp:LinkButton id="LinkButton_Select2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "End")%>'/><p>
try something like
<asp:LinkButton id="LinkButton_Select" runat="server" Text='<%# string.Format("<p>{0}</p> <p>{1}</p>",DataBinder.Eval(Container.DataItem, "Start"),DataBinder.Eval(Container.DataItem, "End")) %>'/>
Your code will fail because, on a runat="server tag, each attribute has to either be completely a '<%# %>' section, or not at all. You can't use <%# %> for part of it and plain text for the rest. #StrouMfios showed the way around that using string.Format, but there's another issue - when converted to HTML, you'd end up with an <a> tag containing <p> tags, which is illegal. If splitting it up into two separate linkbuttons doesn't work for you, the only other way you could do it legally is by using <span> tags styled to be display:block with extra spacing.
I found this answer which is the most simple:
Text='<%# ""+ Eval("Start") + "" + Eval("End")+""
Thanks all!
This worked for me, set the value of attribute text in the page load.
Example:
yourpage.aspx
<asp:Button ID="yourButtonId" runat="server" OnClick="StartEvent" />
yourpage.aspx.cs
protected void Page_Load(Object sender, EventArgs e)
{
// Set Text asp:Button
yourButtonId.Text = "Your text";
}

Change the source of image by clicking thumbnail using updatePanel

For example, i have this ImageViewer.ascx UserControl:
<div class="ImageTumbnails">
<asp:ListView ID="ImageList" runat="server" ItemPlaceholderID="ItemContainer">
<LayoutTemplate>
<asp:PlaceHolder ID="ItemContainer" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:HyperLink runat="server"
NavigateUrl='<%# Link.ToProductImage(Eval("ImageFile").ToString())%>'>
<asp:Image runat="server" ImageUrl='<%# Link.ToThumbnail(Eval("ImageFile").ToString()) %>' />
</asp:HyperLink>
</ItemTemplate>
</asp:ListView>
</div>
<div class="ImageBig">
<asp:Image ID="ProductImageBig" runat="server" ImageUrl="" />
</div>
When the thumbnail is clicked it will change the source of ProductImageBig with its hyperlink target.
How can i achieve this using UpdatePanel ? ( Or will i be able to )
You are currently using a HyperLink control which will direct the user to the value of the NavigateUrl property. If it goes to a separate page, how will it modify the URL of the ProductImageBig control?
One option is to change the HyperLink control to a ImageButton and then specify a method in your codebehind for the "OnCommand" property.
In the code behind, you can cast the sender object to a the ImageButton, retrieve its ImageURL, and then set the URL of your ProductImageBig
public void DisplayPhoto(object sender, CommandEventArgs args)
{
ProductImageBig.NavigateUrl = ((ImageButton)sender).ImageUrl;
updatePanel.Update();
}
If you have the entire markup surrounded in an UpdatePanel named "updatePanel" and you have the properties set correctly, you can then update it after setting the Url.

Conditional output in GridView row

I databing array of User objects to GridView control. Last column contains "action" anchors (edit, remove):
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
Remove
Edit
</ItemTemplate>
</asp:TemplateField>
However I would like not to output first anchor to Remove action if currently binded User object has the same id as use logged in (available with this.SessionUser.Id).Something like this:
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
Remove
if (this.SessionUser.Id <> Eval("user_id") { Edit }
</ItemTemplate>
</asp:TemplateField>
How can I do it?
Thanks!
You can use a runat="server" control for this
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
Remove
<a href="Edit.aspx?id=<%# Eval("user_id") %>" runat="server"
visible='<%# this.SessionUser.Id <> Eval("user_id") %>'>Edit</a>
</ItemTemplate>
</asp:TemplateField>
All server controls, even HTML tags with runat="server" have this Visible property, which omits the control from the final HTML when it is false.
not supported :( you need to write another function passing it user_id and get the appripriate string from it like this:
//in cs file
protected string GetLink(object o)
{
if(!SessionUser.Id.Equals(o)) //or anyother way to compare equality
return string.Format("<a href=\"Edit.aspx?id={0}\">",0);
return "";
}
//in aspx file
<ItemTemplate>
Remove
<%# GetLink(Eval("user_id"))%>
</ItemTemplate>
you can use CSS:
<a style='visible:<%# this.SessionUser.Id <> Eval("user_id") %>' > ... </a>
make sure that this.SessionUser.Id is a public variable in your .cs file

Resources