I am using a datagrid to display some information. I also have an edit command column attached to the grid for editing/updating/deleting. I am using a custom DropDownList class to fill the drop down lists with data from an XML source.
Here is the class:
Code:
Imports System.Web.Security
Imports System.Xml
Imports System.Web.HttpContext
Imports System.IO
Public Class CodeDropDownList
Inherits System.Web.UI.WebControls.DropDownList
Dim DCode As String
Dim DS As String
Dim UName As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim path As String = Current.Server.MapPath("datasource\dropdownlists.xml")
'Creates a XML Document and Loads path into it.
Dim doc As XmlDocument = New XmlDocument
doc.Load(path)
'Flag to determine if update is successful
Dim success As Boolean = True
'Creates a root Node to store the Document Element
Dim root As XmlNode
root = doc.DocumentElement
'Creates node to select a single specific node matching the user's name
Dim node As XmlNode = root.SelectSingleNode("list/code[@name='" + DCode + "']")
If node.HasChildNodes Then
Dim i As Integer
For i = 0 To node.ChildNodes.Count - 1
MyBase.Items.Add(node.ChildNodes(i).InnerText)
Next i
End If
If Not DisplaySelected Is Nothing And Not Username Is Nothing Then
Dim path2 As String = Current.Server.MapPath("datasource\users.xml")
Dim doc2 As XmlDocument = New XmlDocument
doc2.Load(path2)
Dim root2 As XmlNode
root2 = doc2.DocumentElement
Dim node2 As XmlNode = root2.SelectSingleNode("user[username= '" + UName + "']")
Dim rank As String = node2.Item(DS).InnerText
MyBase.Items.FindByValue(rank).Selected = True
End If
End Sub
'Indicates which drop down data to grab
Public Property DropCode() As String
Get
Return DCode
End Get
Set(ByVal Value As String)
DCode = Value
End Set
End Property
Public Property DisplaySelected() As String
Get
Return DS
End Get
Set(ByVal Value As String)
DS = Value
End Set
End Property
Public Property Username() As String
Get
Return UName
End Get
Set(ByVal Value As String)
Value = Current.Session.Item("USER_ID")
UName = Value
End Set
End Property
End Class
And the HTML Page, specifically the datagrid
Code:
<asp:datagrid id="ContactDataGrid" runat="server" AutoGenerateColumns="False" CellPadding="2"
CellSpacing="0" Border="1" BorderColor="#000000" Width="888px" HeaderStyle-HorizontalAlign="center"
Headerstyle-BackColor="#CCCCCC" HeaderStyle-ForeColor="#000000" OnEditCommand="ContactDataGrid_Edit"
OnCancelCommand="ContactDataGrid_Cancel" OnUpdateCommand="ContactDataGrid_Update" HeaderStyle-Font-Bold="True"
ItemStyle-BackColor="#66CCFF" AlternatingItemStyle-BackColor="#99CCFF" HorizontalAlign="Center">
<AlternatingItemStyle BackColor="GhostWhite"></AlternatingItemStyle>
<ItemStyle BackColor="Lavender"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="Black" BackColor="CornflowerBlue"></HeaderStyle>
<Columns>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Wrap="false" HeaderText="Commands"
HeaderStyle-Wrap="false" />
<asp:BoundColumn DataField="password" ItemStyle-HorizontalAlign="Center" HeaderText="Password"></asp:BoundColumn>
<asp:BoundColumn DataField="level" ItemStyle-HorizontalAlign="Center" HeaderText="Level"></asp:BoundColumn>
<asp:BoundColumn DataField="raceclass" ItemStyle-HorizontalAlign="Center" HeaderText="Race/Class"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="PvP Rank">
<ItemTemplate>
<div align="center">
<asp:Label id=PvPranking runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.pvp") %>'>
</asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<cc1:CodeDropDownList id="PvPrank" CssClass="textfieldsmall" DisplaySelected="pvp" Username="user" DropCode="pvp"
runat="server"></cc1:CodeDropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Guild Rank">
<ItemTemplate>
<div align="center">
<asp:Label id="Guildranking" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.rank") %>'>
</asp:Label>
</div>
</ItemTemplate>
<EditItemTemplate>
<cc1:CodeDropDownList id="GRank" CssClass="textfieldsmall" DisplaySelected="rank" Username="user" DropCode="grank"
runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="skills" ItemStyle-HorizontalAlign="Center" HeaderText="Professions"></asp:BoundColumn>
</Columns>
</asp:datagrid>
The dropdownlists populate fine, and when I have a DisplaySelected and Username property set, they grab the value from the Users XML source, and set it as the selected. W
When I UPDATE, I select a new value, hit update, and the XML is written to the file, EXCEPT the same value that was selected previously is there. When the dropdown class reloads, it sets the selected value back to what it was previously, how do I stop it from doing this, and make it take the new selection.