C#DropDownList SelectedValue是否已设置但未保存?

时间:2018-08-21 22:40:16

标签: c# asp.net dropdownbox

我觉得我确实缺少一些明显的东西。 :)

C#ASPX页面,已在最新版本的Chrome中进行测试。

以下是相关代码:

MyItems

JobSearch.aspx.cs(相关部分):

<plugins>
  <plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
      <instructions>
        <Import-Package>
        org.apache.aries.blueprint.web,
        *
        </Import-Package>
      </instructions>
    </configuration>
  </plugin>
</plugins>

问题:例如,当我运行 http:// server / Jobs / JobSearch?customerid = 22 时,应该将customerID 22加载到下拉列表中顾客。然后应该对该客户进行搜索。它做第一件事,但不做第二件事。

也就是说,我可以看到ID为22的客户已在下拉列表中被自动选择。但是,当我逐步执行PageLoad例程时,它会运行<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="JobSearch.aspx.cs" Inherits="GCWebForms.Jobs.JobSearch" %> <asp:Content ID="HeadContent" ContentPlaceHolderID="Head" runat="server"> <style> a img{border: none;} ol li{list-style: decimal outside;} div#container{width: 780px;margin: 0 auto;padding: 1em 0;} div.side-by-side{width: 100%;margin-bottom: 1em;} div.side-by-side > div{float: left;width: 50%;} div.side-by-side > div > em{margin-bottom: 10px;display: block;} .clearfix:after{content: "\0020";display: block;height: 0;clear: both;overflow: hidden;visibility: hidden;} </style> <link rel="stylesheet" href="../Content/chosen.css" /> </asp:Content> <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <div class="body-content"> <div id="searchform" class="simplelayoutform"> <div> <div> <asp:label id="lblCustomerSearch" runat="server" Text="Jobs for Customer:" AssociatedControlID="cboCustomerID" /> <asp:DropDownList ID="cboCustomerID" runat="server" class="chzn-select" DataSourceID="DataSourceCustomer" DataTextField="CustomerName" DataValueField="CustomerID"> </asp:DropDownList> <gcctl:MyCheckBox ID="chkCustomersShowInactive" Text="Show Inactive?" Checked="false" AutoPostBack="true" runat="server" /> </div> </div> [[cut - search results go here]] </div> <asp:SqlDataSource ID="DataSourceCustomer" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnString %>" SelectCommand="SELECT NULL AS [CustomerID] , NULL AS [CustomerName] UNION SELECT [CustomerID] ,[CustomerName] FROM [dbo].[Customers] WHERE ((@ShowInactive = 0 AND IsActive = 1) OR (@ShowInactive = 1)) ORDER BY CustomerName"> <SelectParameters> <asp:ControlParameter Name="ShowInactive" Type="Boolean" ControlID="chkCustomersShowInactive" PropertyName="Checked" /> </SelectParameters> </asp:SqlDataSource> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="JQScripts" runat="server"> <script type="text/javascript"> function fixform() { if (opener.document.getElementById("PageForm").target != "_blank") return; opener.document.getElementById("PageForm").target = ""; opener.document.getElementById("PageForm").action = opener.location.href; } </script> <script src="../Scripts/chosen.jquery.js" type="text/javascript"></script> <script type="text/javascript"> $(".chzn-select").chosen(); $(".chzn-select-deselect").chosen({ allow_single_deselect: true }); </script> </asp:Content>,并在其中设置namespace GCWebForms.Jobs { public partial class JobSearch : System.Web.UI.Page { int? iCustomerID; int? iStaffID; bool bolIsPostBack = false; //-------- protected void SetIDs() { this.iCustomerID = null; this.iStaffID = null; if (Request.QueryString["customerid"] != null) //new customer { try { this.iCustomerID = Convert.ToInt32(Request.QueryString["customerid"]); } catch { } } if (Request.QueryString["staffid"] != null) //new customer { try { this.iStaffID = Convert.ToInt32(Request.QueryString["staffid"]); } catch { } } if (iCustomerID != null) { this.cboCustomerID.SelectedValue = this.iCustomerID.ToString(); } if (iStaffID != null) { this.cboStaffID.SelectedValue = this.iStaffID.ToString(); } } //-------- protected void DoSearch() { //gvJobsDataSource.SelectParameters["CustomerSearch"].DefaultValue = cboCustomerID.SelectedValue.ToString(); //gvJobsDataSource.SelectParameters["StaffSearch"].DefaultValue = cboStaffID.SelectedValue.ToString(); //gvJobsDataSource.SelectParameters["ShowClosed"].DefaultValue = chkShowClosed.Checked.ToString(); if(string.IsNullOrEmpty(this.cboCustomerID.SelectedValue.ToString()) && string.IsNullOrEmpty(this.cboStaffID.SelectedValue.ToString())) { SearchPrompt.Visible = this.bolIsPostBack; gvJobs.DataSourceID = "gvJobsDataSourceInit"; } else { SearchPrompt.Visible = false; gvJobs.DataSourceID = "gvJobsDataSource"; } gvJobs.DataBind(); } //-------- protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SetIDs(); DoSearch(); } this.bolIsPostBack = true; } //-------- protected void btnSearch_Click(object sender, EventArgs e) { DoSearch(); },但是当它运行SetIDs()并将手表放在this.cboCustomerID.SelectedValue = '22'上时,它将返回DoSearch(),因此不会运行搜索。

当我仅将this.cboCustomerID.SelectedValue.ToString()的值设置为''时为什么返回this.cboCustomerID.SelectedValue.ToString()

非常感谢!

0 个答案:

没有答案