views:

1947

answers:

4

Hi,

i have a problem.

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 will always give me null value because of not checking "Enable AutoPostBack".and when i do this,it will refresh the page so this isnt what i wanted.

how can i solve this problem?

any ideas...

+3  A: 

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.

womp
well i have created a wizard like structure.and its very complicated actually.but let me tell u this.i have 7 .ascx file and in my default.aspx file there is ScriptManager and UpdatePanel.in my updatepanel by using Next and Back buttons i call these .ascx files dynamically so it gives a wizard-like view.if i autopostback my dropdownlist it will go back to the 1st webusercontrol named page1.ascx. this isnt acceptable for me.im trying to find a solution but couldnt find yet
iersoy
and by using the session im planning to store global variables for using in other .ascx files.
iersoy
I updated my answer with a possible solution for you.
womp
+1  A: 

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).

Matt Ball
thanks i will be looking at it.
iersoy
+3  A: 

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);
    }
RSolberg
oke now i found a way how to solve this i think.as i said i dynamically called 7 ascx files in an updatepanel.if i use If(IsPostBack) Then....End Ifmaybe i can make it work
iersoy
I typically do add the if(IsPostback) check around my page load and page init functions if they have any content...
RSolberg
+1  A: 

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

iersoy