• 0

[ASP.NET 3.5, C#] I don't even know what the title is ;(.


Question

So, I've been battling with this problem for 2 days now, and I need your help.

What I'm trying to do is the following: I have an HTML form that I need to load dynamically at run time. The HTML code looks something like this:

<form name="form1" method="post" action="action goes here." id="Form1">
<input type="hidden" name="x" VALUE="asdasdasd">
<input type="hidden" name="y" VALUE = "asdasdasd">
<input type="hidden" name="z" VALUE="asdsad">
other tags go here
</form>

I have a lot of forms like the above, and I need to load them dynamically without changing anything

So I tried to put them in a User Control, and each input was turned into a TextBox, and depending on a specific criteria, I load the correct form (user control). That turned to be wrong, since the ID and NAME are changing.

ASP.NET is inserting "dynamic" ID and NAME for each one.

I need a way to load that form at runtime without changing anything. I forgot to mention that values are populated from a database, that's why I chose the Textbox way.

Any ideas fellas? I even thought about inheriting from upper classes that, for example, textbox is derived from.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Any html control that does not have runat="server" as an attribute is not graphed into the control tree, but rendered as pure html by ASP.NET. Your UserControl will have a dynamic ClientID and UniqueID, as is the nature of ASP.NET controls. As you probably know, this is to ensure that each control can be referenced uniquely, depending on Naming containers, etc.

Can we see some more code, how are you loading your UserControls?

Link to comment
Share on other sites

  • 0

Well, I changed the TextBox evilness and I create my own web control.

	public class CustomControl : WebControl
	{
		public string name { get; set; }

		[DefaultValue("")]
		public string value { get; set; }

		protected override void RenderContents(HtmlTextWriter output)
		{
			output.Write("<input type=\"hidden\" value=\""+ value +"\" name=\""+ name +"\" />");
		}

The user control will be something like this (.ascx):

<form id="form" runat="server" action="http://aas.museglobal.com/muse/servlet/MusePeer">

	<mine:CustomControl ID="action" name="action" runat="server"></mine:CustomControl>
	<mine:CustomControl ID="parameterName0" name="parameterName0" runat="server"></mine:CustomControl>
	<mine:CustomControl ID="parameterValue0" name="parameterValue0" runat="server"></mine:CustomControl>
	<mine:CustomControl ID="parameterName1" name="parameterName1" runat="server"></mine:CustomControl>
	<mine:CustomControl ID="parameterValue1" name="parameterValue1" runat="server"></mine:CustomControl>
	<mine:CustomControl ID="queryStatement" name="queryStatement" runat="server"></mine:CustomControl>
	<mine:CustomControl ID="applicationPageId" name="applicationPageId" runat="server"></mine:CustomControl>

</form>

And the code behind will look like this:

		public string Action
		{
			get { return this.action.value; }
			set { this.action.value = value; }
		}
etc .. etc ...

and .... I'm calling it from the page like this:

						tEhControl test = this.LoadControl(@"~\usercontrols\tEhControl.ascx") as tEhControl;
						test .Action = "logon";
						test .QueryStatement = "";
						test .ApplicationPageId = "Advanced";

						this.placeholder.Controls.Add(test);

What do you think? The current problem is that I have to write all of the above, like 20 times. I have to create the user control (markup) and the code behind (the properties) and I have to load the control to the page and set the properties *Sighs*.

Link to comment
Share on other sites

  • 0

I can see what you are doing, but to be honest, it still seems a bit overkill. I'm not sure why ASP.NET would be rendering your non-server controls with unique IDs, because it shouldn't :s

The current problem is that I have to write all of the above, like 20 times

The quick answer there is to put the control loading logic into a function:

public void LoadControl(string url, string action, string queryStatement, string applicationPageId, Control container)
{
  if (url == null) {
	throw new ArgumentNullException("url");
  }

  if (container == null) {
	throw new ArgumentNullException("container");
  }

  tEhControl control = LoadControl(url) as tEhControl;
  if (control == null) {
	throw new InvalidOperationException("The specified control is not of type tEhControl.");
  }

  control.Action = action;
  control.QueryStatement = queryStatement;
  control.ApplicationPageId = applicationPageId;

  container.Controls.Add(control);
}

Link to comment
Share on other sites

  • 0

Good idea.

Well, if you have a very basic user control, lets say:

<form id="submitForm" runat="server" name="submitForm" method="post" action="">
	<asp:TextBox  name="field" ID="field" runat="server"></asp:TextBox>
	<asp:TextBox  name="another" ID="another" runat="server"></asp:TextBox>
</form>

And you're loading the control dynamically to the page, the IDs and Names property will be something like this:

ctl02$field

Link to comment
Share on other sites

  • 0

Lol, but if you do this:

<form id="submitForm" runat="server" name="submitForm" method="post" action="">
	<input id="field" name="field" />
	<input id="another" name="another" />
</form>

It shouldn't generate the dynamic IDs, well it will only for the form element, because of runat="server"

Edit: I've had a thought -_-. If you are planning to use multiple instances of the same control on a page, you would need unique IDs to access them individually anyway, so what would the actual issue be. How are you trying to process their data?

Link to comment
Share on other sites

  • 0

Well, what I posted is a sample form. I have 20 different forms with different amount of fields and fields name and almost everything.

At any one time, there will be 1 instance of a user control loaded. I need to retain their name property because the form will be submitted to another website that is expecting these values.

Link to comment
Share on other sites

  • 0

I see, well as I have previously said, omitting runat="server" form the element markup with stop ASP.NET from creating a control from it. You should then be able to post the data as you would expect.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.