users@woodstock.java.net

Re: setId() breaks Ajax?

From: Dan Labrecque <Dan.Labrecque_at_Sun.COM>
Date: Tue, 05 Feb 2008 21:23:50 -0500

autozoom wrote:
> I am adding a button to the page using:
>
> public void prerender() {
> Button button = new Button();
> button.setId("ciao");
> JSFActionManager.setValueExpression(button, "text", "#{Page1.txt}");
> button.setStyle("left: 210px; top: 3px; position: absolute");
> form1.getChildren().add(button);
> }
> ...
> public String getTxt() {
> return (new Date()).toString();
> }
>
> public void setTxt(String t) {
> }
>
> this renders the button but calling
> $("form1:ciao").refresh() via Javascript doesn't refresh the button as it
> should.
>

I'm not familiar with the "$("form1:ciao").refresh()" syntax. Don't you
use document.getElementById to obtain the HTML element, first? For example:

var domNode = document.getElementById("form1:ciao");
domNode.refresh();

> But if I remove the button.setId("ciao"), everything works fine.
>

What to you use for a component id?

> Does this mean that I cannot use my own ids when dynamically adding
> components to the page?
>

No, you should set the id for every component.

Most likely, JSF Extensions (i.e., DynaFaces) could not find the given
id in the JSF component tree. It could be that you have not provided the
correct, fully qualified component id. Or, the button has not been added
to the JSF component tree when the Ajax request is made. If I recall,
you were using request scope beans, correct?

Dan