Tuesday, October 12, 2010

set Rad Combobox to custom message when customtext is false

In an event where "AllowCustomText" is set to false, and you want to set the custom message and set the current index to less then zero so the first item is not already selected, leaving you the option to select the first available item in combobox and fire a postback event!






CodeBehind :

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            StockRep stockRep = new StockRep();
            var items = stockRep.FindAllActiveStocks();
            this.rcbRefNo.DataSource = items;
            this.rcbRefNo.DataTextField = "RefNo";
            this.rcbRefNo.DataValueField = "StockId";
            this.rcbRefNo.DataBind();

            RadComboBoxItem rcbi = new RadComboBoxItem();
            rcbi.Text = "--Select an Item--";
            this.rcbRefNo.Items.Insert(0, rcbi);
        }
    }

-

Using the AdRotator Control in ASP.NET 2.0 and C#

This tutorial will show how to implement random advertisements on your website, using the AdRotator Control, and how to capture the number of clicks for each. C# version. 


Random rotation of advertisements is made very easy in Visual Studio. This tutorial shows how to implement this control, and also a way of monitoring the number of clicks each ad receives.
This tutorial uses graphics to display clickable advertisements.

Create New Folder in Solution Explorer for images. Copy to this folder existing images to be used as advertisements.
Then create an XML file to list the ads:



~/images/ad1.jpg
http://www.aspnettutorials.com
Ad for ASPNETTutorials.com



~/images/ad2.jpg
http://www.asp.net
Ad for ASP.NET Web site

Create a web page to display the ads, add the AdRotator Control, and add the XML file (App_Data/Sample.ads) as the Data Source. The ASP should look something like this:












One way of tracking ad clicks is to write them into an XML file. To do this, you'll need to redirect the user to a page that counts the click before it redirects them to the advertisement website.
Change NavigateUrl in the XML file (Sample.ads):





~/images/ad1.jpg
AdRedirector.aspx?ad=ad1&target=http://www.aspnettutorials.com
Ad for ASPNETTutorials.com



~/images/ad2.jpg
AdRedirector.aspx?ad=ad2&target=http://www.asp.net
Ad for ASP.NET Web site

Now add another XML file - AdResponse.xml - to count the number of clicks.






Now create the redirection page, AdRedirector.aspx


protected void Page_Load(object sender, EventArgs e) {
String adName = Request.QueryString["ad"];
String redirect = Request.QueryString["target"];
if (adName == null | redirect == null)
redirect = "Default.aspx";

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
String docPath = @"~/App_Data/AdResponses.xml";
doc.Load(Server.MapPath(docPath));
System.Xml.XmlNode root = doc.DocumentElement;
System.Xml.XmlNode adNode =
root.SelectSingleNode(
@"descendant::ad[@adname='" + adName + "']");

if (adNode != null) {
int ctr =
int.Parse(adNode.Attributes["hitCount"].Value);
ctr += 1;
System.Xml.XmlNode newAdNode = adNode.CloneNode(false);
newAdNode.Attributes["hitCount"].Value = ctr.ToString();
root.ReplaceChild(newAdNode, adNode);
doc.Save(Server.MapPath(docPath));
}
Response.Redirect(redirect);
}

To read the click data from XML, create another ASPX page - ViewAdData.aspx
Add XmlDataSource from the toolbox and choose AdResponses.xml (containing click data) as Data Source.
Also add GridView from toolbox and choose the Data Source. The code should be something like this: