3

I've created a Windows Forms Application in C# which allows users to add controls to a TabPage which they can resize and reposition. Now I want to do the same thing only in ASP.NET.

I managed to add the controls dynamically following this tutorial. I use jQuery UI to make them resizable and draggable. The problem I encountered is that when I add a new control all the others are reverted to their initial position and dimension.

I assume I have to save their position and size and apply them to the newly created control on LoadViewState. Is there a way I can view this attributes from code-behind? I've managed to get these info using Javascript but I don't know how to get it into code-behind.

Can someone please point me in the right direction? Thanks in advance.

[EDIT] Thank you for your answers. Here's the code: HTML C#

1 Answers1

1

You need to store the positions and dimensions of the controls and pass those values to the server when you click the add control button.

You have a few options of how to do this.

  • Query String
  • Hidden Form Fields (<input type="hidden">)
  • Hidden Text Box (hidden with style="display: none;")

You can use JavaScript to set these values, then apply the positions in your code behind after adding a new control.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
  • 1
    Just to extend the answer given, you would need to declare your hidden fields as Server Controls and then push the values into them using JavaScript on the client when you move an object around and then when the postback happens, your location values would be available at the server side. You've a number of options here, one super hidden field that contains all of your location values encoded in some way, or a hidden field for each attribute for each control that's added to the page. – pb. May 06 '11 at 16:45
  • @pb Not necessarily. Hidden form field that aren't server controls can be accessed by the `Request.Params` in the code behind, keyed by the `name` property. I've actually found it easier to use a non-server control hidden field if the value is going to be set via javascript. – Kyle Trauberman May 06 '11 at 16:57
  • I've added a Server TextBox in which I've saved the configuration of the controls via JavaScript. But I'm stuck on when to apply this to the controls. I should do it on `LoadViewState` but I can only access the TextBox beginning with `OnPreLoad`. I couldn't figure out how to use `Request.Params`. –  May 07 '11 at 12:32
  • Do it on Page_Load. You might need to wrap it in a `if(Page.IsPostBack)` statement. – Kyle Trauberman May 07 '11 at 17:32
  • @Kyle - the reason I suggested a server control was so that the values are persisted across postbacks, that way @feelshift wouldn't have to worry about maintaining state. – pb. May 10 '11 at 08:41