Asp.Net: Charting Control (Error ChartImg.axd)

After installing and playing with the Asp.Net Charting control I decided to use it for a customer. When integrating the control to the project I got the following error

"Error executing child request for ChartImg.axd"

This error occurred because I did not update my web.config file. You have to add the following appSettingkey, httpHandler and handler.

[code:xml]

<appSettings>
    <add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:TempImageFilesDit;" />
</appSettings>
<httpHandlers>
    <add path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
</httpHandlers>

<handlers>
    <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</handlers>

[/code]

More info about the Charting control:
http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt.aspx

Enjoy charting,
Pieter

Asp.Net: Menu control remove MenuItem (MenuItemDataBound)

For a project I needed to remove menu items to the pages in the folder ‘Subscriberpages’ when a the user is in the Role of ‘Marketing’ and ‘AccountManagement’. To do this I added the following code to the MenuItemDataBound event.


protected void mainMenu_MenuItemDataBound(object sender, MenuEventArgs e)
{
 SiteMapNode node = e.Item.DataItem as SiteMapNode;

 if (node.Url.Contains("<em>Subscriberpages</em>"))
 {
  if (HttpContext.Current.User.IsInRole("<em>Marketing</em>") || HttpContext.Current.User.IsInRole("<em>AccountManagement'</em>"))
  {
   // Get menu.
   Menu topMenu = (Menu)sender;

   // Remove dataitem from menu.
   topMenu.Items.Remove(e.Item);
  }
 }
}

To generate the menu I used the Web.sitemap and the asp:Menu control.

Asp.Net: Using the OnCommand Event with CommandArgument

When using a button, linkbutton or imagebutton with CommandArguments or CommandName you can use the OnCommand event instead of the OnClick event. Using the OnCommand Event you use less code to extract the CommandArgument and CommandName from the Event comparing to the OnClick event (because you don’t need to cast the control).
Code example

The Aspx:


&lt;asp:ImageButton ImageUrl=&quot;~/Includes/Images/delete_icon.gif&quot; runat=&quot;server&quot; ID=&quot;ibtDeleteClip&quot; OnCommand=&quot;ibtDeleteClip_Command&quot; CommandArgument=&#039;' /&gt;


And the codebehind (C#):



protected void ibtDeleteClip_Command(object sender, CommandEventArgs e)
{
   string commandArg = e.CommandArgument;
}


Asp.Net: Databinding a array of strings

You can use a string array as datasource and view the string values by using the Container.DataItem property.

Code example

Codebehind:

[code:c#]

string[] testData = {"1","two","3","4"};
rptDemo.DataSource = testData;
rptDemo.DataBind();

[/code]


And in the .aspx:

[code:html]

<asp:Repeater runat="server" ID="rptDemo">
    <ItemTemplate>
        <%# Container.DataItem %>
    </ItemTemplate>
</asp:Repeater>

[/code]

Some tips for the Asp.net dropdown control

I always have problems implementing the Asp.Net dropdown control. Here are some simple tips to make your life (with dropdown controls) easier.

Adding an default item with data binded dropdown

To add an default listItem to your data binded dropdown list you just have to add a ListItem on the first place (0) of your control


CountryDropdown.DataSource = countryList;
CountryDropdown.DataValueField = "CountryId";
CountryDropdown.DataTextField = "CountryName";
CountryDropdown.DataBind();

//Add the item
CountryDropdown.Items.Inser(0, new ListItem("== Choose a Country =="));



Getting the selected value


CountryDropdown.Items[CountryDropdown.SelectedIndex].Text;



Setting dropdown listitem by value


dropDownList.SelectedIndex = dropDownList.Items.IndexOf(dropDownList.Items.FindByValue(option.OptionValue));


Validation on a dropdownlist





Hope it helps.

ListView: OnItemDataBound

Get dataItem onItemDataBound (don't forget to replace the bold parts with your objectname):

[code:c#]

protected void MainPostListView_ItemDataBound(object sender, ListViewItemEventArgs e)

{

YourObject item = (YourObject)((ListViewDataItem)e.Item).DataItem;

}

[/code]

 

Findcontrol within the itemtemplate:

[code:c#]

HyperLink hplTitle = (HyperLink)e.Item.FindControl("hyperLink");

[/code]