13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

var parser = new ActiveXObject("<strong>Microsoft</strong>.<strong>XML</strong>DOM");// Parse the contents of the data island and makes// it available as a <strong>XML</strong> DOM object. The object is given// the same name as the tag's IDvar xmldoc = parser.load<strong>XML</strong>(xmldata);If your f<strong>in</strong>al goal is consum<strong>in</strong>g the data island with<strong>in</strong> the body of a managed control,there is no need to pass through a COM-based <strong>in</strong>termediate representation of the <strong>XML</strong>data. In this case, <strong>in</strong> fact, the parser that will actually process the data is the .<strong>NET</strong>Framework <strong>XML</strong> reader. The reader needs only a str<strong>in</strong>g of <strong>XML</strong> data, not a COMobject. On the other hand, whenever you use the tag, Internet Explorerautomatically creates the <strong>XML</strong>DOMDocument object. So if the f<strong>in</strong>al dest<strong>in</strong>ation of thedata island is a W<strong>in</strong>dows Forms control, you might want to speed th<strong>in</strong>gs a little bit bynot us<strong>in</strong>g the tag, which will produce a useless COM <strong>XML</strong> DOM object. Us<strong>in</strong>g ahidden field offers the same functionality at a lower price. But keep <strong>in</strong> m<strong>in</strong>d that thisoption is valid only if you plan to consume the data island contents through embeddedmanaged code.The Role of Script CodeTo establish a connection between the host environment and the managed control, youmust use script code—Javascript <strong>in</strong> particular. For this reason, while you're design<strong>in</strong>gthe <strong>in</strong>terface of the managed control, don't <strong>for</strong>get what the actual callers of thosemethods will be. A Javascript client has different capabilities than a .<strong>NET</strong> Frameworkclient, so you should keep the signature of public methods as simple as possible andavoid us<strong>in</strong>g arrays and other complex and user-def<strong>in</strong>ed types.In the dataisland.aspx sample code, the connection between the data island and themanaged control is made through the Load method. The Load method accepts a simplestr<strong>in</strong>g, which results <strong>in</strong> a signature that the Javascript code can easily match, as shownhere:// At this po<strong>in</strong>t, Internet Explorer has already created// the <strong>XML</strong>DOMDocument. You can retrieve the content of the// data island either through the <strong>XML</strong>Document object or// the <strong>in</strong>nerHTML property.var data = xmldoc.<strong>XML</strong>Document.xml;// Pass the data island content to the managed controlvar listView = document.all("grid");listView.Load(data);Avoid<strong>in</strong>g Problems with Submit ButtonsWhile develop<strong>in</strong>g the sample ASP.<strong>NET</strong> page to test the DataListView object, I ran <strong>in</strong>toan <strong>in</strong>terest<strong>in</strong>g snag. I orig<strong>in</strong>ally used the tag to <strong>in</strong>sert a button to load thedata island <strong>in</strong>to the control. As a result, the data island was correctly read and thecontrol filled, but a moment later the page refreshed, and the control lost its state andwas displayed as empty. What happened? The reason <strong>for</strong> this strange behavior is thatthe tag always generates a submit button, as shown here:500

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!