下拉列表的选定值不会在asp:DropDownList中使用javascript和C#更新

时间:2017-12-27 21:34:54

标签: javascript c# asp.net ajax

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>
&#13;
<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>
&#13;
&#13;
&#13;

我试图让下拉列表选择值在其相应的文本框失去焦点后设置为某个值。我不是100%确定发生了什么。我知道它多次运行page_load,最后它没有设置选定的值。

你可能会注意到,在我发布的代码片段中:

ddlOldStatus_App.SelectedValue = "AWAITING_DUPLICATE_FILE";

这是因为我已经确认所选的值正在代码中更新(在视觉工作室中悬停在SelectedValue上显示其新值更改为=符号的右侧)< / p>

我专注于它实际上在网页上明显改变。

2 个答案:

答案 0 :(得分:0)

它多次运行page_load,因为你设置ID_Type | BUYER | SELLER ------------------------------------------------ 1 | | Joe 0 | Peter | 0 | Peter | 1 | Sam | 0 | Peter | 0 | | Mark 1 | Tai | 1 | Tai | 0 | | Mark 会导致每次文本框失去焦点时都回发到服务器。见https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.autopostback(v=vs.110).aspx。您还在下拉列表中设置了相同的属性,这将使其在每次选择更改时回发。

因此,您的JS(以及您的ajax请求)在页面回发之前永远不会有机会运行,浏览器会从服务器呈现新页面。如果您想在元素上发生事件时使用JavaScript执行某些操作,请删除AutoPostBack属性,或将其设置为AutoPostBack="true"

BTW,没有直接相关,但是对.aspx页面进行ajax调用并不是一个好的设计,因为这些页面是为了将整个页面呈现给浏览器而不是返回数据或HTML片段而设计的。 C#False方法中的指令ddlOldStatus_App.SelectedValue = "AWAITING_DUPLICATE_FILE";无论如何都不会起作用,因为ajax调用不会更新页面,也不会在整页渲染上下文中运行。它甚至可能会抛出异常。您应该使用单独的Web服务(例如WebMethod或.svc WCF HTTP数据服务)作为端点。我想也许您应该更熟悉AJAX的工作方式和关键概念。

答案 1 :(得分:0)

您正在做的是从客户端JavaScript调用整个ASPX页面。

ASP.NET Web窗体的设计方式与ASP.NET MVC或Web API不同。相反,您需要一个静态 WebMethod ,如this -

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