简体   繁体   English

asp.net动态添加用户控件并将其定位

[英]asp.net dynamically add usercontrols and position them

My issue is that I have a designer that will create a custom aspx page bu without any .net controls. 我的问题是我有一个设计师,它将创建一个没有任何.net控件的自定义aspx页面bu。 I need a way of adding the controls dynamically. 我需要一种动态添加控件的方法。 So far the only types of controls will be textboxes and a button, but there are 30 variations of what the textboxes can be (name, phone #, email, etc). 到目前为止,控件的唯一类型是文本框和按钮,但是文本框可以有30种变化(名称,电话号码,电子邮件等)。 Also the textboxes may or may not need to be required. 此外,可能需要也可能不需要文本框。 Once the textboxes are added the form will be submitted to a db. 添加文本框后,表单将提交给数据库。

My first thought was to have the designer place something like [name] and then replace that with a user control that has a name textbox and a required field validator. 我首先想到的是让设计师放置[name]之类的东西,然后用具有名称文本框和必填字段验证器的用户控件替换。 In order to determine if the validator should be enabled I was thinking that the place holder could look like this, [name;val] or [name;noval]. 为了确定是否应启用验证程序,我认为占位符可能看起来像这样[name; val]或[name; noval]。 I could either do replace the place holders in code dynamically or set up a tool that the user pastes their html into a textbox and clicks a button which then spits out the necessary code to create the aspx page. 我可以动态替换代码中的占位符,也可以设置一个工具,用户将其html粘贴到文本框中,然后单击一个按钮,然后吐出必要的代码以创建aspx页面。

I'm sure there must be a better way to do this but its a fairly unique problem so I haven't been able to find any alternatives. 我确信必须有更好的方法来做到这一点,但这是一个相当独特的问题,所以我一直找不到任何替代方法。 Does anyone have any ideas? 有人有什么想法吗?

Thanks, 谢谢,

Kirk 柯克

IF your designer gives you html pages, just create a new website. 如果您的设计师为您提供html页面,则只需创建一个新网站即可。 copy and pages all the HTML pages with the Image folders and everything to your project. 将所有HTML页面以及Image文件夹以及所有内容复制并分页到您的项目中。 then for every HTML page create an aspx page, (with the same name) copy and pages the html's tags which are between to the aspx page's and for the body copy and paste HTML page's tags which are between into the of the aspx page. 然后为每个HTML页面创建一个aspx页面,(使用相同的名称)复制并分页位于aspx页面和html之间的html标签,并为正文复制并将HTML页面的标签(位于之间)并粘贴到aspx页面。 Now you have your aspx page, exactly the same as html page. 现在您有了aspx页面,与html页面完全一样。

Sounds like an attempt to over-engineer a solution to what should be a non-issue. 听起来像是试图过度解决应该不是问题的解决方案。

As @Alessandro mentioned in a comment above, why can't the designer provide you with pages that have the control markup? 正如@Alessandro在上面的评论中提到的,设计者为什么不能为您提供带有控件标记的页面? As it stands right now, the designer isn't providing you with "a custom aspx" so much as "a custom html page." 就目前而言,设计人员没有像“自定义html页面”那样为您提供“自定义aspx”。 If the designer is promising ASPX but delivering only HTML, that's a misinterpretation somewhere in the business requirements. 如果设计人员承诺使用ASPX但仅提供HTML,那将是对业务需求的误解。

However, even if the designer is rightfully providing only HTML, there shouldn't be a problem with that. 但是,即使设计人员正确地仅提供HTML,也应该没有问题。 At worst, you can set each element you need on the server to runat="server" to access them on the server-side. 最坏的情况是,您可以将服务器上需要的每个元素设置为runat="server"以在服务器端访问它们。 Or, probably better, would be to simply replace them with the ASPX control markup for the relevant controls. 或者,可能更好的方法是将它们简单地替换为相关控件的ASPX控件标记。

Write a simple parser that will recognize the [...] tags and replace them with corresponding controls. 编写一个简单的解析器,将识别标记并将其替换为相应的控件。 Its pretty easy to do and i've often done this... the tag i use is usually $$(..); 它非常容易做到,我经常这样做。我使用的标签通常是$$(..); though, but that doesn't matter as long as your parser knows your tags. 虽然,但是只要您的解析器知道您的标签就没有关系。

Such a parser will consist of a simple state-machine that can be in two states; 这样的解析器将由一个简单的状态机组成,该状态机可以处于两种状态。 text-mode or tag-mode. 文字模式或标记模式。 Loop through the whole page-text, char for char. 循环浏览整个页面文本,char表示char。 As long as you're in text-mode you keep appending each char into a temporary buffer. 只要您处于文本模式,就一直将每个字符追加到临时缓冲区中。 As soon as you get into tag-mode you create a LiteralControl with the content of the temporary buffer and add it to the bottom of your Control-tree, and emtpy the buffer. 进入标记模式后,立即创建一个带有临时缓冲区内容的LiteralControl,并将其添加到Control-tree的底部,并清空该缓冲区。

Now, you still keep adding each char into the buffer, but when you hit text-mode again, you analyze the content of the buffer and create the correct control - could be a simple switch case statement. 现在,您仍然继续将每个字符添加到缓冲区中,但是当再次点击文本模式时,您将分析缓冲区的内容并创建正确的控件-可能是一个简单的switch case语句。 Add the control to the bottom of your control tree and keep looping through the rest of the chars unto you read the end and keep switching back and forth between text-mode and tag-mode adding LiteralControls and concrete controls. 将控件添加到控件树的底部,并不断循环浏览其余字符,直到您读完结尾,并在添加LiteralControls和具体控件的文本模式和标记模式之间来回切换。

Simple example of such a parser... written in notepad in 4 minutes, but you should get the idea. 这样的解析器的简单示例...在4分钟内用记事本编写,但是您应该明白了。

foreach (var c in text)
{
   buffer.Append(c);

   if (c== '[' && mode == Text) 
   { 
      mode = Tag;

      Controls.Add(new LiteralControl(buffer));
      buffer.Clear();
   }

   if (c == ']' && mode == Tag) 
   {   
      mode = Text;

      switch (buffer)
      {
         case "[name]": Controls.Add(new NameControl());
         ... the rest of possible tags
      }

      buffer.Clear();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM