在 C# 中验证 Sharepoint 2007 Web 部件表单字段

发布于 2024-10-05 07:32:19 字数 1325 浏览 0 评论 0原文

我正在创建一个非常简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以笙箫默 2024-10-12 07:32:19

Doh - 现在我意识到我必须将其作为单独的控件添加到页面中。

tc.Controls.Add(messageRequiredValidator);

Doh - now I realise that I have to add this as a separate control to the page.

tc.Controls.Add(messageRequiredValidator);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文