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.

As a result, the page first executes the client-side script associated with the HTMLbutton and fills the control with the <strong>XML</strong> data. Next the browser posts the page back tothe server as the submit button type mandates. This behavior is undesired <strong>for</strong> a coupleof reasons. First, it produces an unneeded round-trip to the Web server. Second, theround-trip cancels the changes to the user <strong>in</strong>terface that have been made on the clientand that constitute the core of our ef<strong>for</strong>ts and our ma<strong>in</strong> reason <strong>for</strong> build<strong>in</strong>g and us<strong>in</strong>g amanaged control. On the other hand, the W<strong>in</strong>dows Forms control is not a server-sidecontrol and does not have access to the ViewState property to control its state whenthe page posts back.This problem has a simple workaround: don't use the tag to <strong>in</strong>sert abutton that is expected to <strong>in</strong>teract with the managed control through client-side scriptcode. Instead, use the tag and explicitly set the type attribute to button, asshown <strong>in</strong> the follow<strong>in</strong>g code:Also, don't set the runat attribute; if you do, the onclick attribute will be mistaken <strong>for</strong>server-side code to be executed. In this way, the browser executes the associatedclient-side script code and refreshes the page accord<strong>in</strong>gly, but no postback occurs.Us<strong>in</strong>g Hidden Fields and SQL QueriesDespite the fact that the tag is the official way of def<strong>in</strong><strong>in</strong>g <strong>XML</strong> data islands withInternet Explorer, a hidden field is probably a better solution. With a hidden field,Internet Explorer doesn't preprocess the <strong>XML</strong> data <strong>in</strong>to a COM-based <strong>XML</strong> DOMobject. This feature is welcome if you are go<strong>in</strong>g to process the <strong>XML</strong> data us<strong>in</strong>g scriptcode. No pars<strong>in</strong>g is needed if you only plan to pass the <strong>XML</strong> data island to a managedcontrol, however. Us<strong>in</strong>g a hidden field or a hidden tag is a valid approach to <strong>in</strong>sert<strong>in</strong>g<strong>XML</strong> data <strong>in</strong> the body of an HTML page.The follow<strong>in</strong>g code illustrates how to create a hidden field that conta<strong>in</strong>s dynamicallygenerated <strong>XML</strong> data. The data is the output you get from the <strong>XML</strong> normal <strong>for</strong>m of aDataSet object. In this sample code, the DataSet object is obta<strong>in</strong>ed by runn<strong>in</strong>g a queryaga<strong>in</strong>st the Customers table <strong>in</strong> the Northw<strong>in</strong>d database.private void Page_Load(object sender, EventArgs e){if (!IsPostBack){str<strong>in</strong>g xmldata = GetDataAsXml();RegisterHiddenField("xml", xmldata);}}private str<strong>in</strong>g GetDataAsXml(){SqlDataAdapter adapter = new SqlDataAdapter("SELECT customerid, companyname, contactname,contacttitle, city, country FROM customers",501

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

Saved successfully!

Ooh no, something went wrong!