Message Box in ASP.NET hindering the layout - asp.net

I am displaying a message in my ASP.NET from code-behind using this:
Response.Write("<script>alert('Hello')</script>");
When the OK button is pressed on the message box, the whole layout of the web page shifts a little towards the right direction. What is wrong? Is something else to be added?

private void alert(string Msg)
{
Response.Write("<script language = 'javascript'>window.alert('" + Msg + "')</script>");
}
try the previos method ..
if u want cool messages try the following link:
Produce "toast" messages like StackOverflow

Could it be that the scrollbars get visible when the popup is active? It has happened a few times for me and I thought the style was getting messed up, but it was just the scrollbars becoming visible and disappearing :) They shift the page a little.

At the risk of getting dinged for providing an additional answer to an "answered" question, I think others coming across this question should be advised as a matter of course that hard-coding HTML strings through Response.Write in ASP.NET applications is an increasingly frowned-upon practice for precisely the reason you've observed - the page rendering emits HTML that has no way of accounting for interjected Response.Write statements, risking all manner of layout grief.

Related

2 Modal Popups - properties being switched when ajax script runs

While I was typing this I figured out what was causing my issue, but I haven't figured out why it was causing an issue, and it seems strange to me. Maybe someone could explain... Here is my situation (question is at the bottom):
I have 2 modal popups.
PopupControlID as follows:
popup1
popup2
calling from server side code:
popup1.Show() 'works fine
popup2.Show() 'works fine
at some point though:
popup1.Show() 'inspecting on server side all properties definitely belong to
'popup1, but when the ajax .js is triggered, popup2
'property values are being used
Everything was set up properly for these controls to run, but something outside of their setup was cauing a problem.
In page_load I was setting the target popup control for popup2 (which was a div) to .Visible = False. When I noticed this code I deleted it because it was unnecessary since I knew the target control was hidden using style="display:none;". After removing this, I ran the code, not expecting this to fix the bug, but the conflict disappeared. I do not understand how this could affect my pop ups in such a manner.
THE QUESTION:
Does anybody know why popup1.Show() would go ahead and use popup2's properties instead of it's own in the ajax script, just because at some point in the code the target control of popup2 was set to .visible = false?
In replicating my issue it seems the answer is as simple as this:
Any time you set a control to .Visible = "False" that is also the PopupControlID of a modal popup extender it will cause an error and the page will not load. I feel kinda stupid for now realizing this up front, a real "duh" moment...
Anyway, pretty basic stuff when I realized what was happening. Here is an article related to this issue:
http://blog.matthewdoyle.net/2008/10/13/visiblefalse-vs-displaynone/

before closing the Application browser should prompt conformation

I have a small requirement..
if the user dint sign off or log off then he try's to close the browser IE clicking on 'X' (top right of IE or Firefox browser ) then i need to ask a conformation message like "Are you sure you want to close ?" ...
I am using Master page in my application and i tried the event : "window.onbeforeunload " in my master page its works fine, shows an alert(conformation) message. but if i press back button on the browser(IE on IE or Firefox) then also its firing(but it should not) is there any way to full fill my requirement ..I hope i had explained u clearly...if not pl z let me know........
what i mean to say is.. if the Session("USerid") is active or if it contains any value ie.
Session("USerid")="XXX"
at that moment if user trys to close the browse(click in 'X'/Close button browser either IE or Firefox ) it should give prompt a message "are u sure do u want to close?"..
Its all about design steps - but the close and the back button is the same, the close the page, so maybe its impossible to have them all together.
To open, close your script you can make a simple trick. Place them inside a literal and open or close it.
<asp:literal run="server" id="txtCloseAlert">
<script>
... you code here ....
</script>
</asp:literal>
and on code behind.
txtCloseAlert.visible = !string.IsNullOrEmpty(Session("USerid"));
I've looked into this recently and there does not appear to be a standard / consistent way to do this cross-browser hence you back-button problem.
On IE at least you get an event object passed as a parameter to the onbeforeunload method that you can use to get the mouse position, but in FireFox you don't and you would need some other way to determine whether a confirmation is required. It is quite posible that you could get the mouse position in some other way as I haven't looked into that. Point is that if your mouse is not on your form you probably want a confirmation.
You can look at this SO question:
Prevent browser from closing in asp.net
Or do an Internet search on 'onbeforeunload prevent browser closing'.
In your case a synchronous ajax call can be made to the server to do the test.
HTH

ASP.NET How can I write a message on the screen without the end user removing it?

I have written a ASP.NET program for a customer, I want to add a message similar to "Preview version, ABD Consulting" on the master.master page, I had thought to use Response.write but it messes up the look of the page as it seems to move page elemets. If I use a label the customer can remove it from the Master.master file, any suggestions? The customer is in a different country so I want to ensure I'm paid.
Many thanks
Serve it on your own server. If it's a preview, they shouldn't have access to the code anyway.
There is nothing you can do unless you host it or control the web server it runs on. Nothing you do in code will matter if they are smart enough. They can write their on HTTP Handlers and replace anything they want.
If you programmatically write out the label during the OnPrerender or Render of the page then the client will not be able to remove it. If you then randomize the ID given to the element, they will find it incredibly hard to apply any javascript functions or CSS styles to it, especially if you directly add the styles to it.
Something like this (pseudo code):
HtmlGenericControl label = new HtmlGenericControl("div");
label.ID = Guid.NewGuid().ToString();
label.InnerText = "My copyright or ownership text";
label.Style.Add(HtmlTextWriterStyle.Height, "50px");
label.Style.Add(HtmlTextWriterStyle.Width, "100px");
if you then absolutely position it, it should always show up. Note that it isn't totally untouchable and fool proof, but you want to just make it hard enough that the client doesn't try to remove it.
Obfuscate it in a dll and use the Current Context to write a pretty div like the one that StackOverflow.com uses on top.
I'm with George and Rick - don't let them have the source and serve it up from a server you control. In addition, I'd created a background image that says "Demo". This will remind that they need to pay up.

does showModalDialog interfere with ClientScript.RegisterStartupScript?

I'm showing a modal dialog via "window.showModalDialog(..." which happens in a vbscript function (the page shown is aspx). I'd like to do some resizing of the window based on the number of rows in a datatable that's coming back. So naturally I go to register a startup script that resizes the window based on the number of rows. Well, that didn't work, so I tried to register a script that just showed a msgbox.
The code looks like (in the OnLoad event handler):
if (!this.ClientScript.IsStartupScriptRegistered(typeof(MyPageClassName), "hello"))
{
this.ClientScript.RegisterStartupScript(typeof(MyPageClassName), "hello",
#"<script language=vbscript>
sub fnWindowOnLoad()
MsgBox ""hello""
end sub
<script>", false);
}
if (!this.ClientScript.IsStartupScriptRegistered(typeof(MyPageClassName), "hello"))
{
throw new Exception("Failed to load script");
}
To me it looks like this should work and show a message box that says "hello" when the page loads (I've got the window's onload event set to fnWindowOnLoad). But what happens is nothing, no exception, no alert. I've tried every Type I could think of in the typeof call. Nothing seems to work. The only thing I can think of is that since the dialog is a modal ClientScript.RegisterStartupScript won't run properly. But that doesn't make any sense to me.
I put the MsgBox "hello" call into my script block directly and the alert showed, so it's possible. But I need to modify some arguments in the code behind so I have to use RegisterStartupScript as far as I can tell.
Have you tried opening your window via window.open() rather than window.showModalDialog()? I've seen some postings on the web about incompatibilities between showModalDialog() and RegisterStartupScript.
showModalDialog() is an IE only method, so it's not recommended anyway. I know it's convenient because it returns a value, but there are various ways to simulate this functionality.
Edit: The other problem with showModalDialog() is that IE often caches the results. This means that if one time you calling the dialog, you do not resize it, then another time you do, then 2nd time might get your the first cached dialog. A way to get around this is to add a unique querystring at the end. Like MyDialog.aspx?q=320934 (randomly generated or generated based on server tics).
The solution for this was to have a script that read a value out of a hidden field and then resized the dialog. The value was set on the Page_Load. Using RegisterStartupScript never seemed to work, neither did RegisterClientScript, so I'm pretty sure modal dialog and RegisterXxx don't get along. Need to use window.dialogHeight & window.dialogWidth in the vbscript.

Asp.net and JavaScript pop up windows

I am writing an intranet application and am considering the use of a pop up window. I am not worried about accessibility since it's an intranet app.
The scenario is such as I need to be able to have the same code be used in a server page as well as in the middle of a process; which is why I decided when using it in the middle of the process, it's best to have it as a pop up window to running out of the real estate on the screen.
Any thoughts on this? I am hesitant to use a pop up window in such a manner as I usually only use it for error messages.
I don't completely understand what you're trying to do, but I think a popup window might be somewhat of an issue if the user's browser automatically blocks popup windows. Plus, if you were trying to run a process in the popup window, the user could close it and no longer have a way to check on the process.
Would it be possible to use Ajax to call back to a web service that gives the page information about the process? You could give the user a way to make the Ajax call to check on the status of the process or just have it continually polling in the background.
Edit:
You said you weren't too familiar with Ajax. For the most part, there are libraries to handle all the of hard details. I'll recommend jQuery because that's what I've been using for a while now.
If you go the Ajax route you'll be able to contain everything on one page and make the updates you need to make when the Ajax call is successful. Depending on how you write the code, it should be pretty reusable if you do it right. It really depends on how specific the your needs on each page.
Take a look at the jQuery documentation though. It may have what you need already built into it. Otherwise, someone else might be able to suggest some reasons why their favorite JavaScript library works better for what you're trying to do.
I think you might want to do something like this:
Inside of the parent page:
<input id="btnShowModal" runat="server" type="button" value='Show Modal' onclick="ShowModal()" />
function ShowModal()
{
var retVal = window.showModalDialog("MyPopup.aspx?param1=value","","center=yes;dialogWidth=200px;dialogHeight=200px;status:0;help:0")
if(retVal != "" && retVal != undefined)
{
//This code will be executed when the modal popup is closed, retVal will contain the value assigned to window.returnValue
}
}
Inside of the modal popup:
<input id="btnSave" runat="server" type="button" value='Save' onclick="Save()" />
function Save()
{
window.returnValue = "Whatever you want returned to the parent here"
window.close()
}
The usual argument against popup windows is that they are unreliable. The user may have disabled script initiated popups, I know I have.
In a controlled environment, such as an inranet, you may be able to be guaranteed that this is not the case, but even so, why risk it, there is an alternative.
Instead of popping up a new window just insert a new, absolutely positioned <div> into the document and insert your content into that using ajax or even an <iframe>. There are lots of examples/libraries on the web.
Thickbox for jQuery for example. There are of course scripts that don't require libraries.
I generally use a div with a z-index and absolute positioning; the .show() can be written and called on demand, it would have a button to .close(), and AJAX can make it seem modal so it must be clicked to close if you so desire. Then again, I hate messageboxes.
I was trying to avoid AJAX, simply because I have never used and don't have the time frame to learn it now. However, I am not totally opposed to it.
In short what I need to do is for the pop up window interact back with the page. Imagine that on the page I am building the links of the chain. Each link has unique properties. When user clicks on "ADD LINK" button, I was thinking have a pop up window with the little form and a Save button. The only issue with this is that a pop up needs to interact with the page; we need to know when something has been saved or not saved.
A div on the same page is one way. A pop up is yet another way. Another catch is that this code (adding new link) needs to be reusable, because I am also going to have a page that just creates new links.

Resources