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


Get RadAjaxManager from within User Control

by

Posted on November 24, 2017



ContentPlaceHolder content = 
  this.Page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
var ajax = content.FindControl("AjaxMng") as RadAjaxManager;
ajax.Alert("Test");

Read More


Redirect users after n seconds from code-behind

by

Posted on November 27, 2017



C#

// redirect them to login in 4 sec
HtmlMeta meta = new HtmlMeta();
meta.HttpEquiv = "Refresh";
meta.Content = "4;url=/login";
this.Page.Controls.Add(meta);

Read More


Force culture for RadNumericTextBox (Telerik)

by

Posted on December 06, 2017



< telerik:radnumerictextbox ... type="Currency" culture="de-CH" numberformat-positivepattern="CHF n" />

Read More


Code Debugging - Is it easy?

by

Posted on February 22, 2018



As mentioned many times by now, Debugging is a discipline that requires patience, and a fervent attention to detail.

When it comes to solving difficult problems, issues or bugs, you need to locate, and resolve the problem's root cause.

Usually it happens that, after going through one idea after another and hitting debug button for an hour (usually more like 2 or 3 hours), you are not closer to solving the problem than you were when you started.

Now, all you've done is wasted a few hours of time, and worked yourself into a desperate and frustrated state that is not conducive to thinking rationally, and applying logic.

Simply because you don't actually have any clue what's causing the problem you are just shooting blindly.

The problem is often, if not always, more complicated than we would like.

As developers, we would much rather add new features, or write the next great algorithm, than try to figure out why something we wrote a month ago is suddenly no longer working.

"Stop thinking, and look!" - is one of the maxims from David Agan who points out that the engineers like to think and not to look.

Why? Well because the thinking is fun. In fact, for a lot of us it's probably why we became software engineers in the first place.

So why do we insist on being able to locate a bug by thinking about it? Probably because it's a lot easier than looking for it.

Of course, looking is tedious. Sometimes you don't even know where to start looking.

However, in order to be certain that you're fixing the right problems, and that you can prove that you've fixed it, make sure that every time you debug a problem you:

  1. Observe the issue in action
  2. Apply your fix
  3. Observe that the issue is resolved

If you don’t do these steps, in that order you can’t know if you actually solved the issue at hand, another different issue, or just wrote a bunch of code that does nothing at all.

So avoid frustration, don't waste time solving the wrong things, and stop thinking, and look already!

Read More


A regex that matches all credit cards

by

Posted on January 26, 2011



This is a regular expression that validates all credit card numbers including VISA, MASTER, AMEX and DISCOVERY! ^((4\d{3})|(5[1-5]\d{2})|(6011)|(34\d{1})|(37\d{1}))-?\d{4}-?\d{4}-?\d{4}|3[4,7][\d\s-]{15}$

 

< asp:regularexpressionvalidator id="CreditCardValidator" runat="server" controltovalidate="CreditCardNumber" display="Static" errormessage="Please enter valid card number" validationexpression="^((4\d{3})|(5[1-5]\d{2})|(6011)|(34\d{1})|(37\d{1}))-?\d{4}-?\d{4}-?\d{4}|3[4,7][\d\s-]{15}$" validationgroup="CustomSignup" text="*" />


Read More


Remove Server Header Attribute In IIS 10.0

by

Posted on January 16, 2019



There is another way to do the same but if you want the simplest one please add the following to the web.config into the system.webServer section:

system.webServer
    
< rewrite >
    < outboundRules rewriteBeforeCache="true" >
	    < rule name="Remove Server header" >
            < match serverVariable="RESPONSE_Server" pattern=".+" / >
            < action type="Rewrite" value="Your Text Here" / >
        < / rule >
    < / outboundRules > 
< / rewrite >

< customHeaders >		
    < remove name="X-Powered-By" / >
< / customHeaders >
    
  

HTH

Read More


Page 1 of 1

Copyright © ASPNETer 2006 - 2016