简体   繁体   中英

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

Serverside :

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

Edit :

Since you are using jquery as well, you can try the following to show the modal:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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