简体   繁体   English

未从C#函数ASP.Net触发Javascript函数

[英]Javascript Function Not Firing from C# Function ASP.Net

I am using a asp:Repeater control while trying to develop a screen as shown below: Submit form 尝试开发如下屏幕时,我正在使用asp:Repeater控件: 提交表单

When ToolId is entered on the TextChanged Event I am getting value for MaxToolLife stored as double. 当在TextChanged事件上输入ToolId时,我将MaxToolLife的值存储为两倍。 When an user enter values greater than the stored value for the field ToolLife I need to show Yes/No popup stating "The value entered is greater than existing value. Do you want to proceed?" 当用户输入的值大于ToolLife字段的存储值时,我需要显示“是/否”弹出窗口,说明“输入的值大于现有值。您要继续吗?” on button submit or textchanged event. 按钮提交或文本更改事件。 Currently I am using the below code but I am not getting Javascript alert. 目前,我正在使用以下代码,但没有收到Javascript警报。

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (Convert.ToDouble(txtToolLifeAchieved.Text) > maxToolLife)
    {
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "Confirm", "javascript:Confirm();", true);

        if (hdnconfirm.Value=="Yes")
        {
            row["Pcs Produced"] = Convert.ToDouble(txtToolLifeAchieved.Text);
        }
        else
        {
            txtToolLifeAchieved.Text = "1";
            txtToolLifeAchieved.Focus();
            return;
        }
    }
    else
    {
        row["Pcs Produced"] = Convert.ToDouble(txtToolLifeAchieved.Text);
    }
}

In place of Page.ClientScript I have also used "Page.ClientScript.RegisterClientScriptBlock" & /ScriptManager.RegisterStartupScript. 代替Page.ClientScript,我还使用了“ Page.ClientScript.RegisterClientScriptBlock”和/ScriptManager.RegisterStartupScript。 Nothing is working as of now. 到目前为止,没有任何工作。 I am unable to call the javascript function from code behind. 我无法从后面的代码调用javascript函数。 Any immediate response would help a lot. 任何立即的反应都会有很大帮助。

The confirm() is as shown below: Confirm()如下所示:

 function Confirm() { var confirm_value = document.createElement("INPUT"); confirm_value.type = "hidden"; confirm_value.name = "confirm_value"; if (confirm("The data already exists. Do you want to Overwrite data?")) { confirm_value.value = "Yes"; document.getElementById('<%= hdnconfirm.ClientID %>').value = "Yes"; } else { confirm_value.value = "No"; document.getElementById('<%= hdnconfirm.ClientID %>').value = "No"; } document.forms[0].appendChild(confirm_value); } 

So, I will stick to your original code, and this is how your server method should look like: 因此,我将坚持您的原始代码,这就是您的服务器方法应如下所示:

// This is just to illustrate the limit
private const double MaxToolLife = 100;


protected void btnSubmit_Click(object sender, EventArgs e)
{
    // Check the limit first
    if (Convert.ToDouble(txtToolLifeAchieved.Text) > MaxToolLife)
    {
        // If the hidden field is empty show the confirm
        // By default the hidden field is empty, it means that user just
        // pressed the submit button but not the confirmation dialog
        if (string.IsNullOrWhiteSpace(hdnconfirm.Value))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Confirm", "Confirm();", true);
            return;
        }

        // If the hidden field is not empty, this postback was made by the confirm
        // So check for the value of the hidden field
        if (hdnconfirm.Value == "Yes")
        {
            // Handle 'Yes'
        }
        else
        {
            // Handle 'No'
        }
    }
    else
    {
        // The limit is not reached
    }
}

I omitted your specific code above. 我在上面省略了您的特定代码。 This is how the form looks like: 表单如下所示:

<form id="form1" runat="server">
    <!-- Hidden field for the confirm result -->
    <asp:HiddenField ID="hdnconfirm" runat="server" />
    <!-- Text box for user input -->
    <asp:TextBox ID="txtToolLifeAchieved" runat="server"></asp:TextBox>
    <!-- Submit button -->
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</form>

Than right before the form tag in your page, place your javascript function, it must be place before the form tag, because calling ClientScript.RegisterStartupScript will append the script at the end of your form tag and that time your Confirm() method be defined. 比对的前form在页面标签,把你的JavaScript函数,它必须是形式标记之前的地方,因为调用ClientScript.RegisterStartupScript将在您的末尾添加脚本form标记和你那个时候Confirm()方法来定义。 And this is your javascript method: 这是您的javascript方法:

<script type="text/javascript">
    function Confirm() {
        if (confirm("The data already exists. Do you want to Overwrite data?")) {
            document.getElementById('<%= hdnconfirm.ClientID %>').value = "Yes";
        }
        else {
            document.getElementById('<%= hdnconfirm.ClientID %>').value = "No";
        }

        // Post the form back to server using the '__EVENTTARGET' hidden field
        var form = document.getElementById("form1");

        // Create hidden field
        var input = document.createElement("input");
        input.setAttribute("type", "hidden");
        input.setAttribute("name", "__EVENTTARGET");
        input.setAttribute("id", "__EVENTTARGET");
        input.setAttribute("value", '<%= btnSubmit.ClientID %>');

        // Append input to the form element
        form.appendChild(input);

        // Submit the form
        form.submit();
    } 
</script>

There is no need to create another hidden field for the confirmation result. 无需为确认结果创建另一个隐藏字段。 Instead just right after the confirm you need to create hidden field with name set to __EVENTTARGET , and the value of that hidden field must be the name of the submit button, and than just call submit method on the form. 而是恰到好处的确认后,您需要创建的隐藏字段name设置为__EVENTTARGET ,而隐藏字段的值必须是提交按钮的名称,并不仅仅是调用submit表单上的方法。 This will case postback to the server, with the hdnconfirm set to Yes or No while your btnSubmit_Click method will be executed. 这会将案例回发到服务器,同时hdnconfirm设置为YesNo,同时执行btnSubmit_Click方法。

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

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