I'm having problems with the DDL in the datalist below when adding an
item.
The list binds properly and lists the correct values (up to about 350).
The code successfully finds a ddl and loads the value into the
sortedlist & the datalist. However, the selected index returned does
not (always) match that shown on the web page. The first few items are
ok, then the selection comes from an item earlier in the list.
Can anyone explain the problem?
---------------------------------------------------------------------------*------------------------
<asp:DataList ID="dlsSkills" runat="server" SelectedIndex="-1">
<HeaderTemplate>
<table border="1">
<tr align="center" style="font-weight: bold; color:
#f7f7f7;
background-color: #4a3c8c;"
valign="top">
<td colspan="6">
Skill List
</td>
</tr>
<tr align="left" style="font-weight: bold; color:
#f7f7f7;
background-color: #4a3c8c;">
<td>
Skill
</td>
<td>
Level
</td>
<td colspan="2">
Options
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr align="left" style="background-color:
lightsteelblue;" valign="top">
<td>
<asp:Label ID="lblSkill" runat="server"
Text='<%# Container.DataItem.Skill %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblLevel" runat="server"
Text='<%# Container.DataItem.Level %>'></asp:Label>
</td>
<td>
<asp:Button ID="btnEditSkill" runat="server"
CommandName="EditSkill" Text="Edit" />
</td>
<td>
<asp:Button ID="btnDeleteSkill" runat="server"
CommandName="DeleteSkill" Text="Delete" />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr align="left" style="background-color: linen;"
valign="top">
<td>
<asp:Label ID="lblSkill" runat="server"
Text='<%# Container.DataItem.Skill %>'></asp:Label>
<asp:DropDownList ID="ddlSkill" runat="server"
DataSource="<%# SkillDDLDataSource() %>"
DataTextField="Skill"
DataValueField="Type">
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtLevel" runat="server"
Text='<%# Container.DataItem.Level %>'
Width="50px"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnConfirmEdit" runat="server"
CommandName="ConfirmEdit" Text="Confirm" />
</td>
<td>
<asp:Button ID="btnCancelEdit" runat="server"
CommandName="CancelEdit" Text="Cancel" />
</td>
</tr>
</EditItemTemplate>
<FooterTemplate>
<tr align="center" style="font-weight: bold; color:
#f7f7f7;
background-color: #4a3c8c;"
valign="top">
<td colspan="6">
<asp:Button ID="btnAdd" runat="server"
CommandName="AddSkill" Text="Add Skill" />
</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
---------------------------------------------------------------------------*--------------------------
Protected Sub dlsSkills_ItemCommand(ByVal source As Object, ByVal e
As
System.Web.UI.WebControls.DataListCommandEventArgs ) Handles
dlsSkills.ItemCommand
SkillTable = GetSkillList()
If Not IsNothing(SkillTable(" ")) Then
SkillTable.Remove(" ")
End If
If e.CommandName = "CancelEdit" Then
dlsSkills.SelectedIndex = -1
dlsSkills.EditItemIndex = -1
dlsSkills.DataSource = SkillTable.GetValueList
dlsSkills.DataBind()
End If
If e.CommandName = "ConfirmEdit" Then
Dim SkillLbl As Label =
dlsSkills.Items(e.Item.ItemIndex).FindControl("lbl Skill")
Dim SkillDdl As DropDownList =
dlsSkills.Items(e.Item.ItemIndex).FindControl("ddl Skill")
Dim LevelLbl As TextBox =
dlsSkills.Items(e.Item.ItemIndex).FindControl("txt Level")
AddSkill(SkillDdl.SelectedItem.Text, LevelLbl.Text)
dlsSkills.SelectedIndex = -1
dlsSkills.EditItemIndex = -1
dlsSkills.DataSource = SkillTable.GetValueList
dlsSkills.DataBind()
End If
If e.CommandName = "AddSkill" Then
dlsSkills.SelectedIndex = -1
AddSkill(" ", 0)
dlsSkills.EditItemIndex = 0
dlsSkills.DataSource = SkillTable.GetValueList
dlsSkills.DataBind()
Dim SkillLbl As Label =
dlsSkills.Items(0).FindControl("lblSkill")
Dim SkillDdl As DropDownList =
dlsSkills.Items(0).FindControl("ddlSkill")
Dim ConfirmBtn As Button =
dlsSkills.Items(0).FindControl("btnConfirmEdit")
Dim CancelBtn As Button =
dlsSkills.Items(0).FindControl("btnCancelEdit")
SkillLbl.Visible = False
SkillDdl.Visible = True
ConfirmBtn.CommandArgument = "Add"
CancelBtn.CommandArgument = "Add"
End If
Session("Skills") = SkillTable
End Sub
Protected Sub AddSkill(ByVal Skill As String, ByVal Level As
Integer)
Dim SkillEntry As New SkillDetails
SkillEntry.Skill = Skill
SkillEntry.Level = Level
SkillTable.Add(SkillEntry.Skill, SkillEntry)
End Sub
Private Function GetSkillList() As SortedList
If Session("Skills") Is Nothing Then
Session.Add("Skills", New SortedList)
End If
Return CType(Session("Skills"), SortedList)
End Function
Public Function SkillDDLDataSource() As DataSet
SkillTable = GetSkillList()
Dim cmd As String = "SELECT [Skill], [Type] FROM [Action] WHERE
[Skill] not in ("
For i As Integer = 0 To SkillTable.Count - 1
cmd += "'" & SkillTable.GetKey(i) & "',"
Next
cmd = cmd.Substring(0, cmd.Length - 1) & ")"
Dim RMconn1 As New
SqlConnection(ConfigurationManager.ConnectionStrin gs("RMDb").ConnectionStri*ng)
RMconn1.Open()
Dim cmdAllSkills As New SqlCommand(cmd, RMconn1)
Dim sadAllSkills As New SqlDataAdapter
sadAllSkills.SelectCommand = cmdAllSkills
Dim dstAllSkills As New Data.DataSet
sadAllSkills.Fill(dstAllSkills)
RMconn1.Close()
RMconn1 = Nothing
Return dstAllSkills
End Function