Archive

Posts Tagged ‘ASP.NET’

C#: Language not supported in Ajax Control Toolkit? No problem, do localization by yourself

In my projects I often have to implement user interface in two languages: English and Polish. Although localization in .NET is quite easy, one thing was annoying to me: AjaxControlToolkit doesn’t support Polish language.

When my user choose Polish language on the webpage then he still see in Calendar control ‘Today’ word instead Polish version (‘Dzisiaj’). Same thing for several other controls, for example PasswordStrength control. So one day I decide to correct this once and for all. Below I will show you how to localize Ajax Control Toolkit.

To localize AjaxControlToolkit to our langauge (Culture) we will have to do few simple steps. In below examples I use library version for .NET 2.0 but the same steps are in case of Ajax Control Toolkit for .NET 3.5

First step, download library source from http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326

Unzip, and open in Visual Studio AjaxControlToolkit project which is located in directory AjaxControlToolkit in zip file.
Read more…

Categories: ASP.NET Tags: ,

C#: How to add (or use) custom parameters for ASP.NET DataSource controls (SqlDataSource, ObjectDataSource)

During work with DataSource controls in Visual Studio one of the common question is how to add some custom data as parameter. There are several pre-definied parameters like SessionParameter, ControlParameter, ProfileParameter etc. but often we need to use something else.

    So let’s for example create custom parameter for username which will be get from User.Identity.Name

A lot of people use _Selecting event of DataSource control to assign suitable data.

In this scenario you have to add empty parameter.

dso_params

Next, add this code into _Selecting event:

  protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.InputParameters[0] = User.Identity.Name;
    }

That’s all, that’s fast but it’s not so elegant and reusable as method shown below.

More elegant way
Read more…

Categories: ASP.NET Tags: ,

C#: How to add programatically meta tag to ASP.NET page

// Example: add meta tag with refresh command
    protected void Page_Load(object sender, EventArgs e)
    {
        string strCurrentURL = "http://localhost/test";

        // Add meta tag with refresh command
        string strRefreshSeconds = "3"; // how many seconds

        HtmlMeta metaTags = new HtmlMeta();
        metaTags.HttpEquiv = "refresh";
        metaTags.ID = "EnterHereSomeUniqueName";
        metaTags.Content = strRefreshSeconds + ";URL=" + strCurrentURL;
        Header.Controls.Add(metaTags);
    }
Categories: ASP.NET Tags: ,

C#: Remove item from DropDownList by Value

// 1st method, more elegant
ListItem li = dropdownlist.Items.FindByValue(strValue);
if(li != null)
    dropdownlist.Items.Remove(li);

// 2nd method, all in one line but less readable
dropdownlist.Items.RemoveAt(
dropdownlist.Items.IndexOf(dropdownlist.Items.FindByValue(strValue))
);
Categories: ASP.NET Tags: ,

C#: How to select an item in a DropDownList by Value

//Tested on .NET 2.0
ListItem li = yourDropDownlist.Items.FindByValue("yourValue");
if (li != null)
    yourDropDownlist.SelectedIndex = yourDropDownlist.Items.IndexOf(li);
Categories: ASP.NET Tags: ,

C#: How to check if value exists in DropDownList

//Tested on .NET 2.0

public static bool IsValueInDropdownList(DropDownList controlName,
string strValue)
{
    if (controlName.Items.FindByValue(strValue) != null)
        return true;

    return false;
}
Categories: ASP.NET Tags: ,