PDA

View Full Version : Validating Dynamically Created Controls


Gregoriusness
07-11-2008, 08:09 AM
Hey all,

Any ideas how i can achieve this. Basically, i create a bunch of checkboxes by databinding to an xml file, and then override the ItemDataBound function like so:

protected void dgRoles_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

e.Item.Cells[0].Visible =Convert.ToBoolean(DataBinder.Eval(e.Item.DataItem, "displayflag"));
System.Web.UI.WebControls.CheckBox cb = (System.Web.UI.WebControls.CheckBox)e.Item.FindControl("chkrole");
cb.Checked =Convert.ToBoolean(DataBinder.Eval(e.Item.DataItem, "default"));
cb.Enabled =Convert.ToBoolean(DataBinder.Eval(e.Item.DataItem, "editable"));
e.Item.Cells[1].Visible =Convert.ToBoolean(DataBinder.Eval(e.Item.DataItem, "displayflag"));
if ((defaulttotalcare)&&(e.Item.Cells[2].Text ==ConfigurationSettings.AppSettings["Some Role"]))
cb.Checked=true;
if (DataBinder.Eval(e.Item.DataItem, "requiresConfirmation") != System.DBNull.Value
&& Convert.ToBoolean(DataBinder.Eval(e.Item.DataItem, "requiresConfirmation")))
{ // Add confirmation checkbox

chkTerms.Visible = true;
chkTerms.Text = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "confirmationMessage"));

// Add javascript call to attribute
cb.Attributes.Add("onClick", "ToggleTermsCheckbox();");

// Inject javascript code to hide/show terms checkbox. NOTE: This will only work for 1 checkbox
// if you need to add more, you'll need to make this more generic
string js = "<script language='javascript'>\n";
js += String.Format("var chkTCRole = '{0}';\n var chkTerms = '{1}';\n", cb.ClientID.ToString(), chkTerms.ClientID.ToString());
js += "function ToggleTermsCheckbox() {\n";
js += "if (document.getElementById(chkTCRole).checked == 1) { document.getElementById('termsRow').className='visible'; }\n";
js += "else { document.getElementById('termsRow').className = 'invisible'; } }\n";
js += "ToggleTermsCheckbox();\n";
js += "</script>\n";


// Add javascript scriptBlock to hide/show the Terms checkbox
if (! this.Parent.Page.IsStartupScriptRegistered("ToggleTermsCheckbox"))
{
this.Parent.Page.RegisterStartupScript("ToggleTermsCheckbox", js);
}
}
}
}

So basically it checks the XML, and if it has a "requiresConfirmation" value, it adds the Terms checkbox. Currently the Terms checkbox is just hardcoded outside of this databinding function, so i can refer to it directly.

I have also injected some javascript code, which sets an action on the current checkbox saying to display the Terms checkbox when the current checkbox is ticked.

The problem is after the binding is done, I've got client side validation working just using the clientID of the checkbox. But server side validation is another story - at the point when i'm trying to set up the server-side validation, the checkbox has not yet been created. So how can I reference this checkbox in order to validate it?

Thanks all,
Greg

MEMOS Software
07-17-2008, 07:29 PM
Do you use some of the asp.net validators or you want to validate it by your own?

Gregoriusness
07-18-2008, 01:22 AM
hey... I've actually solved this problem. All i had to do was move the creation of the dynamic controls into the InitializeComponent() function (instead of Page_Load()), which makes them available earlier in the page lifecycle, and therefore able to setup validation on them.

I'm rolling my own validation as .Net 1.1 client-side validation only works on IE browsers. I'm using jQuery to validate the frontend, and then my own custom backend validators. If I were going to be working much more on .Net 1.1, i'd create a framework so the validators could share business rules etc, but at this point its just not worth it.