Asked by:
set value of custom control textbox on button click event

Question
-
User76727224 posted
I am creating custom web control(textbox) which allows only integer data type in textbox. On button(simple asp button) submit event if there is no data in textbox, custom control should set TextBox1.Text=0, this is what i want. This is code which i wrote.
I checked few events or methods for this but they got fired after btnA_click event. I am kind of newbie in this. Any event or method i am missing? Any suggestion accepted. Thanks in Advance...
public class MasIntTextBox : TextBox { private RequiredFieldValidator req; private RegularExpressionValidator regex; public string ValGrp { get; set; } public string IsRequired { get; set; } protected override void OnInit(EventArgs e) { //this.CssClass = "inp-form"; if (IsRequired == "true" || IsRequired == "yes") { req = new RequiredFieldValidator(); req.ControlToValidate = this.ID; req.ErrorMessage = "Enter Numeric Value"; req.Display = ValidatorDisplay.Dynamic; if (!string.IsNullOrEmpty(ValGrp)) req.ValidationGroup = ValGrp; Controls.Add(req); } regex = new RegularExpressionValidator(); regex.ControlToValidate = this.ID; regex.ErrorMessage = "Numeric Value Only"; regex.Display = ValidatorDisplay.Dynamic; regex.ValidationExpression = "^\\d+(\\.\\d+)?$"; if (!string.IsNullOrEmpty(ValGrp)) regex.ValidationGroup = ValGrp; Controls.Add(regex); //base.OnInit(e); } protected override void Render(HtmlTextWriter w) { base.Render(w); if (IsRequired == "true" || IsRequired == "yes") req.RenderControl(w); regex.RenderControl(w); } }
I want to write code in this class for this requirement.
Thanks in advanced...
Friday, July 27, 2012 2:21 AM
All replies
-
User713354600 posted
use simple javascript, onclick event set you textbox value to 0 if the textbox is empty
Friday, July 27, 2012 1:46 PM -
User76727224 posted
thnx for reply but as i said, i want to write this code in custom control itself.
Saturday, July 28, 2012 1:02 AM -
User3866881 posted
thnx for reply but as i said, i want to write this code in custom control itself.
Hi,
When you want to dynamically create a web-side control, please just put it directly in the ChildControlCreate method and do something like this following:
1)You should use ViewState to contain value in order to avoid html's losing value when rendering。
2)Please put the dynamically creating controls in the CreateChildControls……
public class MasIntTextBox : WebControl { private TextBox txt = null; private RequiredFieldValidator req; private RegularExpressionValidator regex; public string ValGrp { get { return ViewState["grp"] == null ? string.Empty : ViewState["grp"].ToString(); } set { ViewState["grp"] = value; } } public string IsRequired { get { return ViewState["required"] == null ? "yes" : ViewState["required"].ToString(); } set { ViewState["required"] = value; } } protected override void CreateChildControls() { txt = new TextBox(); txt.ID = "mytxt"; Controls.Add(txt); if (IsRequired == "true" || IsRequired == "yes") { req = new RequiredFieldValidator(); req.ControlToValidate = txt.ID; req.ErrorMessage = "Enter Numeric Value"; req.Display = ValidatorDisplay.Dynamic; if (!string.IsNullOrEmpty(ValGrp)) req.ValidationGroup = ValGrp; Controls.Add(req); } regex = new RegularExpressionValidator(); regex.ControlToValidate = txt.ID; regex.ErrorMessage = "Numeric Value Only"; regex.Display = ValidatorDisplay.Dynamic; regex.ValidationExpression = "^\\d+(\\.\\d+)?$"; if (!string.IsNullOrEmpty(ValGrp)) regex.ValidationGroup = ValGrp; Controls.Add(regex); } protected override void Render(HtmlTextWriter w) { txt.RenderControl(w); if (IsRequired == "true" || IsRequired == "yes") req.RenderControl(w); regex.RenderControl(w); } }
Saturday, July 28, 2012 10:21 PM -
User76727224 posted
I guess i was not clear, I want something like this. There are some textbox of custom web control (textbox) which allows only integer values to enter.. Now when i click submit, if that textbox is empty then it should set the value to 0 so i dont have to check every time that if there is value or blank and i dont have to andle error...
Saturday, August 11, 2012 9:28 AM -
User3866881 posted
I guess i was not clear, I want something like this. There are some textbox of custom web control (textbox) which allows only integer values to enter.. Now when i click submit, if that textbox is empty then it should set the value to 0 so i dont have to check every time that if there is value or blank and i dont have to andle error...
Hi,
I think you can just assign the value of "0" to the TextBox in the CreateChildControls.
Reguards!
Saturday, August 11, 2012 8:58 PM -
User76727224 posted
Hi,
thnx for reply.
adding it in childcontrol creating event, the value and properties of that textbox i can't access...why..? i am trying but somehow its small mistake...
Regards..
Monday, August 13, 2012 3:20 AM -
User3866881 posted
adding it in childcontrol creating event, the value and properties of that textbox i can't access...why..? i am trying but somehow its small mistake...What does that mean?
Monday, August 13, 2012 5:59 AM