在 C# 中验证 Sharepoint 2007 Web 部件表单字段
我正在创建一个非常简单的 Web 部件,它接受表单文本字段中的输入,然后对其执行某些操作。在执行此操作之前,我想验证文本字段的内容。它必须存在,并且必须少于 250 个字符。我知道我可能可以编写一些自定义代码来执行此操作,但 Sharepoint 包含验证功能,所以为什么要重新发明轮子呢。
然而,经过一个小时搜索一些关于我想要实现的目标的文档后,我发现很多解释了如果我编写 ASP.NET 代码如何执行此操作,但如果我在 Visual Studio 2008 中用 C# 编写 Web 部件,则很少。剩下的唯一一点就是验证。
所以,我的问题是如何验证表单上的字段。
我的字段名为 txtMessage。这是我编写的代码:
// Add the form field to the web part
tc = new TableCell();
tc.VerticalAlign = VerticalAlign.Top;
txtMessage = new TextBox();
txtMessage.ID = "txtFormField";
txtMessage.Width = Unit.Pixel(300);
txtMessage.MaxLength = 250;
tc.Controls.Add(txtMessage);**strong text**
// Validate form field - required field
RequiredFieldValidator messageRequiredValidator = new RequiredFieldValidator();
messageRequiredValidator.ControlToValidate = txtMessage.ID;
messageRequiredValidator.ErrorMessage = "You must enter text";
messageRequiredValidator.Display = ValidatorDisplay.Dynamic;
messageRequiredValidator.Text = "<img src=\"/_layouts/images/CNSCA16.gif\"/>";
// Send Message button
tc = new TableCell();
btnSendMessage = new Button();
btnSendMessage.Text = "Send";
btnSendMessage.Click += new EventHandler(btnSendMessage_Click);
tc.Controls.Add(btnSendMessage);
tr.Controls.Add(tc);
我真正需要知道的是如何在单击按钮时触发字段验证。我是否需要添加一个新的 EventHandler 来调用验证或其他内容?
I am creating a very simple web part that accepts entry on a form text field and then does something with it. Before doing this, I want to validate the content of the text field. It must exist, and it must be less than 250 characters. I know I could probably write some custom code to do this, but Sharepoint includes Validation features, so why reinvent the wheel.
However, after an hour searching for some documentation on what I want to achieve, I have found plenty that explains how to do this if I am writing ASP.NET code, but very little if writing the web part in C# in Visual Studio 2008. The only bit remaining is the validation.
So, my question is how to validate a field on a form.
My field is called txtMessage. Here is the code I wrote:
// Add the form field to the web part
tc = new TableCell();
tc.VerticalAlign = VerticalAlign.Top;
txtMessage = new TextBox();
txtMessage.ID = "txtFormField";
txtMessage.Width = Unit.Pixel(300);
txtMessage.MaxLength = 250;
tc.Controls.Add(txtMessage);**strong text**
// Validate form field - required field
RequiredFieldValidator messageRequiredValidator = new RequiredFieldValidator();
messageRequiredValidator.ControlToValidate = txtMessage.ID;
messageRequiredValidator.ErrorMessage = "You must enter text";
messageRequiredValidator.Display = ValidatorDisplay.Dynamic;
messageRequiredValidator.Text = "<img src=\"/_layouts/images/CNSCA16.gif\"/>";
// Send Message button
tc = new TableCell();
btnSendMessage = new Button();
btnSendMessage.Text = "Send";
btnSendMessage.Click += new EventHandler(btnSendMessage_Click);
tc.Controls.Add(btnSendMessage);
tr.Controls.Add(tc);
All I really need to know is how to trigger validation of the field when clicking on the button. Do I need to add a new EventHandler that calls the validation, or something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Doh - 现在我意识到我必须将其作为单独的控件添加到页面中。
Doh - now I realise that I have to add this as a separate control to the page.