views:

84

answers:

1

Hey,

I've two issues currently preventing me from finishing two projects properly. I'll put them both here as I believe they're connected to the asp.net page lifecycle, but I can't find a way around them.

First I have a DropDownList which I must sort in codebehind. It only contains text, so I should be able to do that with the following method called in page load:

        Dim alist As ArrayList = New ArrayList

        For Each litem As ListItem In ltEsittelyDropDownList.Items
            alist.Add(litem.Text)
        Next

        alist.Sort()

        Dim uusiDDList As New DropDownList

        For i As Integer = 0 To alist.Count - 1
            Dim litem As New ListItem
            litem.Text = alist(i).ToString
            litem.Value = alist(i).ToString
            uusiDDList.Items.Add(litem)

            ' Response.Write(alist(i).ToString)
        Next

        ltEsittelyDropDownList = uusiDDList
        ltEsittelyDropDownList.DataBind()

As you can see, there's a commented response.write in there, which shows the list is actually sorted. So why, when I load the page, can't I see any effect?

The other problem, which is more critical and difficult, is as follows:

In the aspx page I'm binding a SQL Server 2005 datasource to a gridview. And in the code-behind I catch on to the RowDataBound event in which I handle some links and properties inside the gridviews' cells. But I cannot get this to work on the first page load, only after the first extra postback.

So, what is there to do? And thanks for all advice in front!

+1  A: 

Your first problem is calling DataBind on a control you have filled manually. You likely have a DataSource specified in the control declaration, which is being used when DataBind is called. You can simplify the code by just adding the list items to the original control:

For i As Integer = 0 To alist.Count - 1
    ltEsittelyDropDownList.Items.Add(New ListItem(alist(i).ToString())
Next

Alternatively, as you have a collection already, you can just bind it to the control:

ltEsittelyDropDownList.DataSource = alist
ltEsittelyDropDownList.DataBind()

For your second problem, some example code would help - specifically, where and how the control is databound and the code in RowDataBound.

Jason Berkan
Heh, some mistake. Used your first suggestion and it worked fine. Thanks a bunch!
Zan