Datalist og dropdownlist i EditItemTemplate tagget
Hej jeg har et problem med at binde til og sætte det selected item på en dropdownlist control som er inde i EditItemTemplate tagget i en datalist control. Jeg har prøvet at koge min side meget ned og sammen for at vise mit problem. Min dropdownlist bliver ikke fyldt, og jeg er lidt i tvivl om jeg overhoved gør det på den rigtige måde, for jeg er ikke helt skarp i at lave asp.net endnu.Her kommer koden:
<%@ Page Debug="True" Language="vb" %>
<%@ import Namespace="System.data" %>
<%@ import Namespace="System.data.oleDB" %>
<%@ import Namespace="System.configuration" %>
<%@ import Namespace="System.Security" %>
<%@ import Namespace="System.Security.Principal" %>
<%@ import Namespace="ToolBox" %>
<script runat="server">
Dim my As WindowsPrincipal
Dim myid As WindowsIdentity
Dim isEditing As Boolean = False
Sub Page_Load(sender As Object, e As EventArgs)
my = CType(HttpContext.Current.User, WindowsPrincipal)
myid = CType(my.Identity, WindowsIdentity)
if not my.IsInRole("CGEDB\intranetforside") then
response.redirect("velkommen.aspx?error=")
end if
if not Page.isPostBack then
bindliste()
end if
End Sub
Public Sub nyhedsliste_ItemCommand(Sender As Object, E As DataListCommandEventArgs)
CheckIsEditing(e.CommandName)
End Sub
Public Sub CheckIsEditing(commandName As String)
If DataList1.EditItemIndex <> -1 Then
If commandName <> "Cancel" And commandName <> "Update" Then
errorLabel.Text = "Du er allerede ved at redigere en nyhed. Tryk venligst på 'Opdater' el. 'Annuller',<br /> inden du fortsætter med at redigere en anden."
isEditing = True
End If
End If
End Sub
Public Sub nyhedsliste_ItemCommand(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.EditItem Then
Dim MyDropDownList as dropdownlist = e.item.findcontrol("afnavnlist")
MyDropDownList.DataSource=GetAfdelingsliste()
MyDropDownList.Items.FindByValue(CType(e.item.findcontrol("afnavn"), Label).text).Selected = true
MyDropDownList.databind()
end if
End Sub
public sub nyhedsliste_Edit(ByVal Sender As Object, ByVal E As DataListCommandEventArgs)
If Not isEditing Then
DataList1.EditItemIndex = e.Item.ItemIndex
bindliste()
End If
end sub
public sub nyhedsliste_Update(Sender As Object, E As DataListCommandEventArgs)
end sub
public sub nyhedsliste_Cancel(Sender As Object, E As DataListCommandEventArgs)
DataList1.EditItemIndex = -1
bindliste()
end sub
public sub nyhedsliste_Delete(Sender As Object, E As DataListCommandEventArgs)
end sub
Function bindliste()
Dim h As Hashtable = new Hashtable()
h.Add ("Generelt", "Generelt")
h.Add ("Service", "Service")
h.Add ("Udvikling", "Udvikling")
h.Add ("ADM", "ADM")
h.Add ("PRO", "PRO")
DataList1.DataSource = h
DataList1.DataBind
End Function
Function GetAfdelingsliste() As ArrayList
Dim grouplist() as String = {"Generelt","Service","Udvikling","ADM","PRO","Lager","Salg","PTA","IT","PF","GenereltElite","ServiceElite","ADMElite"}
Dim afdelingsList As ArrayList = New ArrayList()
Dim i As Integer
For i = 0 To grouplist.GetUpperBound(0)
' if my.IsInRole("CGEDB\intranet" & grouplist(i)) then
afdelingsList.Add(grouplist(i))
' end if
next i
errorLabel.Text = "den kommer her ind!!"
return afdelingsList
End Function
</script>
<html>
<head>
<link href="../css/main.css" type="text/css" rel="stylesheet" />
<link href="../css/forms.css" type="text/css" rel="stylesheet" />
<script language="JavaScript" src="../javascripts/common.js" type="text/JavaScript"></script>
</head>
<body>
<form id="form1" runat="server">
<br />
<asp:Label id="errorLabel" runat="server" enableviewstate="False" /><br />
<asp:DataList id="DataList1" runat="server" OnEditCommand="nyhedsliste_Edit" OnUpdateCommand="nyhedsliste_Update" OnCancelCommand="nyhedsliste_Cancel" OnDeleteCommand="nyhedsliste_Delete" OnItemCommand="nyhedsliste_ItemCommand">
<ItemTemplate>
<asp:Label id="afnavn" runat="server" text='<%# Cstr(Container.DataItem.key) %>' /> | <asp:Label id="afnavn2" runat="server" text='<%# Cstr(Container.DataItem.value) %>' />
<br />
<asp:LinkButton id="LinkButton1" runat="server" CommandName="Edit">Rediger</asp:LinkButton>
<asp:LinkButton id="LinkButton2" runat="server" CommandName="Delete">Slet</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="afnavnlist" runat="server"></asp:DropDownList> | <asp:Label id="afnavn2" runat="server" text='<%# Cstr(Container.DataItem.value) %>' />
<br />
<asp:LinkButton id="LinkButton1" runat="server" CommandName="Update">Opdater</asp:LinkButton>
<asp:LinkButton id="LinkButton2" runat="server" CommandName="Cancel">Annuller</asp:LinkButton>
</EditItemTemplate>
</asp:DataList>
</form>
</body>
</html>