【问题标题】:Two panels with forms that switch based on radio button带有基于单选按钮切换的表单的两个面板
【发布时间】:2016-04-13 10:29:48
【问题描述】:

我有 2 个面板,每个面板都有一个表单。它们最初是隐藏的,单选按钮选择将使 1 个面板可见。一种用于信用卡支付,另一种用于电子支票。该代码适用于此,但有一个故障。 echeck 选项首先在提交时清除表单,然后将在第二次单击时提交表单。

第一次被单选按钮选中时,如何让第二个面板(检查)提交表单?它是否与 Page.IsPostBack 或我的 if 语句在 Page_Load 部分中的方式有​​关?

这是 2 个单选按钮

  <asp:RadioButton ID="RadioButton1" runat="server"  GroupName="RbPayment" OnCheckedChanged="RadioButton1_CheckedChanged" Text="Credit Card" Checked="True" AutoPostBack="True" /> 
  <asp:RadioButton ID="RadioButton2" runat="server" GroupName="RbPayment" OnCheckedChanged="RadioButton2_CheckedChanged" Text="Check" AutoPostBack="True" />

这是我有 2 个面板和表单的地方

<asp:Panel ID="PanelCard" runat="server" Visible="false" OnLoad="RadioButton1_CheckedChanged">
       <%--Form here--%>
       <asp:Button ID="Button1" runat="server" Text="Submit Payment" ClientIDMode="Static" CssClass="formbutton" />
</asp:Panel>
<asp:Panel ID="PanelCheck" runat="server" Visible="false" OnLoad="RadioButton1_CheckedChanged">
       <%--Form here--%>
       <asp:Button ID="Button1" runat="server" Text="Submit Payment" ClientIDMode="Static" CssClass="formbutton" />
</asp:Panel>

我在选择单选按钮时,我有此代码在面板之间交换

protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        if (RadioButton1.Checked)
        {
            PnlCard.Visible = true;
            PnlCheck.Visible = false;
        }

        if (RadioButton2.Checked)
        {
            PnlCard.Visible = false;
            PnlCheck.Visible = true;
        }

    }

    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        if (RadioButton1.Checked)
        {
            PnlCard.Visible = true;
            PnlCheck.Visible = false;
        }

        if (RadioButton2.Checked)
        {
            PnlCard.Visible = false;
            PnlCheck.Visible = true;
        }
    }

这是我认为问题所在,但我不确定需要修复什么

  protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BtnSubmitCard.PostBackUrl = "https://secure2.authorize.net/gateway/transact.dll";
            //Other code here for the form
            }

        if (PnlCard.Visible == true)
        {
            BtnSubmitCard.PostBackUrl = "https://secure2.authorize.net/gateway/transact.dll";
            //Other code here for the form
        }

        if (PnlCheck.Visible == true)
        {
            BtnSubmitCheck.PostBackUrl = "https://secure2.authorize.net/gateway/transact.dll";
           //Other code here for the form
        }
    }

【问题讨论】:

  • 这听起来像是一个IsPostBack 问题。也许您可以使用 UpdatePanel 来执行此操作,而不是执行部分回发
  • 这些表单是服务器端元素吗? asp.net 每页只能处理 1 个服务器端表单。这会导致您的问题吗?
  • @MethodMan 谢谢,我刚试过,但它不起作用,因为我使用了一些“输入”字段(由于支付网关的安全性)。
  • @Sami 两种表格都正确提交。只是在页面加载时,第二个面板的提交按钮会在第一次单击时清除表单。在第二次点击时,它提交表单就好了。
  • 您是否已启用 viewstate 设置为 true..?或许您可以将值存储在会话或视图对象中并在该负载上重新分配

标签: c# asp.net


【解决方案1】:

您的代码方面似乎很可靠,但正如第一条评论所建议的那样,这确实是一个回发问题。您需要为每个部分使用 UpdatePanel 来动态更改可见性,如下所示:

<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Panel ID="PanelCard" runat="server" Visible="false" OnLoad="RadioButton1_CheckedChanged">
   <%--Form here--%>
   <asp:Button ID="Button1" runat="server" Text="Submit Payment" ClientIDMode="Static" CssClass="formbutton" />
   </asp:Panel>
   <asp:Panel ID="PanelCheck" runat="server" Visible="false" OnLoad="RadioButton1_CheckedChanged">
   <%--Form here--%>
   <asp:Button ID="Button1" runat="server" Text="Submit Payment" ClientIDMode="Static" CssClass="formbutton" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>

您也可以选择使用两个单独的更新面板,而不是一个大面板,只需将每个标准面板包装在一个更新面板中即可。这将减少每次回传的数据量。

编辑:如果页面不存在,您还需要向页面添加脚本管理器。

【讨论】:

  • 谢谢。有没有办法使用“输入”字段?我不能使用 asp 字段(asp 文本框等),因为它提交给服务器,这会给支付带来安全风险。
  • @SummerDays 看看这个例子stackoverflow.com/questions/389149/…
  • 谢谢!让它工作。我还添加了更新面板所需的&lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt;&lt;/asp:ScriptManager&gt;
  • 另外,我必须在 Page_Load 中再添加一行。我需要if (!Page.IsPostBack) 下列出的两个按钮,所以我添加了 BtnSubmitCheck。
  • 添加了关于添加脚本管理器的注释,以防其他人将来偶然发现此问题。
猜你喜欢
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多