Problem with dropdownlist

Last post 10-29-2008, 6:07 PM by jfeeney. 4 replies.
Sort Posts: Previous Next
  •  10-27-2008, 6:51 PM 45125

    Problem with dropdownlist

    I have the following code in my aspx page

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"

    DataSourceID="SqlDataSource1" DataTextField="PageName" DataValueField="HTML"

    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >

    </asp:DropDownList>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"

    ConnectionString="<%$ ConnectionStrings:GenericCMSConnectionString %>"

    SelectCommand="SELECT [PageName], [HTML] FROM [T_Pages]"

    DeleteCommand="DELETE FROM [T_Pages] WHERE [PageName] = @PageName"

    InsertCommand="INSERT INTO [T_Pages] ([PageName], [HTML]) VALUES (@PageName, @HTML)"

     

     

    UpdateCommand="UPDATE [T_Pages] SET [HTML] = @HTML WHERE [PageName] = @PageName">

    <DeleteParameters>

    <asp:Parameter Name="PageName" Type="String" />

    </DeleteParameters>

    <UpdateParameters>

     

    <asp:Parameter Name="HTML" Type="String" />

    <asp:Parameter Name="PageName" Type="String" />

    </UpdateParameters>

    <InsertParameters>

    <asp:Parameter Name="PageName" Type="String" />

     

    <asp:Parameter Name="HTML" Type="String" />

    </InsertParameters>

    </asp:SqlDataSource>

    <CE:Editor ID="Editor1" runat="server"

    OnPostBackCommand="Editor1_PostBackCommand">

    </CE:Editor>

    </div>

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    </form>

    and this code in the code behind

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

    {

    Editor1.Text = DropDownList1.SelectedItem.Value;

    Label1.Text = "Working on...." + DropDownList1.SelectedItem.Text;

    }

    protected void Editor1_PostBackCommand(object sender, CommandEventArgs e)

    {

    this.SqlDataSource1.UpdateParameters["PageName"].DefaultValue = DropDownList1.SelectedItem.Text;

    this.SqlDataSource1.UpdateParameters["HTML"].DefaultValue = Editor1.Text;

    this.SqlDataSource1.Update();

    //this.SqlDataSource1.UpdateParameters.Clear();

    this.Label1.Text = "saved to...." + DropDownList1.SelectedItem.Text;

    }

    Problem:
    When the user hits the save button on the CuteEditor the dropdownlist always returns to the top item on the list. When I was developing, I didn't notice the problem because I was only saving at the end of my changes. However, when my wife started testing the system she would make little changes and then hit save and make another change and hit save (saving her work incrementally as she goes). The problem is that if you are not carefully, when you hit save the second time you are saving the work back to another page (the top page of the dropdownlist).
     
    Question: Why is the dropdownlist returning back to SelectedItem index 0? What would be the best approach to stop this?
  •  10-28-2008, 2:38 AM 45132 in reply to 45125

    Re: Problem with dropdownlist

    Hi jfeeney,
     
    I can not reproduce this issue,please tyr this code:
     
    When I selected a value from DropDownList1 and click the save button of editor,the value of DropDownList1 still is my choice.

    <%@ Page Language="C#" %>

    <%@ Register TagPrefix="CE" Namespace="CuteEditor" Assembly="CuteEditor" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                ArrayList values = new ArrayList();

                values.Add("Apple");
                values.Add("Orange");
                values.Add("Pear");
                values.Add("Banana");
                values.Add("Grape");

                // Set the DataSource of the Repeater.
                DropDownList1.DataSource = values;
                DropDownList1.DataBind();

            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Editor1.Text = DropDownList1.SelectedItem.Value;

            Label1.Text = "Working on...." + DropDownList1.SelectedItem.Text;
        }

        protected void Editor1_PostBackCommand(object sender, CommandEventArgs e)
        {
            this.Label1.Text = "saved to...." + DropDownList1.SelectedItem.Text;
        }

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Welcome to Quattro Html Editor</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                </asp:DropDownList>
                <CE:Editor ID="Editor1" runat="server" OnPostBackCommand="Editor1_PostBackCommand">
                </CE:Editor>
            </div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
              </form>
    </body>
    </html>
     
     
     
    Regards,
     
    Ken
  •  10-28-2008, 6:20 PM 45162 in reply to 45132

    Re: Problem with dropdownlist

    Ken, I copied your code into my website - it worked fine.

    I then split the code between the aspx part and a aspx.cs (code behind) part - it worked fine.

    I then commented out the "onload" event which databind the values to the dropdownlist. I added my SqlDataSource and linked it to the dropdownlist. I left the other code in the aspx.cs page the same so it displayed the value of the dropdownlist into the editor on selectedindexchanged - again everything works fine.

    I then added this code to the Editor1_PostBackCommand
            this.SqlDataSource1.UpdateParameters["PageName"].DefaultValue = DropDownList1.SelectedItem.Text;
            this.SqlDataSource1.UpdateParameters["HTML"].DefaultValue = Editor1.Text;
            this.SqlDataSource1.Update();

    At this point it stops working - every time the save button is pressed the dropdownlist goes back to the top item in the list.

     

  •  10-29-2008, 4:01 AM 45175 in reply to 45162

    Re: Problem with dropdownlist

    Hi jfeeney,
     
    try this way:

    <%@ Page Language="C#" %>

    <%@ Register TagPrefix="CE" Namespace="CuteEditor" Assembly="CuteEditor" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
        private int index
        {
            get
            {
                object selIndex = this.ViewState["SelIndex"];
                return selIndex == null ? 0 : (int)selIndex;
            }
            set
            {
                this.ViewState["SelIndex"] = value;
            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            Editor1.Text = DropDownList1.SelectedItem.Value;
            Label1.Text = "Working on...." + DropDownList1.SelectedItem.Text;
            DropDownList MyDropDownList = (DropDownList)sender;
            index = MyDropDownList.SelectedIndex;

        }

        protected void Editor1_PostBackCommand(object sender, CommandEventArgs e)
        {

            this.SqlDataSource1.UpdateParameters["PageName"].DefaultValue = DropDownList1.SelectedItem.Text;
            this.SqlDataSource1.UpdateParameters["HTML"].DefaultValue = Editor1.Text;
            this.SqlDataSource1.Update();
            DropDownList1.SelectedIndex = index;
            this.Label1.Text = "saved to...." + DropDownList1.SelectedItem.Text;

        }


        protected void bb1_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.UpdateParameters["PageName"].DefaultValue = DropDownList1.SelectedItem.Text;

            this.SqlDataSource1.UpdateParameters["HTML"].DefaultValue = Editor1.Text;

            this.SqlDataSource1.Update();
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"
                    DataTextField="PageName" DataValueField="HTML" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Server=(local);uid=sa;pwd=sa;database=test"
                    SelectCommand="SELECT [PageName], [HTML] FROM [T_Pages]" DeleteCommand="DELETE FROM [T_Pages] WHERE [PageName] = @PageName"
                    InsertCommand="INSERT INTO [T_Pages] ([PageName], [HTML]) VALUES (@PageName, @HTML)"
                    UpdateCommand="UPDATE [T_Pages] SET [HTML] = @HTML WHERE [PageName] = @PageName">
                    <DeleteParameters>
                        <asp:Parameter Name="PageName" Type="String" />
                    </DeleteParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="HTML" Type="String" />
                        <asp:Parameter Name="PageName" Type="String" />
                    </UpdateParameters>
                    <InsertParameters>
                        <asp:Parameter Name="PageName" Type="String" />
                        <asp:Parameter Name="HTML" Type="String" />
                    </InsertParameters>
                </asp:SqlDataSource>
                <ce:editor id="Editor1" runat="server" onpostbackcommand="Editor1_PostBackCommand">

    </ce:editor>
            </div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
               </form>
      
    </body>
    </html>

     
     
    Regards,
     
     
    Ken
  •  10-29-2008, 6:07 PM 45189 in reply to 45175

    Re: Problem with dropdownlist

    Thanks Ken, that worked perfectly.
View as RSS news feed in XML