Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 13th, 2006, 01:59 PM
Authorized User
 
Join Date: Jun 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to jlrolin Send a message via MSN to jlrolin
Default Setting Selected attribute of DropDownList in Grid

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.

__________________
________________________
ASP.NET 1.1/2.0 Developer
VB.NET Developer





Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList, DataSource and Selected Items kingroon ASP.NET 2.0 Basics 2 February 25th, 2008 10:41 AM
re: DataGrid Selected Item from outside grid vids ASP.NET 1.x and 2.0 Application Design 1 May 2nd, 2005 04:12 PM
Dynamically set selected attribute fs22 Javascript How-To 5 December 24th, 2004 04:10 PM
DropDownList Selected Index = -1?? dorothee ASP.NET 1.0 and 1.1 Basics 4 June 11th, 2004 03:08 AM
selected value from dropdownlist control netwizard_01 ASP.NET 1.0 and 1.1 Basics 3 January 20th, 2004 09:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.