Reusable jquery dialog - asp.net

I am using ASP.NET 3.5 with C# 2008.
I want to use a JQuery dialog for showing error message.
For this I need to include a CSS, a JavaScript file into aspx page. Also have to create a <div> with specific Id or class and have to put some buttons like OK, Cancel inside it. Also have to put a label inside <div>. And have to create a <script> block which contains functions to Open or Close the dialog.
All this working fine in single aspx page. But now I want to use this across all the pages and also don't want to make repetitive codes.
So what will be the best way to implement it? Should I put all these stuff inside a user-control and import it into every aspx page where I want to use that dialog? Or any other best way to achieve this?

You can put your javascript code into a js file and add the reference of that js file into your master page then you can call the jquery error dialog into your application.

Related

In aspx with mvc 4, how to modify layout pages slightly on opening of a new page

When an htmlActionLink is clicked, a new page (say About) is opened with layout and the main body is rendered from the corresponding view. I want to change the style of the about link to show that this link is active. I tried a lot using jquery. It changes momentarily, but it comes back to its original style. I have not tried it through controller. I've used jquery in the layout.cshtml and about.cshtml page.
Look in to MvcSiteMap. You can use the MVC helpers to create a navigation menu that changes as you visit pages. Also, keeping it server side over using jQuery is going to be the best bet.

How to react in ASP.NET code-behind class when a hyperlink is clicked inside the page's IFrame?

I have an ASP.Net page containing an IFrame. In the IFrame I load a html document. When the user clicks on a hyperlink in the content of the IFrame, I would need a callback to be called in the code-behind class of the ASP.Net page.
I guess that I need Ajax to do this but I'm not exactly sure about what I need to do. Could you give me some pointers?
By the way I'm fairly new to ASP.Net.
Thanks
A lot of this depends on what it is you want to do specifically.
The problem you've got is that the DOM of the page in the iframe doesn't appear to be in the DOM of the calling page. All the calling page sees is the iframe tag as a closed tag, like an image tag. Some browsers will detect a click inside an iframe nested within a DIV as a click in the div so you have
<DIV id="iframediv">
<Iframe blah...>
</DIV>
and then you might be able to use jQuery or similar to detect a click inside iframediv and do stuff.
The real solution would be to try not to use an iframe as, like I said, even this solution won't necessarily pay off. I can think of at least one scenario where not using an iframe is not an option so I'll leave that be.
Other than that Willem's suggestions also seem to be sound.
Because the html document is not an aspx page it will not be able to trigger any code-behind. If you can change the page in the iframe make it an aspx page and handle the click on a LinkButton like you would do otherwise.
An other option is to change the link in the html page to call a custom aspx page that handles your needs, but that will redirect the html-page to the new aspx page.
Or indeed change the link to call a webservice through javascript (XMLHttpRequest) and let that webservice do what you wanted to do in code-behind.
Finally I ended up writing a Control Extender for the IFrame. The Control Extender gets the links contained in the IFrame via the following Javascript:
var frame = this.get_element();
var links = frame.contentWindow.document.getElementsByTagName("a");
I then simply attach an event handler that reacts to each link's onclick event. The event handler calls back the ASP.Net side via a WCF service.
Not complicated to do once you know the various technologies.

ASP.Net file upload with an empty posted files collection

I have an ASP.NET file upload control which sits as part of a form. The file upload control is on the content page while the form definition is on a master page across the site. I've added multipart/form-enc to the form on the master page.
I'm using jQuery to submit the form as I show a dialog box from jQuery UI.
When I post, no file is returned to the server. The file upload control has no file and HttpFileCollection is empty. How can I find the posted file?
Most dialogs take your content, wrap it, and place the result just before </body> in the page...this is a problem in ASP.Net because that's outside the <form></form>, it needs to be inside to be included in the POSTed data.
When you create the dialog, make it append inside the <form> when it finishes instead of the <body>, for example this is what you'd do with the jQuery UI dialog:
$("#myDiv").dialog({ ...options... }).parent().appendTo("form:first");
Now that it's been moved inside the <form>, it should post correctly.

Getting server side javascript over client side

I have created custom control using asp.net 2.0. The control contains a textbox txtDate. I have also created a javascript file DateMask.js which contains a function maskDate(). I have attached the maskDate() with textbox using -
txtDate.Attributes.Add("onkeypress","maskDate()");
I have also registered the script using ClientScript.RegisterStartupScript.
When I execute the aspx page containing my custom control it is generating script error showing that maskDate() is undefined.
Could anybody tell me what exactly the problem is?
Thanks for your cooperation.
One way to do it would be to place a literal control above your textbox, and assign the script to it in the code behind:
literal1.Text = "<script>function maskDate() {...}</script>";
The benefit to this, is you would not need to have to reference the script file with some tricky relative paths depending on where your usercontrol is used.
Make sure you didn't forget <form runat="server" ID="Form1"></form> at the end of the <head> tag!
As you can read in Using JavaScript Along with ASP.NET 2.0 under "The Difference Between Page.ClientScript.RegisterStartupScript and Page.ClientScript.RegisterClientScriptBlock" they rely on the location of the form tag.
We have shown two different methods
for placing JavaScript functions on an
ASP.NET pageā€”so what is the
difference? The main difference is
that the RegisterStartupScript method
places the JavaScript at the bottom of
the ASP.NET page right before the
closing element. The
RegisterClientScriptBlock method
places the JavaScript directly after
the opening element in the
page. So what difference does this
make? It can make quite a bit of
difference, as we will see.

I've built a ascx control and I would like to be able to keep adding them using Javascript instead of having to do a full call back

I've built a ascx control and I would like to be able to keep adding new instances of it using JavaScript instead of having to do a AJAX callback. Is this possible? I am basically building a web form for a query control and should clause X be filled in, I want to generate a control for the next clause below. I would like to learn how to do this without doing a callback.
Thanks
ASCX are server side user controls and, to my knowledge, can only be loaded by a server event. This can be accomplished through a full page postback or using UpdatePanels and ASP.net AJAX.
If you don't want to use these options and stick with a full JavaScript solution, you're looking at probably doing DOM manipulation and dynamically adding straight HTML.
If the ASCX controls don't change their appearance and all you're doing is showing and hiding them, one last alternative could be to load all of them into DIV tags that have their display style set to none. Then when the user clicks on a checkbox or whatever, you can use JavaScript to show that DIV tag containing the next control. This is how many JavaScript tab setups work.

Resources