【问题标题】:Retrieve HiddenField Server Side检索 HiddenField 服务器端
【发布时间】:2022-07-07 01:48:32
【问题描述】:

我发现了几篇相似但不完全是我想要做的帖子。我的 aspx 中有一个保存按钮。我在事件处理程序中有一些逻辑来检查某些条件,如果它们满足,那么我需要一个弹出窗口要求确认以继续。由于这是在一些处理之后发生的,而不是在单击按钮后立即发生的,所以我在后端调用它。

cs

Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "MyFunction()", true);

JavaScript

<script type="text/javascript">
function MyFunction() {
    if (confirm("Do you want to continue?") == true) {
        document.getElementById('<%=HiddenField1.ClientID %>').value = "True";
    } else {
        document.getElementById('<%=HiddenField1.ClientID %>').value = "False";
    }
}    
</script>

aspx

&lt;asp:HiddenField ID="HiddenField1" runat="server"/&gt;


这一切都很好。我接下来需要做的是在后端我需要基于 HiddenField 进行额外处理:

if (HiddenField1.Value == "True")
{
    FinishProcessing();
}
else
{
    // Do nothing
}

问题是我必须单击按钮两次才能获得结果,就像我需要回发才能获得 HiddenValue。我不希望使用 Server.Transfer 进行回发,因为我需要在表单上保留几个元素,尽管我认为 UpdatePanel 可以解决这个问题。

我尝试过的替代代码:

JavaScript(在警报中显示正确的值,但存在与上述相同的行为)

<script type="text/javascript">
function MyFunction() {
    if (confirm("Do you want to continue?") == true) {
        document.getElementById('<%=HiddenField1.ClientID %>').value = "True";
        alert(document.getElementById('<%=HiddenField1.ClientID%>').value);
        document.getElementById('form1').submit(); 
    } else {
        document.getElementById('<%=HiddenField1.ClientID %>').value = "False";
        alert(document.getElementById('<%=HiddenField1.ClientID%>').value);
        document.getElementById('form1').submit(); 
    }
}    
</script>

aspx

<asp:UpdatePanel ID="UpdatePanelHidden" runat="server">
    <ContentTemplate>
        <asp:HiddenField ID="HiddenField1" runat="server"/>
    </ContentTemplate>
</asp:UpdatePanel>

两组代码都产生相同的问题。我做错了什么?

【问题讨论】:

    标签: javascript c# asp.net


    【解决方案1】:

    解决此问题的最佳方法是在页面上放置一个平面 jane asp.net 按钮。

    然后,当 js 例程返回 true 时,您可以让按钮单击(服务器端代码)有条件地运行。

    所以,说一些标记和一个看起来像这样的按钮:

    三个按钮是:

          <asp:Button ID="cmdSave" runat="server" Text="Save" CssClass="btn" />
          <asp:Button ID="cmdCancel" runat="server" Text="Cancel" CssClass="btn" style="margin-left:10px" />
          <asp:Button ID="cmdDelete" runat="server" Text="Delete" CssClass="btn" style="margin-left:20px" ClientIDMode="Static"
               OnClick="cmdDelete_Click"  />
          
          <asp:Button ID="cmdDelete2" runat="server" Text="Delete" CssClass="btn" style="margin-left:20px" ClientIDMode="Static"
              OnClientClick="return confirm('Delete this reocrd');" OnClick="cmdDelete2_Click"              
              />
    

    注意客户端点击事件。如果js代码返回false,则stub后面的代码没有运行。

    这表明如果您可以检查客户端,然后让您的 js 代码返回 true 或 false,那么这将控制后面的服务器端代码是否运行。

    现在,如果您必须运行服务器端按钮代码,然后进行确认?这比较困难,但建议您需要有两个按钮。第一次单击按钮,服务器端代码运行,检查条件,然后在 js 中弹出一个确认对话框 - 如果他们接受是,那么第二个按钮代码可以/将触发。

    (因此,可以隐藏第二个按钮,style=display:none)。

    因此,在上面,我们将添加第二个删除按钮

    所以,点击第一个按钮,这段代码可能会运行:

    所以,我们现在有了这个:

    两个按钮的代码是这样的:

        protected void cmdDelete_Click(object sender, EventArgs e)
        {
            // do whatever checking you like, then 
            // prompt user
    
            string strJs = "$('#cmdDelete2').click();";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myprompt", strJs, true);
    
    
        }
    
        protected void cmdDelete2_Click(object sender, EventArgs e)
        {
            Debug.Print("delete code will run");
    
        }
    

    所以,如果可能的话,我会尝试在客户端进行测试 - 这样我们只需要一个按钮。

    但是,如上所示,用户单击第一个删除按钮。我们现在可以运行代码来遛狗——做任何事情,然后有条件地弹出一个对话框。该对话框将单击第二个按钮,如果用户点击确定确认,则代码将继续在该第二个例程中运行。

    如前所述,如果您可以在第一次单击按钮时进行测试,则让其客户端单击返回 true 或 false,然后将有条件地运行该服务器端代码。

    但是,如果您不能进行客户端测试,那么我们需要 + 使用上述两个按钮,并且如上所述,我们可以使用 style=display:none 隐藏第二个按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-05
      • 2015-04-13
      • 1970-01-01
      • 2011-12-05
      • 2014-06-10
      • 1970-01-01
      • 2020-05-17
      • 2018-01-07
      相关资源
      最近更新 更多