Register Login
Home    July 29, 2010
Features & Links Minimize
 
Useful Links Minimize
 
Local Jobs (from other sites) Minimize
Print  
 
Links Minimize
Print  
 
Local News Minimize
 
Fox News Headlines Minimize
 
CNN News Headlines Minimize
Print  
 
Daphne News and Headlines Minimize
Print  
 
Foley News and Headlines Minimize
Print  
 
Gulf Shores News and Headlines Minimize
Print  
 
Orange Beach News and Headlines Minimize
Print  
 
Robertsdale News and Headlines Minimize
Wed, 28 Jul 2010 02:16:00 -0500

Realtor office opened
Gulf Coast Newspapers
ROBERTSDALE – The Baldwin County Association of Realtors has moved. The association, which according to Executive Officer Martha Taylor, ...

Tue, 27 Jul 2010 13:45:14 -0500

Get to Know Juan Martinez: Robertsdale business owner is creative in his work
Press-Register - al.com (blog)
ROBERTSDALE, Alabama -- Being creative is something Robertsdale business owner Juan Martinez implements daily in his work. The owner and chef of The Bake ...

Fri, 23 Jul 2010 14:10:43 -0500

Press-Register - al.com (blog)

Central Baldwin Chamber hosts annual Flavors of the South event
Gulf Coast Newspapers
By Independent Staff ROBERTSDALE, Ala. — Over 20 vendors served a packed house on Thursday, July 22 at the Central Baldwin Chamber of Commerce's annual ...
Flavors of the South a treat for the mouthGulf Coast Newspapers
Get to Know Diane Hintz: Banking and chamber prove to be complementaryPress-Register - al.com (blog)
Central Baldwin Chamber holds Flavors of the SouthPress-Register - al.com (blog)

all 4 news articles »
Fri, 23 Jul 2010 05:15:13 -0500

Gulf Coast Newspapers

Fill the Bus campaign to provide supplies for students
Gulf Coast Newspapers
ROBERTSDALE, Ala. — As a way to help out families who may be struggling with the tough economic times, Project CARE, ...

Fri, 23 Jul 2010 05:15:12 -0500

Robertsdale Public Works Department to take bids for truck
Gulf Coast Newspapers
ROBERTSDALE, Ala. — A request to solicit bids for a new Knuckle boom debris/trash loader truck for the Public Works ...
Bear Club to hosts Drawdown fundraiserGulf Coast Newspapers

all 2 news articles »
Print  
 
Recent Entries from Local Blogs Minimize
Jun 10

Written by: anomaly
6/10/2005 8:23 AM 

In the July issue of MSDN Magazine, Dino Esposito provided an excellent article on allowing DHMTL in ASP.Net Controls.  This is a core concept for a project that I am working on,  so I thought I would share my particular solution to the problem.

First, the problem.   -- If you use DHTML and client-side scripting to manipulate server side objects, whatever attributes of those HTML elements that were changed since initial page render via DHTML will be lost on postback.   My particluar need for DHTML was related to allowing users to move controls around a webform (a form designer) and then saving the new X/Y coordinates of those controls for later use. 

Inspired from Mr. Esposito's article, I decided to model my solution using a single hidden field.   I wasn't excited about this approach but I did consider it to me a good alternative to the common approach of adding one hidden field per DHTML-manipulated server control.

Since my particular need was to capture X and Y coordinates of controls that have been moved around a webform, in the DHTML javascript events fired after the control is dropped into place, I added code to se custom attributes representing the current coordinates.   In this case dd.obj is whatever object was moved.

document.getElementById(dd.obj.name).setAttribute("x",dd.obj.x);
document.getElementById(dd.obj.name).setAttribute("y",dd.obj.y);

This code adds the custom attributes, X and Y to the DHTML layer that was dragged to a new location.   Now, I must be able to persist these custom attributes through the postback.

To the webform, or an external .js referenced by the webform, I added a javascript function to enumerate through all attributes for all divs and set a hidden field's value to a text string that we will be able to reverse engineer on postback to re-set those attributes. (Or, do whatever we would like)

function setAttribState(hiddenID){
 var attribState="";
 //Which HTML tags should we include in attribstate?
 //Div only for now.
 var tagnames = new Array();
 tagnames[0]='div';
 
 for(var h=0;h  var elem = document.getElementsByTagName(tagnames[h]);
  for(var i=0;i   var thisElement ="";
   var attribs = elem[i].attributes;
   var eleAttribs="";
   for(var j=0;j    if (attribs[j].specified){
     var thisAttrib = "";
     var thisAttrib = attribs[j].nodeName + ':' + attribs[j].nodeValue;
     eleAttribs += thisAttrib;
     if (j!=attribs.length-1){eleAttribs+=','};
     }
   }
   thisElement = elem[i].id + '/' + eleAttribs;
   attribState += thisElement;
   if(i!=elem.length-1){attribState+=';'};
  }
 }
 document.getElementById(hiddenID).setAttribute("value",attribState);
}
To the webform, add a hidden field called "__attribState".
Then, in the codebehind for the webform  I wired this javascript into the submit button's onclick event.
I did this by adding this to the page_load event: 
Button1.Attributes.Add("onclick", "setAttribState('__attribState')")
 In the same codehind, under the form click event, I added a call to iterate through the text and set the attributes.
 ReadAttribState("__attribState")

To the webform, I added the sub:

    Private Sub ReadAttribState(ByVal HiddenControlName As String)
        Dim hiddenctl As HtmlControls.HtmlInputHidden = CType(Page.FindControl(HiddenControlName), HtmlControls.HtmlInputHidden)
        Dim elements As String() = Split(hiddenctl.Value.ToString, ";")
        Dim element As String
        For h As Int32 = 0 To elements.GetUpperBound(0)
            Dim x, y As String
            Dim ctlId As String = Split(elements(h), "/")(0)
            Dim ctl As HtmlControls.HtmlGenericControl = Page.FindControl(ctlId)
            Dim attribs As String() = Split(Split(elements(h), "/")(1), ",")
            For i As Int32 = 0 To attribs.GetUpperBound(0)

                Dim attribName = Split(attribs(i), ":")(0)
                Dim attribValue = Split(attribs(i), ":")(1)
                If LCase(attribName) <> "id" Then
                    ctl.Attributes.Add(attribName, attribValue)
                End If
            Next
        Next
    End Sub

 Voilà.   When your DHTML event fires, it will set your custom properties.   Then, onClick, the form button will build a text string of all attributes for all divs and populate the hidden field.  On postback, your code will poll this field, parse the name/value pairs for each attribute and re-set those attributes on page render. In my implementation, I then use these custom attributes to set the style of my controls on page_load.

Until Next Time,

Bill Dodd
mcp, mcsd, mcad, mcdba

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
 
 Happy Holidays!   Terms Of Use  Privacy Statement