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
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