【问题标题】:How to select the same option that was chosen in another page如何选择在另一个页面中选择的相同选项
【发布时间】:2014-05-20 00:23:23
【问题描述】:

我的主页中有几个 DropDownList:

<asp:DropDownList style="width: 245px;" class="default" ID="slcLocation" runat="server" ClientIDMode="Static" AppendDataBoundItems="true">
</asp:DropDownList>
<br /><br />
<asp:DropDownList style="width: 245px;" class="default" ID="slcSpecialty" runat="server" ClientIDMode="Static" AppendDataBoundItems="true">
</asp:DropDownList>
<br /><br />
<asp:DropDownList style="width: 245px;" class="default" ID="slcGender" runat="server" ClientIDMode="Static" AppendDataBoundItems="true">
    <asp:ListItem Text="Any Gender" Value="" Selected="True" />
    <asp:ListItem Text="Male" Value="1" />
    <asp:ListItem Text="Female" Value="2" />
</asp:DropDownList>
<asp:Button ID="btnGoAll" class="loginButton" style="padding: 10px; float: right;" Text="Search All" OnClick="btnGoAll_Click" runat="server" ClientIDMode="Static" />

前两个是从后面的代码自动填充的,而最后一个是在我的 aspx 页面中填充的。生成页面后,源代码如下所示:

<select name="ctl00$FeaturedContent$slcLocation" id="slcLocation" class="default" style="width: 245px;">
    <option value="">All Locations</option>
    <option value="89">Theall Road</option>
    <option value="2535">Mamaroneck Avenue</option>
    <option value="1947">Huguenot Street</option>
    <option value="2666">Huguenot Street(1)</option>
    <option value="2435">verhill Road</option>
    <option value="87">estchester Avenue</option>
    <option value="1121">estchester Avenue</option>
    <option value="90">estchester Avenue</option>
    <option value="100">avis Avenue</option>
    <option value="93">te Plains Road Suite 270</option>
    <option value="1532">ic Surgery at WESTMED</option>
    <option value="1606">l, 73 Market Street</option>
    <option value="1241">nter at WESTMED</option>

</select>
<br /><br />
<select name="ctl00$FeaturedContent$slcSpecialty" id="slcSpecialty" class="default" style="width: 245px;">
    <option value="">All Specialties</option>
    <option value="553">Allergy and Immunology</option>
    <option value="315">Anesthesiology</option>
    <option value="140">Breast Surgery</option>
    <option value="141">Cardiology</option>
    <option value="2672">Care Management</option>
    <option value="2509">Chiropractic Medicine</option>
    <option value="688">Clinical Laboratory</option>
    <option value="337">Colon and Rectal Surgery</option>
    <option value="145">Critical Care</option>
    <option value="143">Dermatology / Cosmetic</option>
    <option value="633">Diabetes Education</option>
    <option value="144">Ear, Nose and Throat</option>
    <option value="146">Endocrinology</option>
    <option value="165">Eye Care</option>
    <option value="155">Family Medicine</option>
    <option value="156">Gastroenterology</option>
    <option value="157">General Surgery</option>
    <option value="158">Geriatric Medicine</option>
    <option value="1841">Gynecologic Oncology</option>
    <option value="2345">Hand Surgery</option>
    <option value="159">Hematology and Oncology</option>
    <option value="722">Hospitalist</option>
    <option value="160">Infectious Diseases</option>
    <option value="161">Internal Medicine</option>
    <option value="2069">Interventional Radiology</option>
    <option value="2318">Maternal Fetal Medicine</option>
    <option value="2654">Medical Oncology</option>
    <option value="2309">Mohs Surgery</option>
    <option value="162">Nephrology</option>
    <option value="163">Neurology</option>
    <option value="652">Nutrition</option>
    <option value="164">Obstetrics and Gynecology</option>
    <option value="634">Optical Shop</option>
    <option value="166">Orthopedic Surgery</option>
    <option value="1951">Palliative Medicine</option>
    <option value="1589">Pathology</option>
    <option value="167">Pediatrics and Adolescent Medicine</option>
    <option value="168">Physical Medicine and Rehabilitation</option>
    <option value="169">Plastic Surgery</option>
    <option value="170">Podiatry</option>
    <option value="171">Pulmonology</option>
    <option value="2061">QCOI</option>
    <option value="1460">Radiation Oncology</option>
    <option value="691">Radiology</option>
    <option value="172">Rheumatology</option>
    <option value="2520">Sleep Center</option>
    <option value="2145">Thoracic Surgery</option>
    <option value="951">Urgent Care</option>
    <option value="173">Urology</option>
    <option value="456">Vascular Surgery</option>
    <option value="174">Weight Management</option>

</select>
<br /><br />
<select name="ctl00$FeaturedContent$slcGender" id="slcGender" class="default" style="width: 245px;">
    <option selected="selected" value="">Any Gender</option>
    <option value="1">Male</option>
    <option value="2">Female</option>

</select>
<input type="submit" name="ctl00$FeaturedContent$btnGoAll" value="Search All" id="btnGoAll" class="loginButton" style="padding: 10px; float: right;" />

我的主页中有一个按钮单击选项,如下所示,它将变量保存在会话中:

protected void btnGoAll_Click(object sender, EventArgs e) {
        var locText = slcLocation.SelectedItem.Text;
        var speText = slcLocation.SelectedItem.Text;
        var genText = slcGender.SelectedItem.Text;

        Session["LocationText"] = locText;
        Session["SpecialtyText"] = speText;
        Session["GenderText"] = genText;

        Response.Redirect("~/physicians.aspx", false);
    }

我在显示相同下拉列表的另一个页面 (physician.aspx) 中有相同的 ASPX 代码。我期待的是,当用户从主页单击提交按钮时,下拉列表将自动填充 physician.aspx 页面中的这些选择。

我在physician.asp 页面中有这样的内容:

public partial class physicians : System.Web.UI.Page {
    string cString = "";
    SqlConnection Conn;
    string strQueryAll = "*";
    string sqlCode = "";
    string theLocation = "", theSpecialty = "", theGender = "";
    string locVal = "", speVal = "", genVal = "";
    string lVal = "", sVal = "", gVal = "";
    int rowCount = 0;
    protected void Page_Load(object sender, EventArgs e) {
        if (!Page.IsPostBack) {
            PopulatePhysician();
            ASPopulateLocation();
            ASPopulateSpecialty();
        }

        if (Session["LocationText"] != null && Session["SpecialtyText"] != null && Session["GenderText"] != null) {
            //dlo.InnerHtml = "Successfully retrieved " + (string)Session["LocationText"];
            //btnGoAll_OnClick(null, null);
        }
    }
}

我在physician.aspx 页面上还有一个按钮,我想自动点击它,就像用户点击了医生页面一样。

我只需要一些帮助来弄清楚如何实现会话转移和自动填充。我应该获取会话中的文本还是值以及如何将其应用于physician.aspx 页面?

我想我需要一个页面加载后执行的函数,因为控件尚未初始化?

我可以使用Javascript来实现该功能吗?我如何使它与我通过 C# 接收的会话一起工作?

【问题讨论】:

  • 不是创建 3 个会话变量,而是创建一个自定义实体并在其中设置值(不是文本,因为它不是一个好的编码习惯)并将其设置在单个会话中,还要检查 !isPostBack 中的会话否则无论用户选择什么,你最终都会再次设置相同的值
  • 感谢您的回复。我可以在主页中使用.Value 而不是.Text,然后如何在医生页面中设置它?我是 ASP.net 开发的新手,所以我会请求我能得到的所有帮助。 :)
  • 您可以在服务器端 (c#) 使用 slcLocation.SelectedValue = Convert.ToString(Session["LocationText"]);
  • 使用您的代码,看起来我必须在主页代码隐藏中使用.Value 而不是.Text?或者我可以在医生页面上slcLocation.SelectedItem
  • 我将主页代码更改为:var locText = slcLocation.SelectedItem.Value; Session["LocationText"] = locText;,在我的医师页面中我这样做了:slcLocation.SelectedValue = Convert.ToInt32(Session["LocationText"]);。我收到此错误:CS0029: Cannot implicitly convert type 'int' to 'string'

标签: c# asp.net session session-variables


【解决方案1】:

你可以有一个通用的函数,比如

public void OnBtnClick()
{
 // add the code with the query to database
}

在页面加载时,您可以像这样调用函数

 protected void Page_Load(object sender, EventArgs e) {
        if (!Page.IsPostBack) {
            PopulatePhysician();
            ASPopulateLocation();
            ASPopulateSpecialty();
        }

        if (Session["LocationText"] != null && Session["SpecialtyText"] != null && Session["GenderText"] != null) {

            this.OnBtnClick();
        }
    }

并且在按钮单击事件处理程序上再次调用通用函数

protected void btnGoAll_Click(object sender, EventArgs e) {
this.OnBtnClick();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 2020-12-28
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多