0

I'm writing a for loop that displays a list of links with some chartfx display. The chartfx needs an sqlDataSource. I'm trying to give the unique ID each time the for loop does one iteration but I can not pass it a value or function. Example below in my code. getSQLID() is just a function that returns a string which I want to be my ID. This is all done on the aspx page and the function is in the .cs . Any help would be really appreciated thank you.

     //name of the contentplace holder on the aspx page
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server" >

    //code behind
    Control ctrl = LoadControl("WebUserControl.ascx");
    Control placeHolderControl = this.FindControl("Content2");
    Control placeHolderControl2 = this.FindControl("ContentPlaceHolder1");

    ctrl.ID = "something";
    if (placeHolderControl != null)
        placeHolderControl.Controls.Add(ctrl);
    if (placeHolderControl2 != null)
        placeHolderControl2.Controls.Add(ctrl);
ZUKINI
  • 195
  • 2
  • 15
  • Hmmm. It doesn't seem like this is the best way to approach the problem. Do you need a separate SqlDataSource for each chartfx control? – Josh Darnell Mar 26 '13 at 15:18
  • yes I do, each one displays their own results based on the sqlDataSource – ZUKINI Mar 26 '13 at 15:35
  • Where is the `for` loop? – bmm6o Mar 26 '13 at 23:52
  • its just a simple for loop right now the size will be dynamic later on i just need unique ID's for my sqlDataSource by I dont know any way to make a unique sqlDatasourceID after each iteration of the foor loop. – ZUKINI Mar 27 '13 at 15:30

1 Answers1

1

First of all, recall that server controls declared in the designer like this are attached to your class at compile time. So it doesn't make sense to try to create multiple instances in a loop at runtime, and that's why the values in e.g. the Id tag have to be known at compile time.

One alternative is to create them in the code behind, with something like:

for (int i=0; i<2; ++i)
{
    var chart = new Chart();
    chart.Id = "chartId" + i;
    chart.DataSourceId = "srcid" + i;

    var src = new SqlDataSource();
    src.Id = "srcid" + i;

    Controls.Add(chart); // either add to the collection or add as a child of a placeholder
    Controls.Add(src);
}

In your case converting all of those declarative properties to the code behind can be a bit of work (though it is possible). An alternative is to make a user control (ascx) that contains the markup that's now in your aspx page. You would instantiate the controls in your code behind with something like:

for (int i=0; i<2; ++i)
{
    var ctrl = LoadControl("~/path/to/Control.ascx");
    ctrl.Id = "something_" + i;
    Controls.Add(ctrl); // again, either here or as a child of another control
    // make the src, hook them up
}
bmm6o
  • 6,187
  • 3
  • 28
  • 55
  • I get this error '<%=GetSqlId(i)%>' is not a valid identifier. ' when i do the same thing for the chartfx it seems to work DataSourceID="<%=GetSqlId(i)%>" – ZUKINI Mar 27 '13 at 16:16
  • apparently either of them dont like having functions give them ID's – ZUKINI Mar 27 '13 at 16:23
  • Would you mind showing me some of the aspx side of things because i have all this code asscociated with the chartfx i'm not 100% sure how the code will be connected to the chart ill post more of my code up above. Should I be trying do that code aswell in the code behind? – ZUKINI Mar 27 '13 at 17:50
  • I created a simple ascx page to display hello world. the problem is that it only displays at the bottom of the page I tryed adding it to an existing contentplaceholder but have had no luck ill post my code above. – ZUKINI Apr 03 '13 at 16:20