Archive

Archive for March, 2012

Handle Null Value with Gridview

March 29, 2012 3 comments

Hi all,

try this example to handle Null Values with Gridview

1) With TemplateField :


<asp:TemplateField>
 <ItemTemplate>
 <asp:Label ID="TxtPhn" runat="server" Text='<%# Eval("Phone")== Null ? "N / A" : Eval("Phone") %>'>
 </asp:Label>
 </ItemTemplate>
 </asp:TemplateField>

2) With BoundField :

<asp:BoundField NullDisplayText="N / A" DataField="Phone" />

3) Another Way :

<span><%#Eval("Phone1") == null ? "it is null" : Eval("Phone1")%></span>

4) Another Way :


public string checkForNull(object val)
{
 if (val == null)
 {
 return "null value";
 }
 return val.ToString();
}

and in .aspx Page

<span><%#checkForNull(Eval("Phone1"))%></span>

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net

Display all images from folder with Repeater

March 2, 2012 1 comment

Hi all,

try this example to Display all images from folder with Repeater

1) Open VS2008 and create a new website and add new web page name it “Default.aspx”

2) In Website add new folder name it “images” and add any images inside this folder

3) Open Default.aspx and add Repeater control from Toolbox


<asp:Repeater ID="FileRepeater" runat="server">
 <ItemTemplate>
 <asp:Images ID="img" ImageUrl='<%#"Images/" + Eval("Name") %>' runat="server" Width="120px" Hight="100px" />
 </ItemTemplate>
</asp:Repeater>

In code behind :-


public List<FileInfo> GetImageNames()
{
 string imgPath = Server.MapPath("~/Images/");
 List<FileInfo> images = new List<FileInfo>();
 DirectoryInfo directoryInfo = new DirectoryInfo(imgPath);
 FileInfo[] fileInfo = directoryInfo.GetFiles();
 foreach (FileInfo file in fileInfo)
 {
 images.Add(file);
 }
 return images;
}

protected void Page_Load(object sender, EventArgs e)
{
 FileRepeater.DataSource = GetImageNames();
 FileRepeater.DataBind();
}

Another Way :-


protected void Page_Load(object sender, EventArgs e)
{
 if (!IsPostBack)
 {
 ListImages();
 }
}
private void ListImages()
{
 DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/images/"));
 FileInfo[] file = dir.GetFiles();
 ArrayList list = new ArrayList();
 foreach (FileInfo info in file)
 {
 if (info.Extension == ".jpg" || info.Extension == ".jpeg" || info.Extension == ".gif")
 {
 list.Add(info);
 }
 }
 Repeater1.DataSource = list;
 Repeater1.DataBind();
}

Hope this help

Good Luck

Categories: ASP.Net