动态控制 C# 上的验证器噩梦
我需要将RequiredFieldValidator和RegularExpressionValidator添加到动态生成的表格单元格中动态创建的文本框中,该文本框位于从母版创建的页面的内容区域中的Web用户控件内。
正如您可能猜到的那样,问题是尝试动态设置 ControlToValidate 属性来查看我动态创建的文本框。
经过一些研究后,现在的代码:
- 创建一个面板(据我所知,ControlToValidate 和 Validator 必须位于同一容器内)。 这最初是一个占位符,但正在尝试下面列出的建议。
- 创建文本框并设置其 ID。
- 将文本框添加到面板。
- 创建RequiredFieldValidator。
设置 ControlToValidate 的 ID。 我尝试使用的值:
- 控件的 ID
- 控件的 ClientID
- 以服务器附加到 Web 用户控件的子控件的添加文本为前缀的控件 ID
- 以同样的方式修改客户端 ID
- 控件的名称(如果有的话)
- 控件名称,以服务器添加到控件名称的文本为前缀
- 使用定制的递归 FindControl 方法尝试将新的 Control 对象转换为 Textbox,然后使用其 ID 和 ClientID
- 控件的 UniqueID
- 与上面详述的前缀相同的修改
- 将验证器添加到面板。
- 将面板添加到表格单元格。
不用说,我仍然无法说服验证器“看到”它应该验证的控件,并且我完全没有新的方法来解决这个问题。
编辑:进一步的侦探工作使我发现,在 page_load 事件完成之前,页面没有问题。 在构建页面的代码执行完毕后,服务器似乎出现了问题。 我开始怀疑我是否真的将控件添加到命名容器中太晚而不是太早。
有什么建议么?
I have a requirement to add a RequiredFieldValidator and RegularExpressionValidator to a dynamically created textbox in a dynamically generated tablecell, inside a Web User Control in the Content Area of a Page created from a Master.
The problem, as you can probably guess, is trying to dynamically set the ControlToValidate property to look at my dynamically created text box.
After some research the code now:
- Creates a Panel (As I have heard the ControlToValidate and Validator must be within the same container). This was originally a placeholder but was trying a suggestion listed below.
- Creates the Textbox and sets its ID.
- Adds the Textbox to the Panel.
- Creates the RequiredFieldValidator.
Sets the id of the ControlToValidate. Values I have attempted to use:
- The ID of the control
- the ClientID of the control
- the ID of the control prefixed by the added text the server appends to child controls of the Web User Control
- the Client ID modified the same way
- the name of the control (on the off chance)
- the name of the control prefixed by the text the server adds to the names of controls
- using a bespoke Recursive FindControl Method in an attempt to cast a new Control object to Textbox and then using its ID and ClientID
- the UniqueID of the control
- the same modified with the prefix as detailed above
- Add the validator to the panel.
- Add the panel to the tablecell.
Needless to say I am still unable to convince the Validator to "see" the control it is supposed to validate and I am completely out of new ways to approach the problem.
EDIT: Further detective work has lead me to the point that the page doesn't have a problem until the page_load event has finished. The server seems to have a problem after the code for building the page has finished executing. I'm starting to wonder if I'm actually adding the controls into naming containers much too late instead of too early.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
创建一个包含文本框和两个验证器的用户控件怎么样? 然后,您可以像往常一样通过 Visual Studio 设置
ControlToValidate
,然后动态地将这个新控件添加到您的表格单元格中。What about creating a user control that contains the textbox and the two validators? Then you can set the
ControlToValidate
via Visual Studio, as usual, and then dynamically add this new control to your tablecell dynamically.我在类似的情况下使用了中继器:
转发器创建一个“命名容器”,以确保 FieldData 控件 ID 在容器内是唯一的。
I used a repeater in a similar situation:
The repeater creates a "naming container" that ensures that the FieldData control ID is unique within the container.
这是一个示例:
在设计时使用 ID =“PanelHolder”将 Panel 控件添加到页面(或者您可以动态添加)。
然后动态创建控件并将它们添加到该面板,如下所示:
here is an example:
Add a Panel control to your page at design time with the ID = "PanelHolder"(or you can add dynamically).
then create your controls dynamically and add them to that panel like this:
只是几个问题:
发回页面的控件是否会导致验证? (如果是这样,请确保它不在单独的验证组中)
您确定没有进行验证吗? 如果您没有设置验证器的 ErrorMessage 属性,可能很容易认为它没有执行任何操作。 (我看不到您将其设置在列表中)
编辑:
如果您正在执行以下操作:
那么您将收到一个错误,因为只有一个文本框实例将添加到控制收集。
如果移动 ' TextBox tb = new TextBox(); ' 部分在循环内,那就没问题了。
我不确定这是否是您的问题,但值得一试。
Just a couple of questions:
Is the control that's posting the page back causes validation? ( if so, make sure it's not in a separate validation group )
Are you sure there's no validation happening? If you don't set the ErrorMessage property of the validators it might be easy to think it's not doing anything. (and I can't see you setting it on your list)
Edit:
If you're doing something like this:
Then you will get an error as only one instance of the textbox will be added to the controls collection.
If you move the ' TextBox tb = new TextBox(); ' part inside the loop tho, it'll be fine.
I'm not sure if this's your problem, but worth a try.
我为网格内的该文本框生成一个文本框和必填字段验证器。
我首先尝试使用文本框的 clientID 作为 requiredfield 验证器的 controltovalidate 属性
这给出了无法找到控制错误
比我将 texbox 的 ID 作为 requiredfield 验证器的 controltovalidate 属性给出,它对我有用。下面的代码返回作为方法的第一个参数给出的控件的正则表达式验证器。
i generate a texbox and requiredfield validator for that texbox inside a grid.
I first tried to use textbox's clientID as controltovalidate property of requiredfield validator
this gived unable to find control error
than I gived texbox's ID as controltovalidate property of requiredfield validator and it was worked for me.The code below returns a RegularExpressionValidator for the control that gived as first argument for method.