I'm stuck in a catch-22 situation...
I have an ASP.NET 4.0 Webforms application, that uses model popups (using the Ajax Control Toolkit's ModalPopupExtender component) to get inputs from user.
One screen is used to add new members, or edit existing member's records. These records include a picture of the member, which I'm uploading using a standard ASP.NET FileUpload control.
That create/edit screen is wrapped in an UpdatePanel so that when I pop up the overlay, I can fill in the textboxes with existing values (when editing an existing member).
Since the popup uses an UpdatePanel, I had to define an explicit PostBackTrigger so that my Save button will actually cause a complete postback and so that I'm able to read up the bytes of the picture uploaded in the FileUpload control.
<asp:UpdatePanel ID="pnlCreateOrEdit" runat="server">
<ContentTemplate>
.... asp.net controls here, including:
<asp:FileUpload ID="uploadPic" runat="server" />
....
<asp:Button ID="btnSave" runat="server" OnClick="BtnSaveClicked" Text="Save" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnSave"/>
</Triggers>
</asp:UpdatePanel>
If I don't define that PostBackTrigger, then my uploaded file can't be read out.
BUT: once I define that postback trigger, now I cannot do my validation anymore, since if I do my validation in the btnSaveClicked method and it fails (something is missing for instance), I can detect that - but I can't respond to it anymore, since the popup will be closed no matter what I've tried so far....
protected void BtnSaveClick(object sender, EventArgs e) {
// do my validation - if something is amiss, don't go any further
if (!IsValid()) {
return;
}
// read back the data
// hide the modal popup form
// call an event handler to process the inputs
}
So now I'm stuck:
if I omit the post back trigger, my validation works nicely - if something is wrong, it's flagged and shown on the popup form, and the popup isn't closed
==> BUT in that case I cannot read out the bytes of the uploaded picture..... (uploadPic.HasFile is always false)
if I put in the post back trigger, my file upload works nicely, I can read out my bytes no problem
==> BUT now, my validation - while detecting something's amiss - cannot show the error since the popup form is being closed by the full postback....
Is there any way to get BOTH - the bytes in the FileUpload control, AND the popup staying open if validation fails?
This popup will give you complete control with out any post back issues.
you can see this in action here: http://atldunia.com/youtube/Zpopup.aspx
you can also download the code.
I ended up using the AsyncFileUpload component from the Ajax Control Toolkit - this works nicely on an UpdatePanel and satisfies all my needs.
Related
I'm new to web programming with .NET.
I am developing a web page with webforms, and I want at a certain moment to programmatically show a modal window, for the user to accept or cancel, according to a question. Exactly what does the "confirm" function of JavaScript.
I tried to get it calling a JavaScript function:
Page.ClientScript.RegisterStartupScript (this.GetType (), "CallMyFunction", "MyFunction()", true);
But I need to do it without reloading the page, and I also need to control if the user has accepted or canceled and I do not know how to do it.
I've also tried getting it using the ModExPopupExtender control from DevExpress.
Can someone tell me a simple way to get what I want?
I can not understand how something so usual in web programming, and that PHP + javascript would not pose any problem can be so complicated.
All start in a one-button event on the code behind:
protected void btn1_Click(object sender, EventArgs e)
{
//I make a series of checks
//If certain conditions I want to show the confirm
//According to the user has chosen ok or cancel will perform a certain action
}
Onclientclick does not help me because before launching the "confirm" I have to do some checks on the server side.
Thank you very much.
You can use OnClientClick which is a property on most web controls.
I like to just bring up a simple confirm() dialog which executes the server code if the user clicks OK and does nothing if the user cancels the action:
<asp:Button runat="server" ID="btnSave" Click="btnSave_Click" Text="Save"
OnClientClick="return confirm('Are you sure you want to do this thing?');" />
You can do other things with it as well, but the key thing to remember is that anything you do in OnClientClick will happen before the page gets posted back to the server.
This is also perfectly valid:
<asp:Button runat="server" ID="btnSave"
OnClientClick="showModalConfirm('some message goes here');" ... />
<script>
function showModalConfirm(msg)
{
$(".modal .message").innerHtml(msg);
$(".modal").Show();
}
</script>
You can set the action that OnClientClick should perform in your codebehind in exactly the same way:
protected void Page_Load(object sender, EventArgs e)
{
btnSave.OnClientClick = "return confirm('Are you sure you want to do this thing?');";
}
You can use below code in c# to call javascript function. Below code will execute afterpostback() javascript function:
ClientScript.RegisterStartupScript(GetType(), Javascript, "javascript:afterpostback();", true);
And you can write code in javascript function to display any div or popup:
<script language="javascript" type="text/javascript">
function afterpostback() {
//Here you can write javascript to display div/modal
}
</script>
One way I've handled this previously was to have 2 buttons on the page. The first would be initially visible and labeled "Submit". The second would be initially hidden and labeled "Confirm". The "Submit" button would postback upon click and perform your server side checks/validation. If those checks failed, an appropriate error message would be displayed. If those checks passed, an appropriate "Please confirm your submission"-type message would be displayed, the "Submit" button would become hidden, and the second "Confirm" button would become visible. When that Confirm button was clicked, it would postback again and fully submit.
EDIT: I forgot to mention, there's a bit more to this that occurred to me after I initially posted. You'll have to protect the fields from being edited in the event the server-side verification is successful as you obviously don't want the user changing values and then clicking the Confirm button. That means disabling all the input controls - which could be a pain if you have a lot. You also have to give them a way to (intentionally) Edit in case the server side verification passes, you display the Confirmation, and they change their minds - so basically you'd need a third "Cancel/Edit"-type button that would put the form back in edit mode and show your initial Submit button.
What I want to do is on a page, there is a basic textbox form in an "InputSection" to get input from users, then when user hit preview button (asp button) at the bottom of form, the data are gather and provided in a sorted format and displayed below the "Input Section", a section called "PreviewSection", before the submission to the database. The preview process is using asp label to receive the direct reference from the textbox on the same page, not go through server/database yet, something like,
LbPrevQuestion.Text = TBQuestion.Text;
I have no problem to do the data collection so far, and display in the preview section when the preview button is hit. But the problem is the page always goes back to the top of the page. What I tried was,
the button:
<asp:Button ID="btPreview" runat="server" Text="Preview" CssClass="btn" OnClick="btPreview_Click"/>
code behind:
protected void btPreview_Click(object sender, EventArgs e)
{
Response.Redirect("Contact.aspx#PreviewSection");
LbPrevQuestion.Text = TBQuestion.Text;
}
when the button is hit, the input content is wiped out... What would be optimal solution for this? thank you very much.
FIrs off i think that what you are needed to achieve, can be achieved using javascript or jquery only client side without a postback. If you strictly want to execute some processing at server side then you may want to use the following server side way for retaining the scroll position.
If you want to maintain the scroll positions after postback then maintainScrollPositionOnPostBack="true" attribute is what you are after. You can specify this attribute at page level in the #page directive in aspx page or
you can also define this in web.config file in the section if you need this in all pages of your application.
<system.web>
<pages maintainScrollPositionOnPostBack="true">
</pages>
</system.web>
http://madskristensen.net/post/maintain-scroll-position-after-postbacks
I am developing a website using asp.net/C#
I want to create a button that can save user data then move to a div tag at the same page.
I already did the saving part, but how do I make the same button to navigate to a different div tag within the same page?
some people use the response.redirect method to navigate to a different page. I want to navigate to a div tag within the same page. For example:
Experience
after I press that it will take me to:
<div class="panel" id="experience">
I want to do the same but with button that can do both that and saving to a DB. As I said, I already did the saving part.
I tried this:
<asp:Button ID="Button1" runat="server" Text="Save & Continue" OnClientClick="#experience" OnClick="Button1_Click" />
But it didn't work.
any ideas? I am trying to access that div tag from code behind
If this is WebForms and you don't want to use javascript, then, after you do the save, you can do a Response.Redirect to the same url, but with the anchor tag, e.g. Response.Redirect("myPage.aspx#experience").
This really depends on the rest of your code though. I suggest posting the relevant parts of your code behind so that we can better see what you have done. Also, don't try to avoid javascript - it is too useful to avoid.
ASP.NET has a Focus method that can be applied to the following ASP.NET server controls:
Button
LinkButton
ImageButton
CheckBox
DropDownList
FileUpload
HyperLink
ListBox
RadioButton
TextBox
At the end of your Button1_Click event handler code-behind, add something along this lines of this:
TextBoxExperience.Focus();
Note - The TextBox control with ID of TextBoxExperience control would be inside of the experience DIV.
I have a Update Panel inside a User Control i use on 2 pages in my website
Both pages use the same MasterPage, ScriptManger is declared in the MasterPage.
Both pages call the UC the same way:
<uc:SearchCube runat="server" ID="searchCube" />
in the Update panel i have many RadioButtons that on change generate a server side event that fill dropdown in the update panel and update the panel
protected void SearchCategoryChanged(object sender, EventArgs e)
{
FillDropdowns();
SearchOptions.Update();
}
Update Panel is set like this:
<asp:UpdatePanel ID="SearchOptions" runat="server" UpdateMode="Conditional"
hildrenAsTriggers="true"/>
Each RadioButton is set like this:
<asp:RadioButton ID="RadioButton1" GroupName="SearchCategory" runat="server"
AutoPostBack="true" OnCheckedChanged="SearchCategoryChanged" Text="Text"/>
I also have an AsyncPostBackTrigger on each Radio Button Controller
The problem i have is that on one page when i call the Update() function the panel is updated and Page_Load is triggered which causes the UC to refresh and reload the default settings of the UC
I can see in DEBUG mode that on the working page Update() does not generate Page_Load.
Can anyone explain to me why this is happening?
Everytime a request goes to the server, it executes the Page_Load event.
What you need to do is make sure you have a PostBack validation on all your pages:
protectec void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//Not a postBack: Normal page load
//Init your page here
}
else
{
//It's a PostBack (from a command).
//Do nothing or init stuff your all your commands.
}
}
If you put some breakpoints in your Page Load and your SearchCategoryChanged method, you'll be able to see what the pipeline looks like.
Fixed my problem.
the problematic page is an index page that takes a few parameters in.
I have a Response.Redirect() on the page to avoid duplication of pages.
Apparently when the PostBack() is made it calls the page without any parameters and i was forcing it to be redirected into a default view since no parameters were sent to the page.
i found a lead to my problem in a Microsoft help forum that stated:
By calling Response.Write() directly you are bypassing the normal
rendering mechanism of ASP.NET controls. The bits you write are going
straight out to the client without further processing (well,
mostly...). This means that UpdatePanel can't encode the data in its
special format.
Anyway the page was reloading every time which caused it to reload the User Control with it's default values.
What I’m trying to accomplish is this. I want it to be so that when the user clicks a button, a pop up comes up and allows them to complete, say, the survey. However, I do NOT want it to be a popup window, just something like in the image. Additionally, the pop up needs to be able to have a webpage embedded in it, because the survey is a page in itself. So basically, instead of it being that when the user clicks the button they are redirected, I want a pop up (not a popup window) to appear with the page in it. What I have used so far is iframe in a panel, but I feel there must be a better and more stable way, than using frames. Can anyone suggest a better method? Sorry about my terminology, if have no idea what this is called, I’m new to .ASP NET wed development.
Here is the code (the button changes visible=”False” to true):
<asp:Panel CssClass="BlowItUp" ID="Panel1" runat="server"
Visible="False" >
<iframe src="http://10.0.0.1/start.htm" style="width:100%; height:100%; top:0px;" />
</asp:Panel>
The CSS if it matters:
.BlowItUp
{
position:absolute;
width:80%;
height:90%;
z-index:300;
right:10%;
top:5%;
padding:0px;
}
Unless you have control over the "inner" page, I think a frame is the best you're going to manage. Otherwise the inner page will seek to navigate to another page and you'll lose your "framing".
The one thing I would suggest though is a different popup panel. Say jQuery UI Dialog?