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

时间:2012-05-16 13:53:06

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

我无法在回发后让按钮执行单击操作。我正在验证网页上某个模式窗口中的一些文本框,只有在单击按钮后才会显示。目前,在回发后,网页重新打开,模态窗口关闭,需要打开。我的代码中没有处理程序,单击该按钮并运行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>

1 个答案:

答案 0 :(得分:4)

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

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

修改

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

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