Asp.NET - Multiple Instances of AdRotator - asp.net

I've created a master page that uses 3 instances of System.Web.UI.WebControls.AdRotator each backed by the same XML file of ad elements. For example,
<asp:adRotator AdvertisementFile="/ads.xml" Runat="server"/>
*** ads.xml ***
<Advertisements>
<Ad>
<ImageUrl>/Path/Banner.gif</ImageUrl>
<NavigateUrl>http://www.google.com</NavigateUrl>
<AlternateText>
The best search engine in the world!
</AlternateText>
<Impressions>3</Impressions>
</Ad>
</Advertisements>
The problem is on occasion the same ad will appear in 2 or more of the AdRotators at a given instant.
What's the best way to make the ads presented at any given time unique? One possibility is to separate the ads into 3 distinct XML files and assign each AdRotator a different file. However, that leads to a given ad always being in the same location that may or my not be the "premium" location on the page.

AdRotator wasn't designed to display a series of banners, so you can't prevent duplicates if you place multiple AdRotator controls on a Web Form and point them to the same AdvertisementFile.
The AdRotator is only designed to serve the most basic ad functionality. It's definitely not intended to compete with or replace a "real" ad-serving system. If you need anything more elaborate, you'll need to look into a third-party system or roll your own.

Although I second Rex M's recommendation of rolling out your own AdRotator because of its inherent limitations, there is a way to do this within the AdCreatedEvent of the rotator. For 3 or more rotators try the concept outlined at http://tinyurl.com/7rymect (must scroll to bottom of page to see answer). Otherwise, you could try something along these lines with 2 AdRotators on the page:
/// <summary>
/// First ad rotator control
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void AdRotator_AdCreated(object sender, AdCreatedEventArgs e)
{
//Get displayed ad's id
int currentAd = (int)e.AdProperties["AdId"];
//Remove that ad from the list
TList<Banner> ads = GetBannerThatIsNotThisAdId(currentAd, AdRotator.KeywordFilter);
//Do we have an ad to display?
if (ads.Count > 0)
{
AdRotator1.DataSource = ads;
AdRotator1.DataBind();
}
else //nope, so hide 2nd control
{
AdRotator1.Visible = false;
}
}
public static TList<Banner> GetBannerThatIsNotThisAdId(int adId, string pCategory)
{
BannerService bannerService = new BannerService();
TList<Banner> banners = bannerService.GetAll();
Banner banner = bannerService.GetByAdId(adId);
banners.ApplyFilter(delegate(Banner b) { return b.Keyword.Equals(pCategory) && (b.IsActive.Equals(true)); });
banners.Remove(banner);
return banners;
}

Related

Get last step in component wizard

C# Wizard control has the event ActiveStepChanged that is triggered when we move through the steps of the wizard. The current step is stored in the property called ActiveStepIndex. I need to retrieve the step immediately preceding the current ActiveStepIndex.
I'm trying this way but without results up to now:
ICollection s = wizTransferSheet.GetHistory();
IList steps = s as IList;
WizardStep lastStep = steps[steps.Count].Name;
Depending on how complex your wizard is, that can be tricky sometimes. You can't always use the ActiveStepIndex. Luckily, the wizard control logs a history of the steps visited, and you can leverage this to retrieve the last step that was visited:
You can use this function to get the last step that was visited:
/// <summary>
/// Gets the last wizard step visited.
/// </summary>
/// <returns></returns>
private WizardStep GetLastStepVisited()
{
//initialize a wizard step and default it to null
WizardStep previousStep = null;
//get the wizard navigation history and set the previous step to the first item
var wizardHistoryList = (ArrayList)wzServiceOrder.GetHistory();
if (wizardHistoryList.Count > 0)
previousStep = (WizardStep)wizardHistoryList[0];
//return the previous step
return previousStep;
}
Here's some sample code from one of our wizards. The wizard is pretty complex, and there is a lot of potential branching based on what the user does. Because of that branching, navigating the wizard can be a challenge. I don't know if any of this will be useful to you, but I figured it was worthwhile including it just in case.
/// <summary>
/// Navigates the wizard to the appropriate step depending on certain conditions.
/// </summary>
/// <param name="currentStep">The active wizard step.</param>
private void NavigateToNextStep(WizardStepBase currentStep)
{
//get the wizard navigation history and cast the collection as an array list
var wizardHistoryList = (ArrayList)wzServiceOrder.GetHistory();
if (wizardHistoryList.Count > 0)
{
var previousStep = wizardHistoryList[0] as WizardStep;
if (previousStep != null)
{
//determine which direction the wizard is moving so we can navigate to the correct step
var stepForward = wzServiceOrder.WizardSteps.IndexOf(previousStep) < wzServiceOrder.WizardSteps.IndexOf(currentStep);
if (currentStep == wsViewRecentWorkOrders)
{
//if there are no work orders for this site then skip the recent work orders step
if (grdWorkOrders.Items.Count == 0)
wzServiceOrder.MoveTo(stepForward ? wsServiceDetail : wsSiteInformation);
}
else if (currentStep == wsExtensionDates)
{
//if no work order is selected then bypass the extension setup step
if (grdWorkOrders.SelectedItems.Count == 0)
wzServiceOrder.MoveTo(stepForward ? wsServiceDetail : wsViewRecentWorkOrders);
}
else if (currentStep == wsSchedule)
{
//if a work order is selected then bypass the scheduling step
if (grdWorkOrders.SelectedItems.Count > 0)
wzServiceOrder.MoveTo(stepForward ? wsServicePreview : wsServiceDetail);
}
}
}
}

asp.net - best way to display 5 records at a time using two pages

I have to create a asp.net page that shows 5 records, stays for 10 seconds, shows the next 5 for another 10 seconds and return to the first 5 record when reaching last record.
I need to retrieve data from database every 5 min only to do refresh for the displayed record. I want to do this in 2 different pages - for example one page does all the work and the second page only displays data without doing anything with the database. So whenever there is any problem with the database the first page will keep on showing the last records.
I figured out the first part by using a grid view and timer but I struggled with the second.
It is a notice page like flight information board idea at airports.
I hope you can help me with it or tell me what is the best way to do it.
I would suggest using a usercontrol inside your aspx page, then in your aspx page you can have it check the database and retrieve the data pass it to your usercontrol and databind. If your aspx page cannot retrieve data then it wouldn't databind your usercontrol so it will not change its data.
usercontrol
private DataTable _newdata = null;
public DataTable NewData
{
get { return _newdata; }
set { _newdata = value; }
}
public override void DataBind()
{
if(NewData.Rows.Count > 0)
{
mygridview.DataSource = NewData;
mygridview.DataBind();
}
}
I would recomment doing it on client side using Java Script/JQuery. JQuery JCycle plugin will do it for you

jQuery UI autocomplete is not displaying results fetched via AJAX

I am trying to use the jQuery UI autocomplete feature in my web application. What I have set up is a page called SearchPreload.aspx. This page checks for a value (term) to come in along with another parameter. The page validates the values that are incoming, and then it pulls some data from the database and prints out a javascript array (ex: ["item1","item2"]) on the page. Code:
protected void Page_Load(object sender, EventArgs e)
{
string curVal;
string type ="";
if (Request.QueryString["term"] != null)
{
curVal = Request.QueryString["term"].ToString();
curVal = curVal.ToLower();
if (Request.QueryString["Type"] != null)
type = Request.QueryString["Type"].ToString();
SwitchType(type,curVal);
}
}
public string PreLoadStrings(List<string> PreLoadValues, string curVal)
{
StringBuilder sb = new StringBuilder();
if (PreLoadValues.Any())
{
sb.Append("[\"");
foreach (string str in PreLoadValues)
{
if (!string.IsNullOrEmpty(str))
{
if (str.ToLower().Contains(curVal))
sb.Append(str).Append("\",\"");
}
}
sb.Append("\"];");
Response.Write(sb.ToString());
return sb.ToString();
}
}
The db part is working fine and printing out the correct data on the screen of the page if I navigate to it via browser.
The jQuery ui autocomplete is written as follows:
$(".searchBox").autocomplete({
source: "SearchPreload.aspx?Type=rbChoice",
minLength: 1
});
Now if my understanding is correct, every time I type in the search box, it should act as a keypress and fire my source to limit the data correct? When I through a debug statement in SearchPreload.aspx code behind, it appears that the page is not being hit at all.
If I wrap the autocomplete function in a .keypress function, then I get into the search preload page but still I do not get any results. I just want to show the results under the search box just like the default functionality example on the jQuery website. What am I doing wrong?
autocomplete will NOT display suggestions if the JSON returned by the server is invalid. So copy the following URL (or the returned JSON data) and paste it on JSONLint. See if your JSON is valid.
http://yourwebsite.com/path/to/Searchpreload.aspx?Type=rbChoice&term=Something
PS: I do not see that you're calling the PreLoadStrings function. I hope this is normal.
A couple of things to check.
Make sure that the path to the page is correct. If you are at http://mysite.com/subfolder/PageWithAutoComplete.aspx, and your searchpreload.aspx page is in another directory such as http://mysite.com/anotherFolder/searchpreload.aspx the url that you are using as the source would be incorrect, it would need to be
source: "/anotherFolder/Searchpreload.aspx?Type=rbChoice"
One other thing that you could try is to make the method that you are calling a page method on the searchpreload.aspx page. Typically when working with javascript, I try to use page methods to handle ajax reqeusts and send back it's data. More on page methods can be found here: http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx
HTH.

Adding TextBocex, CheckBoxes on Request C#

I will be starting a project soon at my company and the client would like to have the option in the portal to add textboxes or checkboxes as an administrator,,, so for instance initially I may have something like
Name [textBox]
Phone [textBox]
So the client would like to log in as an admin and be able to add
Name [textBox]
Phone [textBox]
Receive Brochure [checkBox] //added by client.
Forget about the portal and the admin part.. what I would like to know is what would be the best way to design this (the user to be able to add elements)
Any ideas would be much appreciated
Regards
You could create an additional aspx-form in which the User (or the Admin) is able to define and create his/her own forms, you supply the Variable names and they choose to add the controls, save it in a specific scheme in the Database, e.g.
UserForm:
UserID FormID
Form:
FormID FormName
FormElement:
FormID VariableName ControlType Index
Of course this could also be done by an administrator and be visible by everyone.
To view the specific forms you could add yet another aspx-page containing the following code:
protected void Page_Load(object sender, EventArgs e)
{
//you saved the FormName or ID to Session when you accessed it
string formName = Session["FormName"].ToString();
//this handles getting the elements for this Form from DB
List<FormElement> elementList = FormElement.GetForForm(formName);
this.renderForm(elementList);
}
private void renderForm(List<FormElement> eList)
{
foreach(FormElement fe in eList)
{
//Labels left, Controls right, of course this is just a design decision
if(fe.Index%2==1)
{
Label lbl = new Label();
lbl.Text = fe.Variable;
lbl.ID = fe.ControlType + fe.Variable;
divLeft.Controls.Add(lbl);
}
else
{
dynamic ctrl = null;
switch (fe.ControlType)
{
case "TextBox":
ctrl = new TextBox();
break;
case "CheckBox":
ctrl = new CheckBox();
break;
default:
break;
}
ctrl.ID = fe.ControlType + fe.Variable;
divRight.Controls.Add(ctrl);
}
}
}
Later on after the User hitting submit you'd be able to receive the values entered into those Controls by accessing divRight.FindControl(fe.ControlType + fe.Variable) since that should be unique per Form.
This approach assumes you're using .NET 4.0 (because of dynamic), but of course you can do this just fine without it, it'll just be more code.
Please let me know if this is what you searched for or if it was helpful.
Thanks,
Dennis
From what I have done in the past where I had to create a dynamic survey based on a client needs. I had a database that contained the following tables:
Survey - stores list of client surveys.
Controls - listed the type of controls that would be needed. For example, textbox, checkbox etc.
Survey Controls - links all the controls required for a survey.
User values - stores the values entered in a controll based on the survey used.
I then added some logic to dynamically create the controls based on a survey selected by reading the values from my database.

Working with resources

there!
I'm working with asp.net 3.5 web-site. And I have such problem:
I have 3 aspx pages, that contain asp:Label control with name "LabelContent" and foreach page I have two resx files, that contain LabelContentResource.Text and LabelContent binds LabelContentResource to , for 2 cultures. Also I have content editing page. On this page admin choses page for edit and in WYSIWG editor I need to load appropriate resorce. Like so:
string pageForLoadName = "links.aspx.de-AT.resx";
string key ="LabelContent.Text";
string resValue= LoadREsource(pageForLoadName ,key );
How can I write LoadREsource fnction?
Thanks!
Something along the lines of
public string LoadResource(string pageForLoadName,string key)
{
return (String)HttpContext.GetGlobalResourceObject(pageForLoadName, key);
}
Also, don't think you need pageForLoadName = "links.aspx.de-AT.resx";
just pageForLoadName = "links.aspx.de-AT";

Resources