UpdatePanel 中的下拉列表

发布于 2024-08-02 02:10:27 字数 122 浏览 4 评论 0原文

在我的项目中,我在更新面板中放置了一个下拉列表。我想做的是从下拉列表中选择一个值并在会话中使用它。

但无论我做什么,它总是会给我空值,因为没有检查“启用自动回发”。当我这样做时,它会刷新页面,所以这不是我想要的。

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 isn't what I wanted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

憧憬巴黎街头的黎明 2024-08-09 02:10:27

听起来您可能没有正确使用 UpdatePanel 功能。 如果您将 UpdatePanel 设置为在子项触发事件时更新,则只有 UpdatePanel 应该刷新,而不是整个页面。 下面的代码似乎与您正在寻找的代码类似。 更改下拉列表时,只有更新面板会发回服务器,并且当您刷新页面时,您可以从会话中获取值。

ASPX 代码

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

背后代码

    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);
    }

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);
    }
維他命╮ 2024-08-09 02:10:27

为了将任何内容存储到会话中,您必须将其提交到服务器。

也许更多关于为什么您不希望刷新 UpdatePanel 的详细信息以及您尝试使用 Session 中的值来完成的操作会有所帮助。

编辑:根据您的评论,在我看来,解决方案是将当前的 .ascx 文件存储在会话中,并将 DropDownList 设置为启用自动回发。

因此,在处理“下一步”和“后退”按钮时,请将正确的 .ascx 指示符存储到会话中。

在回发处理 dropdownlist 事件期间,您可以通过检查会话中是否有正确的文件来显示,从而确保当前的 .ascx 文件仍在显示。 当结果返回到客户端时,不会出现任何变化,因为 UpdatePanel 足够聪明,可以意识到它是相同的内容,并且您将成功处理 dropdownlist 值。

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.

放赐 2024-08-09 02:10:27

听起来你所做的工作比你需要做的要多得多。 您是否考虑过使用 ASP.NET 向导控件? http://msdn.microsoft.com/en-us/magazine/cc163894。 aspx 或者只是 Google 一下。

如果您仍然想按照自己的方式进行操作,则必须提交到服务器(要么不使用自动回发 + 手动单击提交按钮,要么启用自动回发),因为会话是服务器端概念。 HTTP 是无状态协议,因此唯一的状态概念必须在 HTTP 域之外完成。 这意味着您必须将状态存储在服务器上(例如,在会话中),或更严格地说,存储在客户端计算机上(例如在 cookie 中)。

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

海夕 2024-08-09 02:10:27

非常感谢,我通过控制 Page_Load 事件中的变量解决了问题。

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

谢谢你的所有回答

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文