Tuesday 22 May 2007

Opening a new window with a linkbutton control

I couldn't do it, and it turns out that lots of other people can't do it either.

They tried to set the navigateurl attribute to the url they want, and then when they tried to open the link in a new window, they got a load of javascript in the address bar and no link.

There is a legitimate way around this, but it involves extending the linkbutton, which I didn't have time to do.

If you do want to do it though, you should try the 4guysfromrolla article, because it does give quite a nice explanation.

If you want a quick workaround, I suggest that you do the following:

Swap your linkbutton link with a regular anchor tag.


<a href="#" id="linkTag" runat="server">This is an example link</a>


That'll be enough for your aspx page. The runat tag and id tags are new and allow you to get to the control from the code-behind.

If you're modifiying the control within a datalist or a datarepeater, you'll need to use findcontrol to get hold of it. If you do, you'll need this code:

Dim link as new htmlAnchor

link = e.item.findControl("linkTag")

The above creates a new anchor tag control and finds the one on your page.

However, if you're just accessing it within a regular function that isn't binding data, you can assign the bits you need like this:

linkTag.HRef = "http://karlitr0s.blogspot.com"
linkTag.innerText = "Karl's Blog"
linkTag.title = "Karl's Blog"
linkTag.target = "_blank"

The innertext tag is for labelling your link.
The href tag points to where you need to go.
The target tag is the html equivalent, and opens up the new window for you.

Doing it this way instead of using Javascript:window.open('') in the href tag means that you don't get your original page from displaying [object] or whatever your browser does when it doesn't understand where it's going within .NET.

Definately the quickest and easiest way around it, but if you have to use external links a lot in your application or you have to utilise the postback as well, you're probably better off extending the linkbutton.

Monday 21 May 2007

Datasets

"One or more rows contain values violating non-null, unique, or foreign-key constraints."

Very informative error, that. Thanks very much Microsoft.

This can happen for a number of reasons, but for me it was happening because within the datatable in my dataset, I had columns set with smaller sizes than the actual stored procedure fetched. Therefore, I was trying to fit three litres of water into a two litre bottle.

I fixed this by matching the data table's column sizes to match those within the database. Somebody else was experiencing similar problems but with a different solution on the Microsoft Forums.

Monday 14 May 2007

Gridview solution

I've found the solution to most of my problem!

The code for a standard gridview is listed below:

Protected Sub pubSearchResults_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles pubSearchResults.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim p As New Pub
p = e.Row.DataItem
Select Case p.award
Case 1
e.Row.Cells(1).Text = "Platinum"
Case 2
e.Row.Cells(1).Text = "Gold"
Case 3
e.Row.Cells(1).Text = "Silver"
Case 4
e.Row.Cells(1).Text = "Bronze"
End Select
Dim lnkEdit As New HyperLink
lnkEdit = e.Row.FindControl("lnkEdit")
'lnkEdit.NavigateUrl = "pubSearch_detail.aspx?id=" + p.id
End If
End Sub

The _RowDataBound method is a default gridview method and is called when every row in the grid is created. What I've done is accessed the row's award value (Which was collected from the row) and done a select case on it. From there, I've accessed the row passed in's cell index for the award. It's the second column in on the table, so it's the index 1. I've then assigned the text value Platinum, although I also intend to give it a css class to make it silver, which is just as simple to do.

Friday 11 May 2007

ASP.NET Web Controls

They're not ready yet!

Its web controls are beginning to cause me problems that I'm not entirely sure how I'm going to find a way around, but I'm going to post it here as soon as I do.

Imagebuttons do not have an onmouseover / onmouseout feature, which seems ridiculous to me, so I wrote a custom control that takes care of it instead. It inherits the linkbutton control with some additional methods. There are plenty of howtos floating around on the internet, so this wasn't a bit problem for me.

The big problem is turning out to be the gridview control.

Sorting out paging and sorting seems like a minimum of fuss, and I'm happy for people that require that really badly, but I'm much more concerned with a lack of formatting.

If you want to add columns to a datagrid in the code-behind, you seem buggered as far as I can see. You can't just do:

objGrid.columns.add("Column1", datasource.column1)

Why the hell can't you do that? It's common sense!

Styling fields is also a problem.

Say that your database produces a column that will contain an integer from 1 to 5. If you get number 1, you want it to say "Gold" in a nice gold font. If you get number 2, you want it to say "Silver" in nice silver font, and so on.

How the hell are you supposed to do this? One suggestion was to customise the control by inheriting the grid's class. Why should I have to do this? Why isn't basic styling for the code-behind included? Is this going to happen in .NET 3.0?

To give you a more accurate picture of what I'm talking about, see the link below:

http://www.dabs.com/productlist.aspx?&CategorySelectedId=11146&NavigationKey=11146&PageMode=1

Scroll down to the product listing, and you can quite clearly see that those rows of what I reckon is a gridview are customised to style differently depending on their values.

Some of this was surely done in the code-behind, as an objectdatasource for every page is a bit silly, especially if you want to use a proper data access layer.

If anybody's got any thoughts on this, feel free to leave a comment and let me know what I'm doing wrong that the gridview supports, as I'm at the end of my tether. How on earth you can see fit to release a control that at best binds with basic styling is ludicrous.

Furthermore, how the hell is this seen as an enterprise solution if you have to inherit classes and write new methods that do what should happen anyway?

Rant over, and I'll post a solution with complete explanation as soon as I can.

An introduction

I'm Karl, and I'm starting this blog to vent my frustrations at coding issues in any language I encounter, and to post solutions to problems that have plagued my life since my coding birth. Sounds messy, doesn't it.

It's about time I started trying to give back to the community since I take so much help from howtos and FAQs.

All the best,

Karl.