DropDownList in UpdatePanel

前端 未结 4 384
清歌不尽
清歌不尽 2021-01-17 15:29

In my project I have placed a dropdownlist in an updatepanel.what I wanted to do is to select a value from dropdownlist and use it in a session.

but whatever I do, it

相关标签:
4条回答
  • 2021-01-17 16:06

    It sounds like you're doing way more work than you need to here. Have you looked into using an ASP.NET Wizard Control? http://msdn.microsoft.com/en-us/magazine/cc163894.aspx or just Google it.

    If you still want to do it your way, you have to submit to the server (either with no autopostback + manual submit button click, or by enabling autopostback) since the Session is a server-side concept. HTTP is a stateless protocol, so the only concept of state has to be done outside of HTTP's domain. This means you're stuck storing state on the server (for instance, in the session) or, much more restrictively, on the client's computer (such as in a cookie).

    0 讨论(0)
  • 2021-01-17 16:08

    In order to get anything stored to Session, you have to submit it to the server.

    Perhaps some more details on why you don't want the UpdatePanel refreshing would be helpful, and what you are trying to accomplish using the value in Session.

    EDIT: Based on your comments, it seems to me that the solution would be to store the current .ascx file in Session, and set your DropDownList to have autopostback enabled.

    So, on your handling of the "Next" and "Back" buttons, store an indicator for the correct .ascx to Session.

    During your postback handling of the dropdownlist event, you could simply ensure that the current .ascx file is still being shown, by checking session for the correct file to show. When the result is returned to the client, nothing will appear to have changed, because the UpdatePanel is smart enough to realize it's the same content, and you will have successfully dealt with the dropdownlist value.

    0 讨论(0)
  • 2021-01-17 16:15

    It sounds like you may not be using the UpdatePanel feature properly. If you have the UpdatePanel set to update when children fire events, only the UpdatePanel should refresh, not the entire page. The code below seems to behave similar to what you are seeking. When changing the drop down, only the update panel posts back to the server and when you refresh the page, you can get the value out of the session.

    ASPX CODE

    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            Current Time: <asp:Label ID="lblTime" runat="server" /><br />
            Session Value: <asp:Label ID="lblSessionValue" runat="server" /><br />
            <br />
            <asp:UpdatePanel ID="upSetSession" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="ddlMyList" runat="server" 
                        onselectedindexchanged="ddlMyList_SelectedIndexChanged"
                        AutoPostBack="true">
                        <asp:ListItem>Select One</asp:ListItem>
                        <asp:ListItem>Maybe</asp:ListItem>
                        <asp:ListItem>Yes</asp:ListItem>
                    </asp:DropDownList>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="ddlMyList" 
                        EventName="SelectedIndexChanged" />
                </Triggers>
            </asp:UpdatePanel>
        </div>
    </form>
    

    CODE BEHIND

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblTime.Text = DateTime.Now.ToShortTimeString();
            if (Session["MyValue"] != null) 
                this.lblSessionValue.Text = Session["MyValue"].ToString();
        }
    
        protected void ddlMyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session.Remove("MyValue");
            Session.Add("MyValue", this.ddlMyList.SelectedValue);
        }
    
    0 讨论(0)
  • 2021-01-17 16:21

    thanks a lot I solved problem by controlling variables in Page_Load event.

    If Label1.Text = 1 Then
        Dim tempcontrol2 As Control = LoadControl("Page1.ascx")
        PlaceHolder1.Controls.Add(tempcontrol2)
    
    ElseIf Label1.Text = 2 Then
        Dim tempcontrol2 As Control = LoadControl("Page2.ascx")
        PlaceHolder1.Controls.Add(tempcontrol2)
    End If
    

    thank u for all answers

    0 讨论(0)
提交回复
热议问题