Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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();";
        }
    }
}
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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