Control Lightbox (Modal) From Code-Behind Of Asp.Net - asp.net

Before meeting with Ajax and Jquery, in my projects I had a function like below.
Public Sub Raise_Alarm(ByVal p_Page As Page, ByVal p_Message As String, Optional ByVal p_IsError As Boolean = True)
strScript = "<script language= ""javascript""> alert('" & p_Message.Replace("'", "") & "')</script>"
Dim t As Type = p_Page.GetType()
p_Page.ClientScript.RegisterStartupScript(t, "alert", strScript)
Dim mylabel As Label
end sub
For now I want to a function instead of function above, which show message as a lightbox (modal box).
How can I do it?

If you want to use jqModal as suggested by cxfx above (+1;), this should work:
strScript = "$('<div>" & p_Message.Replace("'", "\'") & "</div>').jqm();";
ClientScriptManager.RegisterStartupScript(p_Page.GetType(), "alert", strScript, true);

Try one of the excellent jQuery plugins for displaying modal windows such as jqModal. The docs explain how to configure and launch your modal window, and include some great examples.

If you are using "thickbox" it can just display a noraml aspx page in a modal window. You can then use code behind as normal.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<link href="/themes/ui.all.css" rel="stylesheet" type="text/css" />
<link runat="server" href="/styles/thickbox.css" rel="stylesheet" type="text/css" />
<!-- jQuery -->
<script type="text/javascript" src="/scripts/jquery-1.3.2.js"></script>
<script type="text/javascript" src="/scripts/ui.core.js"></script>
<script type="text/javascript" src="/scripts/thickbox.js"></script>
</head>
<body>
<a class="thickbox" href="mylink.aspx?KeepThis=true&TB_iframe=true&height=300&width=850">modal thick box link</a>
</body>
</html>
Hope this helps.

Related

ASP.NET Web Forms call embedded VB Code from another aspx. page

I got a Project where somehow an Method is called from another aspx.page. I want like to know how this work.
For instance, in my Foo.aspx I got this:
<script runat="server">
Sub ShowHint()
some code
End Sub
</script>
in Bar.aspx I got this:
<script runat="server">
ShowHint()
</script>
But how does this can even work? I dont get it.
You can use JavaScript (AJAX) to get the data from a different page.
It is easy to do with the jQuery Load function.
You can also define classes where you can define subs of function which you can call from every webpage.
Is the webpage inside of you project?
An easy example: I have a file test.aspx and I want to load some data from test2.apsx. To load the data I use jQuery.
Here is test.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
//$("#presenter").load("test2.aspx" ... = loads the content of test2.aspx into the div with id = presenter
//$("#presenter").load("test2.aspx #content" ... = loads onlay the content of the div with id = content from text2.aspx
//{ message01: "Hello", message02: "world" } = are the paramter I pass to test2.aspx
$("#presenter").load("test2.aspx #content", { message01: "Hello", message02: "world" }, function () {
//here you can place code which will run after the load is completet
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="presenter"></div>
</form>
</body>
</html>
Here is test2.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="test2.aspx.vb" Inherits="test2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="content" runat="server">
</div>
</form>
And the code from test2.asp
Partial Class test2
Inherits System.Web.UI.Page
Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
Dim msg01 As String = Request("message01")
Dim msg02 As String = Request("message02")
Me.content.InnerHtml = msg01 & " " & msg02
End Sub
End Class
Ok I got this. Just have to include my "Helper" aspx-file in to another aspx-File:
<!--#include virtual="somefilename"-->

Blank page for jquery.SPServices that list a sharepoint List

I have a SharePoint List that I wish to view it on a custom aspx file.
The SharePoint List is named - "AM_Code"
Inside the List, there are multiple columns but I just want those rows with the column 'Title' that is not null.
The data will then be displayed on the screen.
The code as follows:
<%# Page Language="C#" %>
<html dir="ltr">
<head runat="server">
<META name="WebPartPageExpansion" content="full">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing JQuery with Sharepoint List</title>
</head>
<body>
<form id="form1" runat="server">
</form>
<script type="text/javascript" language="javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript" language="javascript" src="jquery.SPServices-0.6.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$().SPServices({
operation: "GetListItems",
async: false,
listName: "AM_Code",
CAMLViewFields: "<Query><Where><IsNotNull><FieldRef Name="Title" /></IsNotNull></Where><OrderBy><FieldRef Name="Title" Ascending="True" /></OrderBy></Query>",
completefunc: function (xData, Status) {
$(xData.responseXML).find("[nodeName='z:row']").each(function() {
var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
$("#tasksUL").append(liHtml);
});
}
});
});
</script>
<ul id="tasksUL"/>
</body>
</html>
However nothing was being displayed. Please advise if I have miss anything.
CAMLViewFields: "<Query><Where><IsNotNull><FieldRef Name="Title" /></IsNotNull></Where><OrderBy><FieldRef Name="Title" Ascending="True" /></OrderBy></Query>",
this doesn't seem right to me
Actually I found my own answer.
The CAMLViewFields contain the following in order to work:
"<Query><Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where><OrderBy><FieldRef Name='Title' Ascending="True" /></OrderBy></Query>",
Hope it help anyone who is also looking for such solution.

Why does the asp.net head control mangle link elements?

Suppose I have the following markup in a standard ASP.NET 2.0 web form:
<head runat="server">
<title>My Snazzy Page</title>
<link type="text/css" href="<%= PathUtilities.AssetPath %>/css/page.css" rel="stylesheet" />
<script type="text/javascript" src="<%=PathUtilities.AssetPath %>/lib/jquery/1.4.2/jquery.min.js"></script>
</head>
What's odd is that this renders the <link> element literally, with the embedded code brackets, while it interpolates the output of the same code into the script tag. In other words, the browser sees this:
<head><title>My Snazzy Page
</title><link type="text/css" href="<%= PathUtilities.AssetPath %>/css/page.css" rel="stylesheet" />
<script type="text/javascript" src="/rmt/lib/jquery/1.4.2/jquery.min.js"></script>
</head>
Obviously the problem disappears if I remove the runat="server" from the head element.
Well what you're doing is (no offence) a bit silly, that is - having a <head> server-side element, with a nested <link> client-side element with server-side href attribute
You are dynamically rendering the href value from server code anyway, so a better solution would be to dynamically render the link tag from the server altogether.
Example (code-behind of page)
// Define an HtmlLink control.
HtmlLink myHtmlLink = new HtmlLink();
myHtmlLink.Href = "/css/page.css";
myHtmlLink.Attributes.Add("rel", "stylesheet");
myHtmlLink.Attributes.Add("type", "text/css");
// Add the HtmlLink to the Head section of the page.
Page.Header.Controls.Add(myHtmlLink);
Your ASPX then becomes much neater:
<head runat="server">
<title>My Snazzy Page</title>
<!-- CSS/JS included dynamically -->
</head>

Cannot use Response.Write in <head> section of aspx page?

I'm trying to use the Response.Write() method to dynamically insert content in the < head > section of an aspx page. I need to inject a string value from a property on a code-behind object which is a link to my CSS file. However, it is not being processed properly at run time. The object is public on the class and is hydrated in the Page_Load() event. Down in the body of the page I can successfully inject other properties from the Corpoartion object with no problem at all.
Why does this not work in the < head > section?
This is the part that does not expand correctly:
<link href="<%= Corporation.PageStyleSheet %>" rel="stylesheet" type="text/css" />
Here is the entire < head > section:
<head runat="server">
<title></title>
<link href="<%= Corporation.PageStyleSheet %>" rel="stylesheet" type="text/css" />
<script language="JavaScript" type="text/JavaScript" src="cntv_menu.js"></script>
<script language="JavaScript" type="text/JavaScript" src="cntv_category.js"></script>
</head>
What is the reason that this will not expand properly?
You can't use <%= %> inside a runat="server" tag, which your <head> tag is.
You can either change it to <%# %> and DataBind to it in the code-behind, or you can make the link tag runat="server", give it an id and assign the attribute from the code behind.
See this answer, which goes into the details.
Use this:
this.myButton.Attributes.Add(attribute, value);
It worked for me :)
the best way to resolve this problem is using OnPreRender
Example:
First, define your tag:
<link href="~/css/your_default.css" type="text/css" runat="server" id="myCSS" />
And on the OnPreRender:
protected override void OnPreRender(EventArgs e){
base.OnPreRender(e);
myCSS.Attributes["href"] = "~/css/your_new.css";
}
If you write out the full line you should be ok:
<%
Response.write("<link href=\"" + Corporation.PageStyleSheet + "\" rel=\"stylesheet\" />");
%>
P.S. My syntax may not be completely right, sorry in advance.

ASP.NET masterpages: how to insert markup in the head section inside the aspx?

I know I can access the head section of a page which uses a masterpage programmatically this way (in code behind):
This is only an example (I'd like to insert scripts and styles etc.):
this.Header.Title = "I just set the page's title";
Is there a simple way to do this in a declarative way on in the aspx file itself?
Sometimes it would be handy to insert a client script or a style declaration or a link to an external resource.
You can do this by using content regions in the head, in exactly the same way as you would in the body of the page. eg, In your masterpage:
<head>
<link type="text/css" rel="stylesheet" href="/styles/common1.css" />
<script type="text/javascript" src="/scripts/common1.js"></script>
<asp:contentplaceholder id="ExtraStylesAndScripts" runat="server" />
</head>
And then in the page itself just something like:
<asp:content contentplaceholderid="ExtraStylesAndScripts" runat="server">
<link type="text/css" rel="stylesheet" href="/styles/extra1.css" />
<link type="text/css" rel="stylesheet" href="/styles/extra2.css" />
<script type="text/javascript" src="/scripts/extra1.js"></script>
<script type="text/javascript" src="/scripts/extra2.js"></script>
</asp:content>
For stylesheet you can use this :
HtmlLink cssRef = new HtmlLink();
cssRef.Href = "addins/main.css";
cssRef.Attributes["rel"] = "stylesheet";
cssRef.Attributes["type"] = "text/css";
Page.Header.Controls.Add(cssRef);
For Meta Tags :
HtmlMeta metaTag = new HtmlMeta();
metaTag.Name = "author";
metaTag.Content = "ScarletGarden";
Page.Header.Controls.Add(metaTag);
But there is no way to add external script files to header element.
You can add inside body element by :
if (!ClientScript.IsClientScriptIncludeRegistered("myExternalScript"))
{
ClientScript.RegisterClientScriptInclude("myExternalScript", "js/myJSFile.js");
}
Hope this helps !
You can declare the page title in the content page declaration.
<%# Title="Page Title" Page Language="C#" AutoEventWireup="true" CodeFile="Subpage.aspx.cs" Inherits="Subpage" MasterPageFile="~/MasterPage.master" %>
I haven't tried this.
But you can put HEAD element inside html with the enclosed string in asp style markup.
e.g.
<%=myTitle%>

Resources