Life, the Universe and the Internet

My ramblings about making a living on the internet and life in general

Archive for the ‘Programming’


Having More Than Two Sections In An ASP.Net Repeater Control

If you’re familiar with the ASP.Net repeater control then you will know that when laying out the repeater contents you can have individual style for alternating rows so far example every other row could have a different background. But what if you wanted more sophisticated options, say for example you wanted to do a news style repeater with the first item having a photo and an in depth story, then next five having a picture and summary and the remaining items having just a headline.

Well I found a great tip on how you can do this with a single repeater. The way to do it is to set up panels inside your repeaters item template, as the example below:

<asp:Repeater id="myRepeater" runat="server">

<ItemTemplate>

<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# (Container.ItemIndex  = 0) %>' >
<div>

My First Item Controls

</div>
</asp:PlaceHolder>

<asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%# (Container.ItemIndex >=1) and (Container.ItemIndex <5) %>' >
<div class="underlined">

My Next Five Item Controls

</div>
</asp:PlaceHolder>

<asp:PlaceHolder ID="PlaceHolder3" runat="server" Visible='<%# (Container.ItemIndex >= 5) %>' >
<div

The Reminder Of My Items Controls

</div>
</asp:PlaceHolder>

</ItemTemplate>

</asp:Repeater>

So you set up a panel for each of the individual styles that you require and then the Visible property of the panel is derived from where we are currently in the list of items. Nice and easy, requires no complex code behind tricks and works like a charm.

Hope this will be of help to someone out there, it certainly was to me.

How To Stop Losing The Focus With An ASP.Net AJAX Panel

I had a recent problem when working with ajax update panels on an asp.net application. I basically had a panel with lots of drop down boxes and text boxes inside it. Whenever a drop down was changed or a value was entered in a textbox, the whole panel would be recalculated as it contained totals and sub totals based on the contents of these boxes.

The problem I had was that I had to include all of the controls inside the update panel otherwise I would have ended up with 20-30 update panels on the page, a situation that I really wanted to avoid. Because the controls were inside the panel every partial postback I was losing the focus as effectively even though you don’t see it, the panel is destroyed and rebuilt every time.

So it became impossible for a user to just tab through the form updating values, as the focus was reset with every field, not an ideal user friendly situation.

So after much research I discovered that you can actually force focus to an individual control using the script manager control in the server side code. The code to do this is pretty simple, and would be something like:

Me.ScriptManager1.SetFocus(Textbox1)

Where scriptmanager1 is the name of your script manager control and textbox1 is the name of the control you want to set the focus too.

Now this is fine if you always know which control should have the focus, however if you have a lot of controls being processed inside one update panel you might not know which control to focus on next. Thankfully I found a neat little javascript on this blog http://couldbedone.blogspot.com/2007/08/restoring-lost-focus-in-update-panel.html That you can include with your script manager on any page, the javascipt will take care of all your focus problems for any update panels you have on your page. I’ll include the full script code below, don’t ask me to explain how it works, I’m just happy that it does and hope it will be of some use to you.

// JScript File

var lastFocusedControlId = "";

function focusHandler(e)

{

document.activeElement = e.originalTarget;

}

function appInit()

{

if (typeof(window.addEventListener) !== "undefined")

{

window.addEventListener("focus", focusHandler, true);

}

Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler);

}

function pageLoadingHandler(sender, args)

{

lastFocusedControlId = typeof(document.activeElement) === "undefined"

? "" : document.activeElement.id;}function focusControl(targetControl)

{

if (Sys.Browser.agent === Sys.Browser.InternetExplorer)

{

var focusTarget = targetControl;

if (focusTarget && (typeof(focusTarget.contentEditable) !== "undefined"))

{

oldContentEditableSetting = focusTarget.contentEditable;

focusTarget.contentEditable = false;

}

else

{

focusTarget = null;

}

targetControl.focus();

if (focusTarget)

{

focusTarget.contentEditable = oldContentEditableSetting;

}

}

else

{

targetControl.focus();

}

}

function pageLoadedHandler(sender, args)

{

if (typeof(lastFocusedControlId) !== "undefined" && lastFocusedControlId != "")

{

var newFocused = $get(lastFocusedControlId);

if (newFocused)

{

focusControl(newFocused);

}

}

}

Sys.Application.add_init(appInit);

ASP.NET 2 Generating a Client Side Message Box from a Server Side Event or Function

One of the big problems with an asp.net application over say a windows application is that when you really want to bring something to the users attention, it can be difficult to pop up a modal dialog box to tell the user what is going on. You cannot use the generic messagebox function because on a server side event this will pop up a message on the server which is not very useful.

So there are of course ways around this problem, by using javascript on button click events etc, but what if you want to display a messagebox from some server side code that is not initiated from a user action such as a button click. Well as it turns out and I recently discovered this is very easy to do with asp.net 2, by using the CientScript.RegisterStartupScript event. Below is an example.

Private Sub SomeSubRoutine()

   If SomeVariable <> SomethingElse Then

      Dim strMessage As String
      strMessage = "Warning, this process generated an error"
      Page.ClientScript.RegisterStartupScript(Me.GetType(), _
        "alert", "<script>alert('" + strMessage + "');</script>")

   End If

End Sub

So there you go, pretty simple really. Now any server side process on a web page can generate a message box for the user if you require it. You don’t necessarily need to declare the fill Page.ClientScript.RegisterStartupScript, on an aspx page you can get away with just ClientScript.RegisterStartupScript. But if you use the full name it will work anywhere including user controls.

Hope this is of some help to someone out there.

SQL Server 2005: Selecting Records From One Table That Do Not Exist In Another

This week I learned a handy little piece of sequel, which even though you probably already now about, I thought I would share it as I had not seen it before.

The Scenario

Say you had two tables, for example a customer table and an orders table. The tables were structured so that whenever an order was placed it was related to a particular customer through the ID. Pretty straightforward so far right? Well now say you wanted a list of all customers that have never had an order placed against them, so therefore no records will exist in the orders table.

Now I was going to approach this by doing all sorts of complicated outer joins, but then someone should be a much neater solution, it is the NOT IN clause which I had not come across before. So you basically say show me all records where table1’s ID is NOT IN table2. Below is an example using the tables I already mentioned.

SELECT cu.CustomerName,
Cu.CustomerID
FROM dbo.Customers cu
WHERE Cu.CustomerID NOT IN
(SELECT CustomerID FROM dbo.Orders)

Pretty simple eh, and works like a charm. Hope this will be as helpful to someone out there as it was to me.

OnClientClick and Form Validation Controls

Yes, believe it or not I am going to write an on-topic post on my blog. Not only that but a programming post even, shocking I know. But I had a particular problem today and I thought the solution was worth sharing.

So I had a form, fairly simple form, a handful of textboxes and dropdown boxes and a submit button. I had some validation controls on there to ensure that the data was entered correctly and the form was working fine. I decided to add a confirmation message to the onclientlclick event of the submit button. You know the sort of thing, a popup button that says “Are you sure you want to perform this action” with an ok and cancel button. So I added the following code to the onclientlclick property of the button.

return confirm('Are you sure you want to perform this action?')

This had an unexpected result, when you click ok on the popup window, it ignored all of the form validation controls and submitted the form whatever was filled in. Well after much digging around google, I found the following solution. You need to wrap the javascript in an if statement and check the Page_ClientValidate property. So all you need to do is replace the code in the onclientlclick property with the following.

if (Page_ClientValidate()){return confirm('Are you sure you want to perform this action?')}

This now works perfectly, the popup doesn’t even appear unless all of the validation requirements have been met. A useful little piece of javascript I though, hope it will be of some use to you.

Interviews, Am I Missing The Point?

I just had a technical interview for a developer contract and I feel the need to post about it because I really couldn’t see the point of the interview. I know that the interview went staggeringly badly, as I did struggle with a lot of the questions. Now that is of course my fault, but I just find myself annoyed at the questions themselves.

Now if I was interested in hiring a contractor, I would be very interested in knowing what real world experience that person has with writing websites. I would want to know about their history, about them personally, about what they want out of the contract etc. I would be very interested in examples of their work that they could demonstrate.

What I wouldn’t do is bombard them with a string of ridiculously low level questions and ask them nothing else at all. Who cares if I can explain from memory every single event that is triggered in order when a datagrid is loaded? What differences does it make? Do you want a developer with a wealth of real word experience in designing a living multi-tiered web application or do you want a walking encyclopaedia on .Net.

The answer in this case was clearly the latter, so I know I won’t get the contract and how I feel right now I’m not overly bothered that I won’t.