【问题标题】:How to perform a button click after a postback to open a modal window如何在回发后执行按钮单击以打开模式窗口
【发布时间】:2012-05-24 02:31:03
【问题描述】:

我无法让我的按钮在回发后执行点击。我正在验证网页上模态窗口中的一些文本框,这些文本框仅在单击按钮后出现。目前回发后网页重新打开,模式窗口关闭,需要打开。我的代码中没有处理程序,单击按钮并运行 html 代码以调出模态窗口。一旦我发回消息,我需要这个按钮来执行点击,以便启动验证。我曾尝试使用 btnSickness.Click() 但它似乎不喜欢这样,而且似乎在任何地方都找不到任何东西!代码:

public partial class _Default : System.Web.UI.Page
{

    int i = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (i > 0)
           {
           }      

    }

    protected void chkDoctor_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpDoctor.SelectedValue == "Yes")
        {
            txtIfNoWhy.ReadOnly = true;
            txtIfNoWhy.BackColor = Color.LightGray;
            i++;
        }
        else if (drpDoctor.SelectedValue == "No")
        {
            txtDocName.ReadOnly = true;
            txtHouseName.ReadOnly = true;
            txtStreet.ReadOnly = true;
            txtTownCity.ReadOnly = true;
            txtCounty.ReadOnly = true;
            txtPostalcode.ReadOnly = true;
            txtInitialDate.ReadOnly = true;
            txtTreatmentRecieved.ReadOnly = true;
            txtCurrentTreatment.ReadOnly = true;

            txtDocName.BackColor = Color.LightGray;
            txtHouseName.BackColor = Color.LightGray;
            txtStreet.BackColor = Color.LightGray;
            txtTownCity.BackColor = Color.LightGray;
            txtCounty.BackColor = Color.LightGray;
            txtPostalcode.BackColor = Color.LightGray;
            txtInitialDate.BackColor = Color.LightGray;
            txtTreatmentRecieved.BackColor = Color.LightGray;
            txtCurrentTreatment.BackColor = Color.LightGray;
            i++;
        }
    }
}

模态窗口代码:

<div class"modal" id="myModal"></div> 
    <div class="row-fluid">
        <div class="span2">
                <asp:Button runat="server" class="btn" data-toggle="modal" href="#Div1" ID="btnSickness" Text="Submit a Sickness Form" />
                       <div class="modal hide" id="Div1">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3>Sickness Form</h3>
</div>
<div class="modal-body">
<p>Please fill in the following information regarding your sickness</p>
<br />
<p>Sickness Date From:</p>
<asp:TextBox runat="server" ID="txtSicknessFrom"></asp:TextBox>
<br />
<p>Sickness Date To:</p>
<asp:TextBox runat="server" ID="txtSicknessTo"></asp:TextBox>
    <br />
<p>Absence Date To:</p>
<asp:TextBox runat="server" ID="txtAbsenceFrom"></asp:TextBox>

【问题讨论】:

  • if (i &gt; 0) => i 永远不会 != 0 由于 http 的无状态。除此之外,我不明白这个问题。 “以便启动验证” 什么验证?如果您需要在服务器端调用 Page.Validate() 触发验证。
  • 验证文本框只读。但要做到这一点,需要回发
  • SelectedIndexChanged 事件是服务器端事件。它永远不会在没有回发的情况下被调用,i 是一个在页面呈现后立即被释放的变量,它不会在回发中存活,并且总是以 0 初始化。
  • 好的,我只需要在回发后执行点击的帮助。
  • 这个模态是通过javascript显示的吗?

标签: c# asp.net html twitter-bootstrap


【解决方案1】:

您可以设置一个隐藏字段来告诉模态框在您从服务器返回时显示。然后您可以添加一个 pageLoad javascript 函数,该函数在每次 pageLoads 时运行,以检查您是否需要显示模态。

服务器端

hdf_ShowModal.Value = "true";

HTML

<asp:HiddenField runat="server" ID="hdf_ShowModal" />

Javascript

function pageLoad(sender, args)
{
    if(document.getElementById('<%= hdf_ShowModal.ClientID %>').value == "true")
    {
        // perform code to show modal
    }
}

编辑

由于您也使用 jquery,您可以尝试以下方法来显示模态:

function pageLoad(sender, args)
{
    if($('[id$=hdf_ShowModal]').val() == "true")
        $('#myModal').modal({ show: true });
}

【讨论】:

猜你喜欢
  • 2015-09-09
  • 1970-01-01
  • 1970-01-01
  • 2018-07-05
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多