ASP.NET AJAX Control Toolkit: Show a ModalPopup and then do PostBack - c#

I want to show a modal popup when a user click on an asp button. The user must select an option of a panel. The value of the option selected must be saved to an input hidden and then the asp.net button must do a PostBack.
Can I do that?
Thank you!

It is possible for a ModalPopupExtender to be displayed using a postback. You'll need an invisible target control. The extender is attached to this hidden control.
<asp:Button runat="server" ID="btnShowModal" Text="Show"
OnClick="btnShowModal_Click" />
<asp:Button runat="server" ID="HiddenForModal" style="display: none" />
<ajaxToolKit:ModalPopupExtender ID="Modal1" runat="server"
TargetControlID="HiddenForModal" PopupControlID="PopupPanel" />
In your message handler in code-behind, you'll show the ModalPopupExtender:
Modal1.Show();
And in the code you're using to dismiss the Modal, call the ModalPopupExtender's Hide function:
Modal1.Hide();
I use this method for showing a modal that displays detailed data that I retrieve from a database based on what's selected in a GridView.

Add your Button or LinkButton
<asp:Button ID="MyButton" Text="Click Here" runat="server" />
Add a Panel to hold your options with a DropDownList
<asp:Panel ID="MyPanel" runat="server">
<asp:DropDownList ID="MyDropDown" runat="server">
<asp:ListItem Value="1" Text="Option 1" />
</asp:DropDownList>
<asp:Button ID="SaveBtn" Text="Save" OnClick="Save_Click" runat="server" />
<asp:Button ID="CancelBtn" Text="Cancel" runat="server" />
</asp:Panel>
Add your ModelPopupExtender
<act:ModalPopupExtender ID="Mpe1" TargetControlID="MyButton"
CancelControlID="CancelBtn" PopupControlID="MyPanel" runat="server" />
Then add your code behind to the SaveBtn Button
public void SaveBtn_Click(object sender, EventArgs e) {
string selectedOption = MyDropDown.SelectedValue;
}

Finally, I've decided to use jQuery to show a ModalPopUp. The following question has the answer to this question:
jQuery UI's Dialog doesn't work on ASP.NET
If you are not agree, tell me.

Related

When I use asp LoginView Role for Button then get an error

I am using the below code in ASP.NET to hide action buttons for simple users who can see only data but can't change it;
<asp:LoginView ViewStateMode="Disabled" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="HRAdmin">
<ContentTemplate>
<asp:Button ID="BtnAddnew" CssClass="bluebutton" title="Click to Add New Employee" runat="server" Text="Add New" OnClick="BtnAddnew_Click" />
<asp:Button ID="BtnSaveNew" CssClass="bluebutton" title="Click to Save New Record" runat="server" Text="Save New" Visible="false" OnClick="BtnSaveNew_Click" />
<asp:Button ID="BtnUpdate" CssClass="bluebutton" title="Click to Update Record" runat="server" Text="Update" OnClick="BtnUpdate_Click" />
<asp:Button ID="BtnSaveUpdate" CssClass="bluebutton" title="Click to Save Record" runat="server" Text="Save Update" Visible="false" OnClick="BtnSaveUpdate_Click" />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
But when I load page or build project, then I get errors:
The name 'BtnAddnew' does not exist in the current context.
The name 'BtnSaveNew' does not exist in the current context.
The name 'BtnUpdate' does not exist in the current context.
The name 'BtnSaveUpdate' does not exist in the current context.
It means in the C# code it is not loading OnClick event of these buttons. How can I fix this problem?
This suggests that you don't in code behind
have the button click events.
Try removing the this part of each button:
So, change:
<asp:Button ID="BtnAddnew" CssClass="bluebutton"
title="Click to Add New Employee" runat="server"
Text="Add New" OnClick="BtnAddnew_Click" />
to:
<asp:Button ID="BtnAddnew" CssClass="bluebutton"
title="Click to Add New Employee" runat="server"
Text="Add New" />
Now, do a build-rebuild Solution
If you want to add a click event to each button, then in markup, type on OnClick=, and THEN let intel-sense suggest to you to create new event (a click event)
eg this:
It will seem like nothing occurred, but if you flip over to code behind, then the button click event should now exist.
In fact, before you try any of above, exit VS. Re-load. Do a bio;d ->rebuild solution. Then try the page - it it still don't work, then you need to do above.
you should check you aspx page cs refrence class.
<%# Page Title="" Language="C#" MasterPageFile="~/Test/Test/Test.master" AutoEventWireup="true" CodeFile="Test_Update.aspx.cs" Inherits="ProjectName_FolderName_Test_Update" %>
This is because I am using <asp:LoginView and buttons was inside it.That's why you may use below code:
Button BtnAddnew = ((Button)(this.LVButton.FindControl("BtnAddnew")));
Button BtnSaveNew = ((Button)(this.LVButton.FindControl("BtnSaveNew")));
Button BtnUpdate = ((Button)(this.LVButton.FindControl("BtnUpdate")));
Button BtnSaveUpdate = ((Button)(this.LVButton.FindControl("BtnSaveUpdate")));

Duplicate web form on button click

I have an ASP web form that includes divs, checkboxes, textboxes, buttons and dropdowns. Backend is c#.
I have an "Add" button at the bottom that when clicked, I want it to duplicate my form below the existing one.
Other than copying my form multiple times and toggling visibility based on a button click, is there some way I can put my form in a class and call on it based on the button click? Or any other way? Would think gridview is an option but I use divs within the form which is not allowed in gridview.
For Example:
<form id="form" runat="server">
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="updatepnl" runat="server">
<ContentTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Button ID="add" runat="server" Text="Add" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
The Add button can be moved outside of the update panel if necessary. But I would like to duplicate the form below the original via clicking the Add button. Would also be handy if I could have a remove option as well.

UserControl CancelControlID working for all other controls?

Hi i have created one User control which is a Popup , It is absolutely working fine , But when i click on other buttons which is other than close button it is getting closed..
Here is my code,
<ajax:ModalPopupExtender ID="ModalPopupContextInfo" runat="server"
TargetControlID="btnShowEditContextPopup" PopupControlID="pnlpopupContextInfo"
CancelControlID="imgClose" BackgroundCssClass="modalBackground">
</ajax:ModalPopupExtender>
< asp:Panel ID="pnlpopupContextInfo" DefaultButton="imgbtnUpdate" runat="server" BackColor="White" Height="360px" Width="400px" Style="display: none" >
<asp:ImageButton ID="imgClose" runat="server" ImageUrl="~/image_repository/cancelButton.png" ToolTip="Close" />
..................
// Code for Labels and text boxes..
<asp:ImageButton ID="imgbtnCancel" runat="server" CausesValidation="false" TabIndex="8" ImageUrl="~/image_repository/cancel.png" />
<asp:ImageButton ID="imgbtnUpdate" runat="server" CausesValidation="false" TabIndex="8" ImageUrl="~/image_repository/UpdateVoyage.png" />
</asp:Panel>
Here when i click on Update or Cancel Button also the popup is getting closed..
how to respond the user control's button(Update here) click event from the parent web form (aspx page)
Can anyone help
Because you didn't put action. You should add onclick="Cancel_Click1" for example. And add the action in the server side.

Button inside asp:panel is not working properly

I have an ajax extender in my aspx page.
I want to make the button do something in code behind when is pushed but my button inside the panel doesn't work.I created another button outside the panel and wrote the same thing to do and is working.
This is my button inside the panel:
<ajax:AccordionPane ID="AccordionPane1" runat="server">
<Header>Adauga titlu</Header>
<Content>
<asp:Panel ID="UserReg" runat="server" DefaultButton="but">
<cc1:Editor ID="Editor1" runat="server" width="500px"/>
<br /><br />
<asp:Button runat="server" ID="but" Text="Adauga Titlu" />
</asp:Panel>
</Content>
</ajax:AccordionPane>
This button is not working but the other button outside is working and they both do the same thing.Can you provide a solution?
Perhaps you are simply missing the OnClick attribute, like this:
<asp:Button runat="server" ID="but" Text="Adauga Titlu" OnClick="but_Click" />
Have you included the Asp ScriptManager?
http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.aspx

Conditional ConfirmButtonExtender

I am trying to bypass the ConfirmButtonExtender depending on the value of another field in the page.
Basically, when a user click on my "Cancel" button, I normally display a modalpopup using the confirmbuttonextender and the modalpopupextender to display a dialog box confirming that they wish to cancel any changes they have made and return to the prior screen. If they click Yes, the button's onclick event fires which calls some code in the codebehind and redirects the user to another page. If they click no, it just returns to the same page, with no changes.
However, in some situations, I know that my user is unable to perform any edits (they aren't allowed to) and for those users, I don't want to display the "Are you sure you want to leave you will loose any changes" dialog box.
I've set a hidden checkbox field named "cbAllowEdit" to indicate whether the user is allowed to edit the fields or not.
I was trying to use the technique found at link text to get this working but it just doesn't even seem to be firing the button's onclientclick event at all.
Here is my code.
ASPX & Javascript
<asp:CheckBox ID="cbAllowEdit" runat="server" Checked="true" />
<asp:Button ID="btnCancel" runat="server" CausesValidation="false"
OnClick="btnCancel_Click" Text="Cancel" OnClientClick="disableSubmit();return false;" />
<ajaxToolKit:ConfirmButtonExtender ID="ConfirmButtonExtenderbtnCancel"
runat="server" DisplayModalPopupID="ModalPopupExtenderbtnCancel"
TargetControlID="btnCancel" BehaviorID="ConfirmButtonExtenderbtnCancel" />
<ajaxToolKit:ModalPopupExtender ID="ModalPopupExtenderbtnCancel" runat="server"
BackgroundCssClass="modalBackground" CancelControlID="btnCancelCancel"
OkControlID="btnConfirmCancel" PopupControlID="ConfirmCancelPanel"
TargetControlID="btnCancel" />
<asp:Panel ID="ConfirmCancelPanel" runat="server" CssClass="modalWindow"
Height="200" Width="450">
<p class="confirmMessage">
Are you sure you want to navigate away from this record?
</p>
<div align="center">
<p class="feedbackError">If you have made any changes to the record since the last time
you saved, they will be lost.</p>
<asp:Button ID="btnConfirmCancel" runat="server" Text="Yes" Width="75" />
<asp:Button ID="btnCancelCancel" runat="server" Text="No" Width="75" />
</div>
</asp:Panel>
<script type="text/javascript">
function disableSubmit() {
if (document.getElementById('<%= cbAllowEdit.ClientID %>').checked) {
return checkSubmit();
}
else {
return true;
}
}
function checkSubmit() {
var confirmButton = $find('ConfirmButtonExtenderbtnCancel');
confirmButton._displayConfirmDialog();
}
</script>
Code behind:
/// <summary>
/// Runs when the btnCancel button is clicked.
/// </summary>
protected void btnCancel_Click(object sender, EventArgs e)
{
Page.Response.Redirect("~/Searches/LookupCode/Default.aspx");
}
any ideas on what I am doing wrong?
As Chris said, you don't have to have a ConfirmButtonExtender and ModalPopupExtender (although this looks like a way round the fact that the JavaScript confirm() function is limited to OK and Cancel buttons in its' dialog - noted as a technique for the future :-) )
If you know in your code-behind that the user isn't going to be doing any editing (as evidenced by your cbAllowEdit control), then you should be able to just disable the ConfirmButtonExtender:
ConfirmButtonExtenderbtnCancel.Enabled = false;
Then clicking the Cancel button should take you into your code-behind function without confirmation at all.
EDIT: The ConfirmPopupExtender shouldn't be pointing to the btnCancel. Instead, it should be pointing to a hidden button (this is what the example in the link does).
<asp:Button ID="btnCancel" runat="server" CausesValidation="false"
OnClick="btnCancel_Click" Text="Cancel" OnClientClick="disableSubmit();return false;" />
<!-- This is the Hidden Buttons that you need to add and reference in the ConfirmButtonExtender -->
<asp:Button ID="HiddenButtonConfirm" runat="server" Text="" style="display:none;" />
<asp:Button ID="HiddenButtonModal" runat="server" Text="" style="display:none;" />
<ajaxToolKit:ConfirmButtonExtender ID="ConfirmButtonExtenderbtnCancel"
runat="server" DisplayModalPopupID="ModalPopupExtenderbtnCancel"
TargetControlID="HiddenButtonConfirm" BehaviorID="ConfirmButtonExtenderbtnCancel" />
<!-- You'll also want the ModalPopup to point to a seperate hidden button. -->
<ajaxToolKit:ModalPopupExtender ID="ModalPopupExtenderbtnCancel" runat="server"
BackgroundCssClass="modalBackground" CancelControlID="btnCancelCancel"
OkControlID="btnConfirmCancel" PopupControlID="ConfirmCancelPanel"
TargetControlID="HiddenButtonModal" />
This javascript might work:
var confirmbtn = $find('ConfirmButtonExtenderID');
confirmbtn.enabled =false;
//ConfirmButtonExtenderID needs to be the name of your ConfirmButtonExtender ID [:$]

Categories

Resources