Blog Posts List

Having a “No image available” if your image URL errors out – RadGrid

by Yugolancer

Posted on January 08, 2016



It often happens that you’ve got no image for all the items in your store.

It could be caused by different reasons like your server where you host the images is temporary down, the third party cannot serve the image at the moment or you just have no appropriate image at the moment. Anyway this is how you should keep your store consistent without those broken image icons that internet browsers add in there.

Usually i do that from code-behind like the following:

VB.NET code:


Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
        ' set the markup for each item
        If TypeOf e.Item Is GridDataItem Then
            Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)            
 
            Dim thumbnail As Image = CType(dataItem.FindControl("imageThumbnail"), Image)
            If thumbnail IsNot Nothing Then
                Dim imagedbvalue As String = DataBinder.Eval(dataItem.DataItem, "imageThumbnail").ToString
                thumbnail.ImageUrl = imagedbvalue
                ' if the image does not exist we load an image with "photo coming soon" title
                Dim comingsoon As String = "http://" & Request.Url.Host & "/images/ComingSoon.jpg"
                thumbnail.Attributes.Add("onerror", "this.src= '" & comingsoon & "';")
            End If
            ' other code 
       End If
End Sub

C# code:


protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
	// set the markup for each item
	if (e.Item is GridDataItem) {
	        GridDataItem dataItem = (GridDataItem)e.Item;
 
		Image thumbnail = (Image)dataItem.FindControl("imageThumbnail");
		if (thumbnail != null) {
			string imagedbvalue = DataBinder.Eval(dataItem.DataItem, "imageThumbnail").ToString;
			thumbnail.ImageUrl = imagedbvalue;
			// if the image does not exist we load an image with "photo coming soon" title
			string comingsoon = "http://" + Request.Url.Host + "/images/ComingSoon.jpg";
			thumbnail.Attributes.Add("onerror", "this.src= '" + comingsoon + "';");
		}
		// other code 
	}
}

Read More


System.InvalidOperationException – Eval(), XPath(), or Bind()

by Yugolancer

Posted on February 04, 2016



If you get the following error you are about to get the solution either :)

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Cause:

Usually it happens when you directly call say an Eval() function in the code-behind.

For example, you may have a GridView control with a label inside of the Template column:

<asp: id=" lblDescription " runat=" server " text="< %# Eval('Description') % >"></asp:>

 

Then for some reason you decide to move that to the code-behind AS IS so you say:


Dim lblDescription As Label = CType(e.Item.FindControl("lblDescription"), Label)
lblDescription.Text = Eval("Description").ToString()

Your project will compile but as soon as you try to open the certain Page, an exception ‘System.InvalidOperationException’ will be thrown.

Solution:

Just replace Eval() with DataBinder.Eval() making your code to looks like this:


Dim lblDescription As Label = CType(e.Item.FindControl("lblDescription"), Label)
lblDescription.Text = DataBinder.Eval(dataItem.DataItem, "Description").ToString()

Voila! Your problem has been solved.

Read More


Prevent RadGrid hierarchy collapsign after edit/delete

by Yugolancer

Posted on December 09, 2016



In the past you had to retain the expanded state manually when the grid rebinds, but recently Telerik introduced a property named RetainExpandStateOnRebind. If it's true, the expanded state persists automatically.

 

Read More


Retrieve data contained in a comma-delimited list of values Warning!

by

Posted on June 03, 2017



Say that you have a comma-separated String containing user IDs like following:

String userIDs = "1,2,5,8,23,67,101";

and that you want to fetch those users without splitting and looping their IDs.

Usually you do use CharIndex to search the expression:

command.CommandText = 
    "SELECT * FROM Users WHERE  CharIndex(CAST(userID AS varchar(8)) + ',', + @userIDs) > 0";
command.Parameters.AddWithValue"@userIDs", userIDs;

However if you execute the above query the last ID will be omitted YEAH it will be indeed because it checks against value + comma which does not match when it comes to 101.

FIX: add the missing comma to the end of the string:

command.Parameters.AddWithValue"@userIDs", userIDs + ",";

Hope this helps someone!

Read More


Page 1 of 1

Copyright © ASPNETer 2006 - 2016