page 1 of 2
Comments: 28 | Views: 1311
turrican
turrican
Condemnation without investigation is the height of ignorance! - Albert Einstein

This thing won't do anything... any idéa why?

protected void Page_Load(object sender, EventArgs e)
  {
  ClientScriptManager CS = this.ClientScript;
  StringBuilder SB = new StringBuilder();
  SB.Append("window.alert(\"1111\");");
  CS.RegisterClientScriptBlock(this.GetType(), "MyScript", SB.ToString(), true);
  }

Edit : The script wont' even show if I do "View source", I even tried RegisterStartupScript.

Thanks!

W3bbo
W3bbo
The Master of Baiters
Try this example taken from MSDN (not that I disapprove of the ClientScriptManager API)

( ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system.web/html/2f72329c-2263-9feb-11b6-b3fa14498b7b.htm )


<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public void Page_Load(Object sender, EventArgs e)
{
// Define the name and type of the client scripts on the page.
String csname1 = "PopupScript";
String csname2 = "ButtonClickScript";
Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
String cstext1 = "alert('Hello World');";
cs.RegisterStartupScript(cstype, csname1, cstext1, true);
}

// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
{
StringBuilder cstext2 = new StringBuilder();
cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
cstext2.Append("Form1.Message.value='Text from client script.'} </");
cstext2.Append("script>");
cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form id="Form1"
runat="server">
<input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
</form>
</body>
</html>

W3bbo
W3bbo
The Master of Baiters
Did you set <xhtmlConformance mode="Strict" /> in Web.config?


Note that ASP.NET does not recognise the W3C Validator's UA string and will automatically revert to Transitional Conformance regardless of the <xhtmlConformance /> configuration element. You'll need an overarching default.browser file to override it all.
W3bbo
W3bbo
The Master of Baiters
The name="" attribute for <input />, <textarea>, and <select> elements isn't just allowed, it's actually required to give name/value pair results. The name="" attribute is only bad if it's used as id="". ASP.NET is doing it right.

As for .browser files:

ASP.NET (stupidly, some might say) uses a whitelist browser caps ("capabilities") system to determine what capabilities (Java, Flash, ECMAScript support, maximum supported DOM level, etc) the current UA supports by matching the User-Agent string against a database of .browser files located in the filesystem. Each .browser file contains the listing of capabilities in an XML format.

.browser files have been around since the days of "classic" ASP (they were in .ini format rather than XML though), so they're nothing new.

ASP.NET first tries the file "default.browser" and if the regular expression or wildcard contained within matches the current UA then it uses that. The .browser file tells ASP.NET how it should render content in ways that cannot be defined in web.config. I've created my own default.browser file that makes ASP.NET assume that every UA (including the W3C Validator) supports everything IE6 does (which in this day and age... does).

Here it is:

<browsers>
<browser refID="Default">
<capture></capture>
<capabilities>
<capability name="css1" value="true" />
<capability name="css2" value="true" />
<capability name="ecmascriptversion" value="3.0" />
<capability name="frames" value="true" />
<capability name="inputType" value="keyboard" />
<capability name="isColor" value="true" />
<capability name="javaapplets" value="true" />
<capability name="javascript" value="true" />
<capability name="jscriptversion" value="5.7" />
<capability name="preferredRenderingType" value="xhtml" />
<capability name="screenBitDepth" value="32" />
<capability name="supportsAccesskeyAttribute" value="true" />
<capability name="supportsBold" value="true" />
<capability name="supportsCss" value="true" />
<capability name="supportsDivNoWrap" value="true" />
<capability name="supportsFileUpload" value="true" />
<capability name="supportsFontName" value="true" />
<capability name="supportsFontSize" value="true" />
<capability name="supportsImageSubmit" value="true" />
<capability name="supportsItalic" value="true" />
<capability name="supportsMaintainScrollPositionOnPostback" value="true" />
<capability name="supportsMultilineTextBoxDisplay" value="true" />
<capability name="supportsVCard" value="true" />
<capability name="supportXmlHttp" value="true" />
<capability name="tables" value="true" />
<!-- Don't use XhtmlTextWriter since <form> renders without the <div> wrapper around the hidden inputs -->
<capability name="tagwriter" value="System.Web.UI.HtmlTextWriter" />
<capability name="w3cdomversion" value="2.0" />
<capability name="xml" value="true" />
</capabilities>

<controlAdapters markupTextWriterType="System.Web.UI.HtmlTextWriter">

</controlAdapters>
</browser>
</browsers>

stevo_
stevo_
Casablanca != Manchester
Pretty sure that in xhtml 1.0, some browsers require the name attribute on form fields, they don't always use the id attribute.. to say this is your first asp.net application (I'm assuming since all the questions), its really nuts to be aiming for an xhtml 1.0 strict compilation.. you may well think that you should learn the best first.. but as far as xhtml strict goes right now.. the browser support just isn't good.. you put lots of restrictions on yourself where the replaced functionality just isn't well supported..

Turn on Strict XHTML conformance in your Web.config again:

<configuration>
  <system.web> 
    <xhtmlConformance mode="Strict"/>
  </system.web>
</configuration>

What's the problem with the name attribute for your INPUT elements? While the name attribute may be deprecated, it is still valid XHTML.

stevo_
stevo_
Casablanca != Manchester
Aw how cute, you think all the difference is the looks? plus now you've already fallen back to transitional.. wow, didn't take long.
W3bbo
W3bbo
The Master of Baiters
Epic Fail.


Turrican:

Please, listen to me: you're doing it wrong.

The name="" attribute is valid in XHTML1.0 Strict and XHTML1.1 for <input />, <textarea>, and <select>. It is a required attribute of those elements to get forms to work in the first place. Check the specification if you don't believe me.

Do not confuse it with the name="" attribute on the <form> element, which ASP.NET only serves up if you have <xhtmlConformance mode="" /> not set to "Strict" or you haven't overriden the Browser definitions.