[英]Required Field Validator For Image Control
Old Title: Prevent dynamically created control from retaining value 旧标题:防止动态创建的控件保留价值
Old Info: The reason I need to do this is because I am trying to create a work around for a required field validator for an Image control. 旧信息:我需要这样做的原因是因为我正在尝试为Image控件的必需字段验证器创建解决方案。 The way my code works is I have a Image control beside a Button, a user clicks on the button and then is prompted to upload an image.
我的代码的工作方式是,我在按钮旁边有一个Image控件,用户单击该按钮,然后被提示上传图像。 I need to ensure that an image is uploaded before the user can move onto the next stage.
我需要确保在用户可以进入下一个阶段之前上传图像。
Since there is no required field validator for an image control, I created a textbox which is suppose to display the imageURL of the image control every time the image control is recreated on postbacks. 由于图像控件没有必需的字段验证器,因此我创建了一个文本框,该框假定每次在回发时重新创建图像控件时都显示图像控件的imageURL。 However, the textbox always retains the value from the initial creation of the control.
但是,文本框始终保留控件最初创建时的值。
* Note: all controls on the page are dynamically created. *注意:页面上的所有控件都是动态创建的。
The first thing I do is create the image control and add it to an HTML Table. 我要做的第一件事是创建图像控件并将其添加到HTML表中。 This works fine.
这很好。 Right after that I find the table cell and add the textbox to the cell that has an image control:
之后,我找到表格单元格并将文本框添加到具有图像控件的单元格中:
HtmlTableCell tc = (HtmlTableCell)customProperties.FindControl("tcControl_" + (i + 1).ToString());
RadBinaryImage rbi = (RadBinaryImage)customProperties.FindControl("CustomControl" + (i + 1).ToString());
TextBox photoValue = new TextBox();
photoValue.ID = "CustomControl" + (i + 1).ToString() + "_txt";
photoValue.Text = rbi.imageUrl;
This occurs everytime I create all the controls. 每当我创建所有控件时都会发生这种情况。 For all the controls they all retain their values, this is the only control which I don't want this to happen.
对于所有控件,它们都保留其值,这是我不希望发生的唯一控件。 Does anyone know of how this can be done?
有谁知道如何做到这一点? Or another way of validating an image control?
还是验证图像控件的另一种方法?
Thanks for your time, All comments/answers are appreciated (: 多谢您的宝贵时间,所有评论/答案均受到赞赏(:
SOLVED: 解决了:
I created a modified version of a checkboxlist required field validator that I found here. 我创建了一个在此处找到的checkboxlist必填字段验证器的修改版本。 Here is the code: I replaced the namespace with ######## for security reasons.
这是代码:出于安全原因,我用#########替换了名称空间。
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Telerik.Web.UI;
namespace #######################################
{
public class RequiredFieldValidatorForImages :
System.Web.UI.WebControls.BaseValidator
{
private Control _ctrl;
public RequiredFieldValidatorForImages()
{
base.EnableClientScript = false;
}
protected override bool ControlPropertiesValid()
{
Control ctrl = FindControl(ControlToValidate);
if (ctrl != null)
{
_ctrl = (Control)ctrl;
return (_ctrl != null);
}
else
return false; // raise exception
}
protected override bool EvaluateIsValid()
{
try
{
Image rbi = (Image)_ctrl;
return rbi.ImageUrl != "~/images/noimages.jpg";
}
catch
{
RadBinaryImage rbi = (RadBinaryImage)_ctrl;
return rbi.ImageUrl != "~/images/noimages.jpg";
}
}
}
}
Solved: 解决了:
I created a modified version of a checkboxlist required field validator that I found here. 我创建了一个在此处找到的checkboxlist必填字段验证器的修改版本。 Here is the code: I replaced the namespace with ######## for security reasons.
这是代码:出于安全原因,我用#########替换了名称空间。
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Telerik.Web.UI;
namespace #######################################
{
public class RequiredFieldValidatorForImages :
System.Web.UI.WebControls.BaseValidator
{
private Control _ctrl;
public RequiredFieldValidatorForImages()
{
base.EnableClientScript = false;
}
protected override bool ControlPropertiesValid()
{
Control ctrl = FindControl(ControlToValidate);
if (ctrl != null)
{
_ctrl = (Control)ctrl;
return (_ctrl != null);
}
else
return false; // raise exception
}
protected override bool EvaluateIsValid()
{
try
{
Image rbi = (Image)_ctrl;
return rbi.ImageUrl != "~/images/noimages.jpg";
}
catch
{
RadBinaryImage rbi = (RadBinaryImage)_ctrl;
return rbi.ImageUrl != "~/images/noimages.jpg";
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.