2

I'm rendering a custom FormTemplate that includes a ListfieldIterator. How can I add an event like onclick to one of my controls rendered by the iterator?

Thanks in advance

3

If you want to do it in ListFieldIterator you can try the following code:

protected override void RenderChildren(System.Web.UI.HtmlTextWriter writer)
{
    foreach (BaseFieldControl fieldControl in SPContext.Current.FormContext.FieldControlCollection)
    {
        if(fieldControl.Field != null && fieldControl.Field.InternalName=="BooleanFieldName")
        {
           //attach server-side event
           ((CheckBox)fieldControl).CheckedChanged += check1_CheckChanged;
           //attach client-side event
           ((CheckBox)fieldControl).Attributes("change","jsfunc();");
        }
        if(fieldControl.Field != null && fieldControl.Field.InternalName=="DateTimeFieldName")
        {
           //attach server-side event
           ((DateTimeControl)fieldControl).DateChanged+= dateTime1_DateChanged;
           //attach client-side event
           ((DateTimeControl)fieldControl).OnValueChangeClientScript="jsfunc();";
        }
    }
}
  • Thanks Andrew! I don't know how to explain it. But if I got you right, I can add .NET events here!? How do I add javascript events like 'onclick' here? COul u please post another piece of code? – Stefan Hennicken May 6 '13 at 14:32
  • For what type of control do you want to add js event? – Andrew May 6 '13 at 14:41
  • BooleanField-> change and DateTimeField -> onblur/lostfocus – Stefan Hennicken May 6 '13 at 14:53
  • I updated my answer. Hope it will help. – Andrew May 6 '13 at 15:15
  • Seems to be exactly what I was looking for! I'll try it now. – Stefan Hennicken May 6 '13 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.