简体   繁体   English

回发后如何执行按钮单击以打开模式窗口

[英]How to perform a button click after a postback to open a modal window

I am having trouble with making my button perform a click after a postback. 我在使按钮在回发后执行点击时遇到麻烦。 I am validating some textboxes within a modal window on a web page, which only appears after a button click. 我正在验证网页上的模式窗口内的一些文本框,该文本框仅在单击按钮后出现。 Currently after the postback the web page re-opens and the modal window is closed, which is required to be open. 当前,在回发之后,网页将重新打开,并且模式窗口已关闭,需要打开该窗口。 There is no handler in my code, the button is clicked and runs html code to bring up the modal window. 我的代码中没有处理程序,单击该按钮并运行html代码以打开模式窗口。 I need this button to perform a click once i have posted back so that the validation is initiated. 一旦我发回邮件,就需要单击此按钮,以便启动验证。 I have tried using btnSickness.Click() but it does not seem to like this and can't seem to find anything anywhere! 我尝试使用btnSickness.Click(),但它似乎不喜欢这种方法,而且似乎在任何地方都找不到! Code: 码:

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++;
        }
    }
}

Modal window code: 模态窗口代码:

<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>

You can set a hidden field to tell the modal to show when you return from the server. 您可以设置一个隐藏字段,告诉模态从服务器返回时显示。 Then you can add a pageLoad javascript function, which runs every time the pageLoads, to check if you need to show the modal. 然后,您可以添加一个pageLoad javascript函数,该函数在每次pageLoad时运行,以检查是否需要显示模式。

Serverside : 服务器端

hdf_ShowModal.Value = "true";

HTML : HTML

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

Javascript : Javascript

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

Edit : 编辑

Since you are using jquery as well, you can try the following to show the modal: 由于还使用了jquery,因此可以尝试以下操作来显示模式:

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

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

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