0

好的,所以我有点迷路,可以做一些帮助。

我正在创建一个程序,用户将数据输入到默认页面上的表单中(我有这个工作)。

然后我使用会话变量从默认页面上的文本框中获取数据输入,并将该数据放入 Page2 上的下拉菜单中(我有这个工作)。

我现在要做的是使用从 page2 的下拉列表中选择的数据并将其输出到标签上。任何帮助,将不胜感激。

Page2 代码波纹管会话填充下拉

public partial class About : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            MyFruit = Session["Fruitname"] as List<string>;
            //Create new, if null
            if (MyFruit == null)
                MyFruit = new List<string>();
            DropDownList1.DataSource = MyFruit;
            DropDownList1.DataBind();


        }
4

2 回答 2

2

您可以使用SelectedIndexChangedevent ofDropDownList来处理这个问题。你的AutoPostBack属性DropDownBox应该设置为True

示例代码如下:

设计代码:page.aspx

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem>name1</asp:ListItem>
        <asp:ListItem>name2</asp:ListItem>
    </asp:DropDownList>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

代码隐藏文件:page.aspx.cs

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label1.Text = DropDownList1.SelectedValue.ToString();
        }
于 2013-11-10T11:10:05.047 回答
1

不确定这是否是您要查找的内容,但我猜您想要下拉列表的事件以获取信息并将其放入会话中以传递到下一页

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string item=DropDownList.SelectedItem;
    Session["selectedItem"]=item;
    Response.Redirect("TheNextPageURL")
}

public partial class TheNextPage : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["selectedItem"]!=null)
        {
            Label1.Text=Session["selectedItem"].toString();
        }
    }
}

希望有帮助

于 2013-11-10T11:14:10.203 回答