Feb 12 2009

ASP.NET empty Repeater problem

Classified in: ASP.NETpaomic at 4:17 pm

I was looking for a way to solve the problme of the repeater Control in ASP.NET, which does not offer a functionality like the Empty Data Template of the GridView control or other controls. A solution would be to inherit the Repeater control and create an EmptyTemplate element, as in this post. After some searches I fount this interesting post. The author uses the HeaderTemplate and checks the DataItem count, if it is 0 it shows some text (in the example, it shows a tabel row, but you could easuly do the same with a div or span), otherwise it is hidden. Just what I was looking for! This is the code to use:

<asp:Repeater ID=”repeater” runat=”server”

DataSourceID=”mydatasource” >

<HeaderTemplate>
<span id=”span1″ runat=”server” visible=’<%# ((ICollection)repeater.DataSource).Count == 0 ? true : false %>’>
No data found.
</span>
</HeaderTemplate>

Here repeater is the Id of the Repeater Control.

Tags:

Nov 20 2008

ASP.NET DropDownList and null value (or nullable database field)

Classified in: ASP.NETpaomic at 12:41 pm

If you have an ASP.NET detailsview or similar control, you may already have added extended control, such as radiobuttonlists or dropdowns. If so, you may have experienced and error when you use such controls binded to a database field which is nullable, and has a null value. In this case, the bind fails. To avoid this error, simply add a list item to the collection of the dropdownlist, like this:

<asp:ListItem Selected=”True” Value=”">Nessuna</asp:ListItem>

Add the AppendDataBoundItems property to the dropdownlist:

asp:DropDownList ID=”ddl” runat=”server” DataSourceID=”mydatasource”
DataTextField=”text” DataValueField=”id”
SelectedValue=’<%# Bind(“id”) %>’ AppendDataBoundItems=”true” >

et voila, it works! The trick is using Value=”", which is interpreted as a null value during databinding.

Happy coding!

Tags:

Jul 11 2008

Debugging ASP.NET applications on Windows Vista

Classified in: ASP.NET, Windowspaomic at 1:43 pm

While debugging an ASP.NET application under Windows Vista, I noticed I could not debug it, because the green arrowhead button next to debug remained green after pressing it, meaning I was not in debug mode. Finally I found out this interesting tutorial. It basically suggests to open Control panel, then program and features, then click on Turn Windows features on and off, and then set the checkbox as the following figure:

Debugging ASP.NET applications on Windows Vista

That’s it!

PS: don’t forget to run Visual Studio 2005 as administrator, clicking on the icon with the right button!

Tags:

Feb 23 2008

Empty Datalist: show message?

Classified in: ASP.NETpaomic at 11:36 am

If you have a ASP.NET datalist and you need to show a message when there are no items in the list, you can use a code like the following:

myDataGrid.DataSource = …;
myDataGrid.DataBind();

if(myDataGrid.Items.Count == 0)
{
myDataGrid.Visible = false;
someLabelWithAMessage.Visible = true;
}
else
{
myDataGrid.Visible = true;
someLabelWithAMessage.Visible = false;
}

This is for C#, for VB it wuold be something like:
myDataGrid.DataSource = …
myDataGrid.DataBind()

If myDataGrid.Items.Count = 0 then
‘There are no records
myDataGrid.Visible = False
someLabelWithAMessage.Visible = True
Else
myDataGrid.Visible = True
someLabelWithAMessage.Visible = False
End If

Happy coding!

Tags: