hi
I have a custom control, (relevant code at the bottom)
it consists of some text/links and a hidden textbox (rather than a hidden input). when rendered, the user via another dialog and some JS sets the value of the textbox to an integer and clicks submit on the form.
i am trying to assure that the user has entered a number (via the JS) and that the textbox is not still set to its default of "0"
the requiredfieldvalidator works just fine, if i dont change the value it spots it on the client and fills the validation summary on the page with the appropriate message.
unfortunately when i do fill the textbox, the validator also rejests the post as the controls textbox text value is still set to "0", even though on the form it has changed.
clearly i am doing something wrong.. but cant work out what it is?
could someone enlighten me please, this is driving me potty
if i step through the code, when the get of the mediaidtext is hit,the findcontrol finds does not the textbox, if i inspect the controls collection however i can find the textbox, but its value is still "0"
it also seems that when findcontrol is called, the createchildcontrols is called again - thus resetting the value back to "0" there.. ! how can i ever assure the control realises that the textbox value has changed?
many thanks
code below
nat
[ValidationProperty("MediaIdText")]
public class MediaLibraryFileControl: CompositeControl {
private int mediaId = 0;
public int MediaId {
get { return mediaId; }
set { mediaId = value; }
}
public string MediaIdText
{
get
{
string txt = "0";
Control ctrl = this.FindControl(this.UniqueID+ "_hidden");
try
{
txt = ((TextBox)ctrl).Text;
MediaId = Convert.ToInt32(txt);
}
catch { MediaId = 0; }
return txt;
}
}
protected override void CreateChildControls()
{
this.Controls.Add(new LiteralControl("<span>"));
this.Controls.Add(new LiteralControl("<span id=\"" + TextControlName + "\">" + getMediaDetails() + "</span>"));
TextBox tb = new TextBox();
tb.Text = this.MediaId.ToString();
tb.ID = this.UniqueID + "_hidden";
tb.CssClass = "hidden";
this.Controls.Add(tb);
if (Select == true)
{
this.Controls.Add(new LiteralControl(string.Format(" [<a href=\"javascript:{0}(this,'{1}','{2}')\" class=\"select\">{3}</a>]", dialog, this.UniqueID, this.categoryId, "select")));
this.Controls.Add(new LiteralControl(string.Format(" [<a href=\"javascript:{0}(this,'{1}')\" class=\"select\">{2}</a>]", clearDialog, this.ID, "clear")));
}
this.Controls.Add(new LiteralControl("</span>"));
}
protected virtual string getMediaDetails() {
//...
return String.Empty;
}
}
this is the code adding the validationcontrol
the editcontrol is the instance of the control above
public override Control RenderValidationControl(Control editControl)
{
Control ctrl = new PlaceHolder();
RequiredFieldValidator req = new RequiredFieldValidator();
req.ID = editControl.ClientID + "_validator";
req.ControlToValidate = editControl.ID;
req.Display = ValidatorDisplay.None;
req.InitialValue = "0";
req.EnableClientScript = false;
req.ErrorMessage = "control cannot be blank";
ctrl.Controls.Add(req);
return ctrl;
}