dynamically assign tag properties
-
here is a simplified version of the page i have <% for( int i =0; i < 10; i++ ) { %> <% }%> this doesn't assign the value of i the commandargument is assigns <%=i%> i also tried CommandArgument='<%i%>' assigns <%i%> CommandArgument='<%#i%>' doesn't compile can't find i is this possible in .net ?
-
here is a simplified version of the page i have <% for( int i =0; i < 10; i++ ) { %> <% }%> this doesn't assign the value of i the commandargument is assigns <%=i%> i also tried CommandArgument='<%i%>' assigns <%i%> CommandArgument='<%#i%>' doesn't compile can't find i is this possible in .net ?
You should be using the databinding approach <%# %> - I think the reason it can't find 'i' is because the variable is out of scope at that point (databinding calls are standalone, and only know about the class. Try either: Calling a protected level property in the code-behind which returns the value instead, and use that in the for loop Or: Using a repeater control, databinding to an array/datatable/whatever with the values in, and using Databinder.Eval to get the value. -- Ian Darling If I was any more loopy, I'd be infinite.
-
You should be using the databinding approach <%# %> - I think the reason it can't find 'i' is because the variable is out of scope at that point (databinding calls are standalone, and only know about the class. Try either: Calling a protected level property in the code-behind which returns the value instead, and use that in the for loop Or: Using a repeater control, databinding to an array/datatable/whatever with the values in, and using Databinder.Eval to get the value. -- Ian Darling If I was any more loopy, I'd be infinite.
-
i figured that is what somebody was going to say. I can't really do databinding. what I have to display is to complicated and dynamic that is doesn't fit the very simply repeater pattern. thanks anyways
What I would have done is to build the list of buttons in the Page_Load method. And have a PlaceHolder tag in the HTML-part (aspx-page). The Placeholder component is god as it dosn't leav any html-tags.. Then I can do whatever complicated task in the code-behind. code-behind example:
protected Placeholder plcButtons private void Page_Load(object sender, System.EventArgs e) { Button btn; for(int i=0;i<10;i++) { btn = new Button(); btn.Text="go" btn.OnCommand="GO" btn.CommandArgument= i plcButtons.controls.add(btn); } }