My button opens my modal popup window, but the c# codebehind connected to the button does not fire

StackOverflow https://stackoverflow.com/questions/22408180

  •  14-06-2023
  •  | 
  •  

質問

I have a button that opens a modal popup, but before I open the modal popup, I want my "OnClick" event for the "btnSaveAndScheduleTask" button to fire. I am using ASP.NET 4.5 / Visual Studio 2012 / HTML5 / CSS3

My aspx (snipet):

How do I get the codebehind OnClick event for my "btnSaveAndScheduleTask" button to fire? If the entire code would help figure it out, let me know, but I'm probably missing something simple (bear in mind that I want to be able to view all my asp controls from the C# codebehind):

<asp:Button ID="btnSaveAndScheduleTask" runat="server" CausesValidation="true"
  OnClientClick="javascript:return validatePage();" OnClick="btnSaveAndScheduleTask_Click"
  Font-Bold="true"  Text="Schedule Task"  />

<ajaxToolkit:ModalPopupExtender ID="mpeScheduleTask" runat="server" ValidateRequestMode="Enabled"
  BackgroundCssClass="modalBackground" CancelControlID="btnCancSchedule"
  PopupControlID="pnlScheduleTask" TargetControlID="btnSaveAndScheduleTask"  DropShadow="true" >
</ajaxToolkit:ModalPopupExtender> 


  <div id="divScheduleTask" runat="server">
                <asp:Panel ID="pnlScheduleTask" Height="310" Width="690" BackColor="#ece4e1" ForeColor="Black"  runat="server" >
                    <asp:UpdatePanel runat="server" ID="udpScheduleTask" UpdateMode="Conditional">
                        <ContentTemplate>
             <asp:Label ID="lblTaskSch" Visible="false" Font-Bold="true" Text="Task Scheduling: " runat="server" />
                            <asp:Button ID="btnSaveSchedule" runat="server" OnClick="btnSaveSchedule_Click" Text="Save Schedule" />
                            </div><asp:Button ID="btnCancSchedule" runat="server"  Text="Canc" />
                                </ContentTemplate></asp:UpdatePanel></asp:Panel></div>

I've left out most of the panel as it's huge... here is my validatePage() Javascript:

<script type="text/javascript">
    function validatePage() {
        //Executes all the validation controls associated with group1 validaiton Group1. 
        var flag = window.Page_ClientValidate('vTask');
        if (flag)
            //Executes all the validation controls which are not associated with any validation group. 
            flag = window.Page_ClientValidate();
        if (!Page_IsValid) {
            $find('mpeScheduleTask').hide();
        }
        return flag;
    }
</script>

My aspx.cs code behind:

 protected void btnSaveAndScheduleTask_Click(object sender, EventArgs e)
    {
    //do stuff
    }
役に立ちましたか?

解決 2

I would put the javascript to be called in the behind code in your button click..

protected void btnSaveAndScheduleTask_Click(object sender, EventArgs e)
{
    // do stuff.
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "validatePage();", true);
}

this is how I do most of mine. Hope this helps.

And your ASP should look like..

<asp:Button ID="btnSaveAndScheduleTask" runat="server" CausesValidation="true" OnClick="btnSaveAndScheduleTask_Click" Font-Bold="true" Text="Schedule Task"  />

他のヒント

Remove TargetControlID="btnSaveAndScheduleTask" , give other dummy control's ID So when button clicks, click will take user to server , in code behind you need to manually open popup.

This is given here...!!!!

http://www.codeproject.com/Tips/215040/ModalPopupExtender-from-Server-Side-Code

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top