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#

4
  • Are you using ajax (update panels) to communicate with the server, or doing a complete post back of the page? Commented May 5, 2011 at 18:11
  • Currently I'm doing a complete post back. Commented May 5, 2011 at 18:12
  • Can you post your html/asp.net code and javascript? Commented May 5, 2011 at 18:14
  • HTML C# Commented May 5, 2011 at 18:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 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.
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.
Do it on Page_Load. You might need to wrap it in a if(Page.IsPostBack) statement.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.