AsyncPostBack FileUpload on a ModalPopup - asp.net

I am using a UpdatePanel, and my submit button is one of the triggers along with the clear button. But the problem is I have a FileUpload Control in the div. This is a modal popup, so it displays a form for a user to upload a little note. When I try to upload a file with AsyncPostBackTrigger it does nothing (which I have read about). My question is how do I not use the PostBackTrigger because I want to use the asyncpostbacktrigger because if an error occurs, then the Modal popup closes and the user doesnt know if the file was uploaded or not. What can I do?
Code:
<asp:Panel ID="addnotepanel" runat="server" style="/*display:none;*/" CssClass="addnotepanel">
<asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ClrBtn" />
<asp:PostBackTrigger ControlID="SubmitBtn" />
</Triggers>
</asp:UpdatePanel>
File:
<br />
<asp:Label ID="ErrorLabel" runat="server" Visible="False"></asp:Label>
<br />
<asp:Button ID="Submitbtn" runat="server" Text="Submit"
onclick="Submitbtn_Click" />
<asp:Button ID="CnlBtn" runat="server" Text="Cancel" onclick="CnlBtn_Click" />
<asp:Button ID="ClrBtn" runat="server" onclick="ClrBtn_Click"
Text="Clear" />
</div></asp:Panel>

keep SubmitBtn as a PostBackTrigger but dont set it as the "OkControlID" of the modalpopupextender.
in the Submitbtn_Click server side sub, call yourModalpopupextenderID.hide() if the upload is completed so the Modal pop up closes only if there is no error.
You could use a AsyncFileUpload from the AjaxControlToolkit
Here, as an exemple some code to show how to use it :
<AjaxControlToolkit:AsyncFileUpload ID="AttachementsFileUpload"
runat="server"
OnUploadedComplete="AttachementsFileUpload_UploadedComplete"
OnClientUploadComplete="uploadComplete" />
<script type="text/javascript">
var UpdateAttachementsGridViewButton = '<%= UpdateAttachementsGridViewButton.ClientID %>';
function uploadComplete(sender, args) {
$get(UpdateAttachementsGridViewButton).click();
}
</script>
As you see, when the upload is complete, I use Javascript to trigger an click on an hidden button. Meanwhile I retrieve the file in the AttachementsFileUpload_UploadedComplete using something like this:
Dim AttachementsFileUpload As AjaxControlToolkit.AsyncFileUpload = AnnouncementFormView.FindControl("AttachementsFileUpload")
Attachements.add(e.filename, AttachementsFileUpload.FileBytes)
It's how I used it in my situation, but you will find plenty of examples on how it work

Related

Page gets refreshed while loading Crystal Report

I am working on an Asp.net project and we have some interfaces to load corresponding reports.
My html code is as follows:
<form id="form1" runat="server">
<div>
/*----html code for selecting parameters----*/
<asp:Button Text="Submit" AutoPostBack="False" CssClass="btn btn-primary" runat="server" ID="btnSubmit" OnClientClick="javascript:return Validate()" OnClick="btnSubmit_Click" />
/*----Button to check validity and load report----*/
</div>
/*----Some html codes----*/
<CR:CrystalReportViewer ID="CRViewer" runat="server" AutoDataBind="true" EnableParameterPrompt="False" ReuseParameterValuesOnRefresh="True" ToolPanelView="None" EnableDatabaseLogonPrompt="False" HasCrystalLogo="False" HasToggleGroupTreeButton="False" HasToggleParameterPanelButton="False" HasDrilldownTabs="False" HasDrillUpButton="False" HasRefreshButton="True" HasPageNavigationButtons="True" HasPrintButton="True" DisplayToolbar="True" />
/*----this is where report get loaded----*/
</form>
after selecting parameters and clicking submit button, the report gets loaded, but the page gets reloaded and all parameters get reset.
How do I prevent the parameters from getting reset?
To prevent the whole page from reloading you need to use a updatepanel to do partial postback.
The example would look like this
<asp:UpdatePanel ID="UpdatePanel" runat="server>
<ContentTemplate>
<CR:CrystalReportViewer ID="CRViewer" runat="server" AutoDataBind="true" EnableParameterPrompt="False" ReuseParameterValuesOnRefresh="True" ToolPanelView="None" EnableDatabaseLogonPrompt="False" HasCrystalLogo="False" HasToggleGroupTreeButton="False" HasToggleParameterPanelButton="False" HasDrilldownTabs="False" HasDrillUpButton="False" HasRefreshButton="True" HasPageNavigationButtons="True" HasPrintButton="True" DisplayToolbar="True" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSubmit" />
</Triggers>
</asp:UpdatePanel>
Make the button the trigger for the updatepanel meaning if you press the button only the part inside the updatepanel will reload so you won't lose any values

ScriptManager issue in asp.net on Submit

I have following code:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Button ID="btnSave" runat="server" Text="Submit"/>
<asp:Panel ID="pnlMyView" runat="server">
<asp:GridView Control here...
</Panel>
<asp:Panel ID="pnlInfo" runat="server" Visible="False">
<div>
Some information...
</div>
</Panel>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
Update in progress...
</ProgressTemplate>
</asp:UpdateProgress>
<script language="javascript" type="text/javascript">
$(window).load(function() {
$("input:submit[id$='btnSave']").click(function() {
//jQuery code to validate gridview enteries...
})
});
</script>
Steps:
1.Refresh the page Or load page first time and clicked on Submit button to validate the entries in GridView
2.Validation done properly and save changes to database.
3.Made some changes in gridview entries and clicked on Submit button
4.Validation didn't take place and save the entries(i.e. wrong entries) in database.
5.Refresh the page i.e. pressing F5 Now everything working fine.
How to resolve issue at step 4.
-----------------PS----------------
Actually I have two panel pnlMyView and pnlInfo.So on page load I only make visible to pnlMyView and other is invisible.
Once if i move to pnlInfo and come to back on pnlMyView and submit Nothing validation take place.
Thanks
The problem is that your HTML gets reloaded and thus the event handler on the button gets lost.
You could try the following code which assures you that the handler stays in place:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div id="root">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Button ID="btnSave" runat="server" Text="Submit"/>
<asp:Panel ID="pnlMyView" runat="server">
<asp:GridView Control here...
</Panel>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
Update in progress...
</ProgressTemplate>
</asp:UpdateProgress>
</div>
<script language="javascript" type="text/javascript">
$(window).load(function() {
$("input:submit[id$='btnSave']").on("click", ".root", function() {
//jQuery code to validate gridview enteries...
})
});
</script>
What this does is register the handler differently. You need to wrap the UpdatePanel inside a div with class root. Then you register the handler like this:
$("input:submit[id$='btnSave']").on("click", ".root", function() {});
This tells jQuery to registere a handler on an element called root, but only execute it when the input:submit... is executed.
Since the div is not removed, it will still detect the handler even after you have replaced the HTML.
These types of events are called delegated events. You can read a more thorough explanation in the jQuery documentation: http://api.jquery.com/on/
Better use OnClientClick property of Button control:
<asp:Button ID="btnSave" runat="server" Text="Submit" OnClientClick="return validate()" />
function validate(){
//jQuery code to validate gridview enteries...
//if validation isn't succeeded return false, otherwise return true
}
Hey friends I got the solution.I used live event and now everything is working fine.
$("input:submit[id$='btnSave']").live("click", function(event) {
Thanks a all for your response

HTML input within a asp:UpdatePanel

I have a html input control within a asp:UpdatePanel and I have its associated upload button specified within a asp:PostBackTrigger tag. Here is the aspx code:
<asp:UpdatePanel ID="upGallery" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<portal:ModuleTitleControl id="Title1" runat="server" />
<portal:OuterBodyPanel ID="pnlOuterBody" runat="server">
<portal:InnerBodyPanel ID="pnlInnerBody" runat="server" CssClass="modulecontent">
<div id="Uploader" runat="server">
<h2>Upload a docx file to be translated.</h2>
<input id="input_FileUpload" runat="server" type="file" />
<asp:Button ID="button_UploadFile" runat="server" OnClick="button_UploadFile_Click" Text="Upload" />
</div>
</portal:InnerBodyPanel>
</portal:OuterBodyPanel>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="button_UploadFile" />
</Triggers>
</asp:UpdatePanel>
Here is the code behind to retrieve the value of the input control "input_FileUpload":
string filename = input_FileUpload.Value;
filename is always empty when I step through the code.
What am I doing wrong?
The FileUpload control has known problems with the UpdatePanel. Check out this prior discussion: FileUpload control inside an UpdatePanel without refreshing the whole page?

Popup on mouse enter and hide on mouseout

I was trying to apply a pop up control to a Link button on my ASP.NET website. The popup appears only on button click. How can the behavior be moedified to make the popup appear when on mouseover and ide on mouseout?
Button code:
<asp:LinkButton ID="LinkButton2" CssClass="btn green" ToolTip="NewProfile" Text="NewPlugin"
runat="server" Width="175px" onclick="AddBtn_Click" /><br /><br />
For popup control:
<asp:Panel ID="Panel4" runat="server" CssClass="popupControl">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Administrative previliges are required for this action.
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<asp:PopupControlExtender ID="PopupControlExtender1" runat="server" TargetControlID="LinkButton2" PopupControlID="Panel4" Position="Right">
</asp:PopupControlExtender>
Here is a JQuery Function that does exactly the same thing you are trying to do...
$(".deal-hotel").live('mouseover',
function() {
$(this).find('.deal-hotel-popup').show();
}).live('mouseout',
function() {
$(this).find('.deal-hotel-popup').hide();
});
Yet changing the HTML Attribute for onclick to onmouseover should get you there. Read This
On a side note switching to JQuery will make you like a lot easier.

ModalPopupExtender OK Button click event not firing?

I have a Button inside an UpdatePanel. The button is being used as the OK button for a ModalPopupExtender. For some reason, the button click event is not firing. Any ideas? Am I missing something?
<asp:updatepanel id="UpdatePanel1" runat="server">
<ContentTemplate>
<cc1:ModalPopupExtender ID="ModalDialog" runat="server"
TargetControlID="OpenDialogLinkButton"
PopupControlID="ModalDialogPanel" OkControlID="ModalOKButton"
BackgroundCssClass="ModalBackground">
</cc1:ModalPopupExtender>
<asp:Panel ID="ModalDialogPanel" CssClass="ModalPopup" runat="server">
...
<asp:Button ID="ModalOKButton" runat="server" Text="OK"
onclick="ModalOKButton_Click" />
</asp:Panel>
</ContentTemplate>
</asp:updatepanel>
Aspx
<ajax:ModalPopupExtender runat="server" ID="modalPop"
PopupControlID="pnlpopup"
TargetControlID="btnGo"
BackgroundCssClass="modalBackground"
DropShadow="true"
CancelControlID="btnCancel" X="470" Y="300" />
//Codebehind
protected void OkButton_Clicked(object sender, EventArgs e)
{
modalPop.Hide();
//Do something in codebehind
}
And don't set the OK button as OkControlID.
It appears that a button that is used as the OK or CANCEL button for a ModalPopupExtender cannot have a click event. I tested this out by removing the
OkControlID="ModalOKButton"
from the ModalPopupExtender tag, and the button click fires. I'll need to figure out another way to send the data to the server.
It could also be that the button needs to have CausesValidation="false". That worked for me.
I was just searching for a solution for this :)
it appears that you can't have OkControlID assign to a control if you want to that control fires an event, just removing this property I got everything working again.
my code (working):
<asp:Panel ID="pnlResetPanelsView" CssClass="modalPopup" runat="server" Style="display:none;">
<h2>
Warning</h2>
<p>
Do you really want to reset the panels to the default view?</p>
<div style="text-align: center;">
<asp:Button ID="btnResetPanelsViewOK" Width="60" runat="server" Text="Yes"
CssClass="buttonSuperOfficeLayout" OnClick="btnResetPanelsViewOK_Click" />
<asp:Button ID="btnResetPanelsViewCancel" Width="60" runat="server" Text="No" CssClass="buttonSuperOfficeLayout" />
</div>
</asp:Panel>
<ajax:ModalPopupExtender ID="mpeResetPanelsView" runat="server" TargetControlID="btnResetView"
PopupControlID="pnlResetPanelsView" BackgroundCssClass="modalBackground" DropShadow="true"
CancelControlID="btnResetPanelsViewCancel" />
Put into the Button-Control the Attribute "UseSubmitBehavior=false".
None of the previous answers worked for me. I called the postback of the button on the OnOkScript event.
<div>
<cc1:ModalPopupExtender PopupControlID="Panel1"
ID="ModalPopupExtender1"
runat="server" TargetControlID="LinkButton1" OkControlID="Ok"
OnOkScript="__doPostBack('Ok','')">
</cc1:ModalPopupExtender>
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
</div>
<asp:Panel ID="Panel1" runat="server">
<asp:Button ID="Ok" runat="server" Text="Ok" onclick="Ok_Click" />
</asp:Panel>
I often use a blank label as the TargetControlID. ex. <asp:Label ID="lblghost" runat="server" Text="" />
I've seen two things that cause the click event not fire:
1. you have to remove the OKControlID (as others have mentioned)
2. If you are using field validators you should add CausesValidation="false" on the button.
Both scenarios behaved the same way for me.
I've found a way to validate a modalpopup without a postback.
In the ModalPopupExtender I set the OnOkScript to a function e.g ValidateBeforePostBack(), then in the function I call Page_ClientValidate for the validation group I want, do a check and if it fails, keep the modalpopup showing. If it passes, I call __doPostBack.
function ValidateBeforePostBack(){
Page_ClientValidate('MyValidationGroupName');
if (Page_IsValid) { __doPostBack('',''); }
else { $find('mpeBehaviourID').show(); }
}

Resources