下拉所选项目并repose.redirect

问题描述 投票:0回答:1

我的项目中有两个下拉列表,并从 sql server 获取项目。其中一个显示某项内容的列表(我将其命名为 DropdownSoore),另一个显示它的成员列表(我将其命名为 DropdownAye),当从 DropdownSoore 中选择一个项目时,该列表会更新。然后,当从 DropdownAye 选择一个项目时,通过转发查询字符串转到它们的页面。我更新 DropdownAye 没有问题,但是当我从中选择项目并重定向到它的页面时,DropdownAye 会丢失所选项目并显示第一个项目(默认)和 url 中查询字符串的索引。

我该怎么办?

请原谅我的语法问题,我不是英语......

我设置了DropdownSoore:

<asp:DropDownList ID="DropDownListSoore" runat="server" class="nav-link btn btn-outline-secondary dropdown-toggle" aria-haspopup="true" aria-expanded="true" AutoPostBack="True"></asp:DropDownList>
for (int i = 0; i <= dt.Rows.Count - 1; i++) 
{ 
    string IdSoore = Convert.ToString(dt.Rows[i]["IdSoore"]); 
    string NameSoore = Convert.ToString(dt.Rows[i]["NameSoore"]); 
    DropDownListSoore.Items.Add(IdSoore + "." + NameSoore); 
}

string forwardedIdSoore = Request.QueryString["IdSoore"];

if (forwardedIdSoore != null)
{
    DropDownListSoore.Items.FindByValue(forwardedIdSoore);
}

然后设置DropdownAye:

<asp:DropDownList ID="DropDownListAye" runat="server" class="nav-link btn btn-outline-secondary dropdown-toggle" aria-haspopup="true" aria-expanded="true" OnSelectedIndexChanged="DropDownListAye_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
DropDownListAye.Items.Clear();
for (int i = 0; i <= dt1.Rows.Count - 1; i++)
{
    DropDownListAye.Items.Add(Convert.ToString(dt1.Rows[i]["NumberAye"]));
}

int iSelectedAye = Convert.ToInt32(DropDownListAye.SelectedIndex) + 1;
string SelectedAye = Convert.ToString(dt1.Rows[iSelectedAye]["IdAye"]);
Session["SSelectedAye"] = SelectedAye;

string forwardedIdAye = Request.QueryString["IdAye"];
if (forwardedIdSoore != null)
{
    DropDownListSoore.Items.FindByValue(forwardedIdAye);
}

在page_Load中。并且更新 DopdownAye 没有问题,但是当我用它来重定向时,总是在 url 中获取 IdAye=1 并显示 DropdownAye 的第一项...:

protected void DropDownListAye_SelectedIndexChanged(object sender, EventArgs e)
{
    int SelectedAye = Convert.ToInt32(Session["SSelectedAye"]);
    
    int SelectedSoore = Convert.ToInt32(DropDownListSoore.SelectedIndex) + 1;
    
    string forward = "~/contentAye.aspx?IdSoore=" + SelectedSoore + "&IdAye=" + SelectedAye;
    Response.Redirect(forward);
}

我尝试了 !IsPostBack 但也没有实用程序。

我做了我想做的所有事情,但我不知道我应该做什么。

c# asp.net dropdown response.redirect
1个回答
0
投票

您在哪里加载这些信息?

请记住,页面加载始终会在每次回发、每次按钮单击以及页面上调用后台代码的任何其他事件时触发和触发。

以上的真正含义是什么?

嗯,这意味着您只需要加载一次下拉菜单、网格视图或您拥有的任何其他内容。

因此,对于我构建的最后 200 多个网页,我总是在页面加载事件中包含此代码存根:

所以这个标记:

        <h3>Select Hotel</h3>
        <asp:DropDownList ID="DropDownList1" runat="server"
            DataValueField="ID"
            DataTextField="HotelName" Width="356px">
        </asp:DropDownList>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server"
            Text="Show/get/display drop selection" OnClick="Button1_Click"
            CssClass="btn" />
        <br />
        <h3>Selected result</h3>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // load up data, controls etc.
        string strSQL =
            "SELECT ID, HotelName FROM tblHotelsA ORDER BY HotelName";
        DataTable dt = General.MyRst(strSQL);
        DropDownList1.DataSource = dt;
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("Please Select Hotel", ""));

    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    string sResault =
        $@"dropdown list value (pkid) = {DropDownList1.SelectedItem.Value}
        <br/>
        Dropdown list Text (Hotel) = {DropDownList1.SelectedItem.Text}";

    Label1.Text = sResault;
}

结果:

超级重要:

如果我在页面加载中省略 !IsPostBack 测试,那么对于每次单击按钮,下拉列表重新加载代码都会触发,并在按钮存根或回发之前运行。因此,我将从下拉列表中看到/找到/获取没有有效值,因为页面加载始终在给定的代码存根之前运行,因此如果页面加载“每次”都重新加载数据,那么它将崩溃在下拉列表中选择所选值。

因此,网格、数据、下拉列表等的加载只能在页面加载中发生一次,因为页面加载始终在后面的每个按钮或事件代码存根之前运行。

如果您因此仅在第一个真实页面加载时加载控件和下拉列表,那么用户在这些下拉列表中选择的值应该起作用,但只有当您始终在您的代码中包含所有重要的 if (!IsPostBack) 代码存根时才起作用。页面加载事件。

现在,当然可以代替该按钮来加载+将下拉列表显示到该标签中?

当然,单击按钮很可能(像经常一样)跳转或导航到另一个页面。因此,我们可以将所选值传递到下一页,而不是将所选值填写到该标签中的代码。以及如何做到这一点?好吧,你可以使用 session(),你可以在 URL 中使用参数,甚至可以使用回发 URL,然后整个上一页就变得可用 (Page.Previous),理论上可以让你抓取上一页中的任何和所有值。

但是,假设我们选择 session() 来传递值,那么:

  Session["HotelPK"] = DropDownList1.SelectedItem.Value;

  Response.Redirect("GridFun3.aspx");
© www.soinside.com 2019 - 2024. All rights reserved.