使用postbackurl将dropdownlist值传递给另一个页面asp.net

时间:2017-08-10 23:32:26

标签: asp.net

还有其他类似标题的问题,但我找不到任何问题可以解决我的问题。

我在第1页上有一个带有下拉列表和按钮的表单。在按钮上我指定了一个postbackurl。我需要从第1页的第1页检索该下拉列表的值。我已经省略了第1页的代码隐藏和第2页的aspx,因为它们无关紧要。你能帮我解决一下我的问题吗?

ASPX Page 1

<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder1" runat="server">

    <asp:DropDownList ID="DDLCountryList" runat="server" CssClass="ddlstyle" AutoPostBack="true" onchange="if(this.selectedIndex == 0)return false;" OnSelectedIndexChanged="Country_SelectedIndexChanged">
        <asp:ListItem Text="Country" Value="Country" Selected="True" />
        <asp:ListItem Text="USA" Value="USA" />
        <asp:ListItem Text="Canada" Value="Canada" />
    </asp:DropDownList>

    <asp:Button ID="Button1" runat="server" Text="Register" CssClass="pink-btn" PostBackUrl="/page2"/>

</asp:Content>

第2页的代码背后

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Testing
{
    public partial class WebForm11 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            string DDLCountryList = Request.Form["DDLCountryList"];


        System.Diagnostics.Debug.WriteLine(DDLCountryList);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您正在使用母版页。他们将控件的ID重命名为MainContentPlaceHolder1_DDLCountryList。对照的name也会发生这种情况。这将变成ctl00$MainContentPlaceHolder1$DDLCountryList

现在,当您捕获表单帖子时,您需要name作为键,而不是ID。因此,当您使用aspnet重命名的名称时,您正在使用甚至不存在的Control的ID。

所以在页面上尝试使用DropDownList来获取正确的密钥。

string DDLCountryList = Request.Form[DDLCountryList.UniqueID];

这是在另一页上捕获表单帖子所需的关键值。