简体   繁体   English

使用JavaScript和C#在asp:DropDownList中不会更新下拉列表的selectedValue

[英]selectedvalue of dropdown list wont update in asp:DropDownList using javascript and C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (!String.IsNullOrEmpty(Request.QueryString["ApplicationTrackingID"]))
        {
            GetStatusIDByATID(Convert.ToInt32(Request.QueryString["ApplicationTrackingID"]));
        }
    }
    else
    {
        Master.SetPreviousPage(Master.GetMain());

        applicationTrackingID = Request.QueryString["atid"];
        jobTrackingID = Request.QueryString["jtid"];

        // Get Application status
        int.TryParse(applicationTrackingID, out appTrackingID);
        ApplicationSettings applicationStatus = new ApplicationSettings(appTrackingID);
        appStatus = applicationStatus.StatusCode; // get the full status code name

        // Get Job status
        int.TryParse(jobTrackingID, out jobTID);
        jobstatus = getJobStatusByJobTrackingId(jobTID);

        // Get Statement status using ATID
        statementStatus = getStatementStatusByApplicationTrackingID(appTrackingID);

        if (applicationTrackingID != null && jobTrackingID == "")
        {
            txtAppTrackingID_App.Text = applicationTrackingID;
            txtAppTrackingID_Stmt.Text = applicationTrackingID;
            ddlOldStatus_App.SelectedValue = appStatus.ToUpper();
            ddlOldStatus_Stmt.SelectedValue = appStatus.ToUpper();

            if (statementStatus != null)
            {
                ddlOldStatus_Stmt.SelectedValue = statementStatus.ToUpper();
            }
        }
        else if (applicationTrackingID != null && jobTrackingID != null)
        {
            txtAppTrackingID_App.Text = applicationTrackingID;
            txtJobTrackingID_Stmt.Text = jobTrackingID;
            txtJobTrackingID_Job.Text = jobTrackingID;

            // If the TID has run to job process 
            if ((jobstatus != "ERROR"))
            {
                // and is job is not in "ERROR" state
                ddlOldStatus_App.SelectedValue = appStatus.ToUpper();
                ddlOldStatus_Stmt.SelectedValue = jobstatus.ToUpper();
                ddlOldStatus_Job.SelectedValue = jobstatus.ToUpper();
            }
            else
            {
                // job is in "ERROR" state
                // Old ddls of all are set to the "ERROR" status of the job
                ddlOldStatus_App.SelectedValue = jobstatus.ToUpper();
                ddlOldStatus_Stmt.SelectedValue = jobstatus.ToUpper();
                ddlOldStatus_Job.SelectedValue = jobstatus.ToUpper();
            }
        }

        bindrgJobStatus();
    }
}


public void GetStatusIDByATID(int ApplicationTrackingID)
{
    using (SqlConnection sqlConnection = UTSqlConnection.GetSQLConnection("DynaBillDB"))
    {
        sqlConnection.Open();
        var sqlCommand = new SqlCommand("GetAppstatusByATID", sqlConnection);
        sqlCommand.CommandType = CommandType.StoredProcedure;
        SqlParameterCollection parameters = sqlCommand.Parameters;

        // Define the parameters used
        parameters.Add("@AppTrackingID", SqlDbType.Int).Direction = ParameterDirection.Input;
        parameters.Add("@AppStatus", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;

        // Populate the parameters
        parameters["@AppTrackingID"].Value = ApplicationTrackingID;

        sqlCommand.ExecuteNonQuery();

        string output = parameters["@AppStatus"].Value.ToString();
        ddlOldStatus_App.SelectedValue = output;
    }

    ddlOldStatus_App.SelectedValue = "AWAITING_DUPLICATE_FILE";
}

  <script type="text/JavaScript" src="Scripts/jquery-2.2.4.mis.js"></script> <script type="text/javascript"> function GetStatusIDByATID(val) { $.ajax({ type: "GET", url: "ChangeProcessStatus.aspx?ApplicationTrackingID=" + val, contentType: "application/json", dataType: "json", Error: function (x, e) { alert("did not work"); } }); $('#ddlOldStatus_App').val("AWAITING_DUPLICATE_FILE"); } </script> 
 <div class="searchFormNoBox span12"> <span> <label>App Tracking ID</label> <asp:TextBox ID="txtAppTrackingID_App" onblur="GetStatusIDByATID(this.value)" AutoPostBack="true" runat="server" Width="100px"></asp:TextBox> </span> <span> <label>Old Status</label> <asp:DropDownList ID="ddlOldStatus_App" AutoPostBack="true" EnableViewState="true" runat="server" Width="170px"> <asp:ListItem Value="" Selected="True"></asp:ListItem> <asp:ListItem Value="AWAITING_CONF">AWAITING_CONF</asp:ListItem> <asp:ListItem Value="AWAITING_DUPLICATE_FILE">AWAITING_DUPLICATE_FILE</asp:ListItem> <asp:ListItem Value="CANCELLED">CANCELLED</asp:ListItem> <asp:ListItem Value="COMPLETE">COMPLETE</asp:ListItem> <asp:ListItem Value="CONFIRM_APPROVED">CONFIRM_APPROVED</asp:ListItem> <asp:ListItem Value="CONFIRM_REJECTED">CONFIRM_REJECTED</asp:ListItem> <asp:ListItem Value="COPY_FINISHED">COPY_FINISHED</asp:ListItem> <asp:ListItem Value="COPY_STARTED">COPY_STARTED</asp:ListItem> <asp:ListItem Value="DUPLICATE_FILE_APPROVED">DUPLICATE_FILE_APPROVED</asp:ListItem> <asp:ListItem Value="DUPLICATE_FILE_REJECTED">DUPLICATE_FILE_REJECTED</asp:ListItem> <asp:ListItem Value="EMAIL_READY">EMAIL_READY</asp:ListItem> <asp:ListItem Value="EMAIL_COMPLETE">EMAIL_COMPLETE</asp:ListItem> <asp:ListItem Value="ERROR">ERROR</asp:ListItem> <asp:ListItem Value="FILE_PROCESSING">FILE_PROCESSING</asp:ListItem> <asp:ListItem Value="FILE_READY">FILE_READY</asp:ListItem> <asp:ListItem Value="FILE_RECEIVED">FILE_RECEIVED</asp:ListItem> <asp:ListItem Value="HOLD">HOLD</asp:ListItem> <asp:ListItem Value="PROCESSING_APP">PROCESSING_APP</asp:ListItem> <asp:ListItem Value="PROCESSING_QC">PROCESSING_QC</asp:ListItem> <asp:ListItem Value="QUICKCHANGE_READY">QUICKCHANGE_READY</asp:ListItem> <asp:ListItem Value="READY">READY</asp:ListItem> <asp:ListItem Value="SCHEDULED">SCHEDULED</asp:ListItem> <asp:ListItem Value="WEBVIEW_READY">WEBVIEW_READY</asp:ListItem> </asp:DropDownList> </span> 

I'm trying to get the drop down list selected value to be set to a certain value after its corresponding text box has lost focus. 我试图在其对应的文本框失去焦点之后将下拉列表选择的值设置为某个值。 I'm not 100% sure what is happening. 我不确定100%发生了什么事。 I know it's running page_load multiple times and in the end it is not setting the selected value. 我知道它多次运行page_load,最后它没有设置选定的值。

You may notice, in the snippets of code I posted: 您可能会注意到,在我发布的代码片段中:

ddlOldStatus_App.SelectedValue = "AWAITING_DUPLICATE_FILE";

That is because I've already confirmed that the selected Value is being updated in the code (hovering over the SelectedValue in visual studio shows its new value changes to what is right of the = sign) 那是因为我已经确认代码中所选值正在更新(将鼠标悬停在Visual Studio中的SelectedValue ,显示其新值更改为=符号的右边)。

I'm focused on it actually visibly changing on the web page. 我关注的是它实际上在网页上的可见变化。

It's running page_load multiple times because you set AutoPostBack="true" which causes it to post back to the server every time the textbox loses focus. 它多次运行page_load,因为您设置了AutoPostBack="true" ,这导致它在每次文本框失去焦点时都将其回发到服务器。 See https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.autopostback(v=vs.110).aspx . 请参阅https://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.textbox.autopostback(v=vs.110).aspx You also have the same property set on the dropdown list, which will cause it to post back every time the selection changes. 您还在下拉列表上设置了相同的属性,这将导致它在每次更改选择时都回发。

Therefore your JS (and thus your ajax request) never gets chance to run before the page is posted back and the browser renders the fresh page from the server. 因此,在页面回发并且浏览器从服务器渲染新页面之前,您的JS(以及您的ajax请求)将永远没有机会运行。 If you want to do something using JavaScript when an event happens on your elements, then remove the AutoPostBack property, or set it to False . 如果要在元素上发生事件时使用JavaScript进行操作,请删除AutoPostBack属性,或将其设置为False

BTW, not directly related, but making an ajax call to a .aspx page is not a good design, as these pages are designed for rendering whole pages to the browser, not returning data or HTML snippets. 顺便说一句,不是直接相关的,但是对.aspx页面进行ajax调用不是一个很好的设计,因为这些页面旨在将整个页面呈现给浏览器,而不返回数据或HTML代码段。 Your instruction ddlOldStatus_App.SelectedValue = "AWAITING_DUPLICATE_FILE"; 您的指令ddlOldStatus_App.SelectedValue = "AWAITING_DUPLICATE_FILE"; within the C# GetStatusIDByATID method would never work anyway, as the ajax call does not update the page, and doesn't run in a full page rendering context. C#中的GetStatusIDByATID方法将永远无法正常工作,因为ajax调用不会更新页面,也无法在完整的页面呈现上下文中运行。 It might even throw an exception. 它甚至可能引发异常。 You should use a separate webservice (eg a WebMethod or a .svc WCF HTTP data service) as the endpoint instead. 您应该使用单独的Web服务(例如WebMethod或.svc WCF HTTP数据服务)作为端点。 I think perhaps you should also familiarise yourself more thoroughly with how AJAX works and the key concepts. 我认为也许您还应该更全面地熟悉AJAX的工作原理和关键概念。

What you are doing is calling the entire ASPX Page from client-side JavaScript. 您正在做的是从客户端JavaScript调用整个ASPX页面。

ASP.NET Web Form is not designed that way like ASP.NET MVC or Web API. ASP.NET Web窗体的设计方式不像ASP.NET MVC或Web API。 Instead, you need a static WebMethod like this - 相反,你需要一个静态的WebMethod这样 -

[WebMethod]
public static string GetStatusIDByATID(int ApplicationTrackingID)
{
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM