creating on-the-fly asp:table in the code file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Murtix Van Basten

    creating on-the-fly asp:table in the code file

    Hi,

    I dont know it is doable, but I wanna ask it anyway.

    I am pulling 5 different data from a datatable row by row. So each row
    has 5 fields and row count is variable not a constant. So, each row's fields
    will be shown in a table in a different design. That means, I cannot use
    DataGrid, because the view should be different. Here is the html:table
    structure fo each datarow:

    <table>
    for (int i=0;i<DataSet.T ables["mytable"].Rows.Count;i++ )
    {
    DataRow row = DataSet.Tables["myTable"].Rows[i];
    // from here the html table's rows are being entered
    <tr>
    <td width=18%><img src=row[4].ToString()></td>
    <td colspan=2>row[2].ToString()</td>
    </tr>
    <tr>
    <td rowspan=2>&nbsp ;</td>
    <td rowspan=2 width=54%>row[5].ToString()</td>
    <td width=28%>row[3].ToString()</td>
    </tr>
    <tr>
    <td>row[1].ToString</td>
    </tr>
    <tr>
    <td colspan=3><a href=details.as px?dno=row[0].ToString()>Cli ck Here
    For More Info</a></td>
    </tr>
    }
    </table>

    As you have seen here, the table row count cannot be known, so I will
    have to create tablerows as much as the datarow goes.

    I have tried to do it with Rersponse.Write , but it is writing all of
    these on top of the page, and it is ruining the design. I have tried to use
    the <asp:Table> object, but it needs the <asp:TableRow > and <asp:TableCell> s
    to be declared at design time. I wanted to do it inline of the aspx page, it
    has given alot of runtime errors regarding to the compilation. (Because I am
    using VS.NET and I am writing the code behind, not in the aspx files.)

    So, how can I create these table rows on-the-fly ? Or I am kind of hoping
    that, there must be another command, that does similar thing Response.Write
    command does, but it puts the code whereever I want to be put.

    Thanks and Regards.

    Murtix Van Basten




  • PJ

    #2
    Re: creating on-the-fly asp:table in the code file

    Create your table w/ the LiteralControl. The LiteralControl takes as it's
    constructor a string argument...whic h is the html. Build that string with a
    StringBuilder object.
    Put a PlaceHolder control on the aspx page and add a the new LiteralControl
    to the PlaceHolder control. Something like this:

    private void MakeTable(DataT able dt)
    {
    StringBuilder mytable = new StringBuilder(" <table width=/"100%/">");
    for each ( DataRow dr in dt )
    {
    mytable.Append( GetRow(dr));
    }
    mytable.Append( "</table>");
    myPlaceHolder.C ontrols.Add(new LiteralControl( myTable.ToStrin g()));
    }

    private string GetRow(DataRow dr)
    {
    //do the processing on the row
    }

    // ~PJ

    "Murtix Van Basten" <rdagdelenj31@c omNO-SPAMcast.net> wrote in message
    news:V6KcnVurUs Q2p2SjXTWJhg@co mcast.com...[color=blue]
    > Hi,
    >
    > I dont know it is doable, but I wanna ask it anyway.
    >
    > I am pulling 5 different data from a datatable row by row. So each row
    > has 5 fields and row count is variable not a constant. So, each row's[/color]
    fields[color=blue]
    > will be shown in a table in a different design. That means, I cannot use
    > DataGrid, because the view should be different. Here is the html:table
    > structure fo each datarow:
    >
    > <table>
    > for (int i=0;i<DataSet.T ables["mytable"].Rows.Count;i++ )
    > {
    > DataRow row = DataSet.Tables["myTable"].Rows[i];
    > // from here the html table's rows are being entered
    > <tr>
    > <td width=18%><img src=row[4].ToString()></td>
    > <td colspan=2>row[2].ToString()</td>
    > </tr>
    > <tr>
    > <td rowspan=2>&nbsp ;</td>
    > <td rowspan=2 width=54%>row[5].ToString()</td>
    > <td width=28%>row[3].ToString()</td>
    > </tr>
    > <tr>
    > <td>row[1].ToString</td>
    > </tr>
    > <tr>
    > <td colspan=3><a href=details.as px?dno=row[0].ToString()>Cli ck[/color]
    Here[color=blue]
    > For More Info</a></td>
    > </tr>
    > }
    > </table>
    >
    > As you have seen here, the table row count cannot be known, so I will
    > have to create tablerows as much as the datarow goes.
    >
    > I have tried to do it with Rersponse.Write , but it is writing all of
    > these on top of the page, and it is ruining the design. I have tried to[/color]
    use[color=blue]
    > the <asp:Table> object, but it needs the <asp:TableRow > and[/color]
    <asp:TableCell> s[color=blue]
    > to be declared at design time. I wanted to do it inline of the aspx page,[/color]
    it[color=blue]
    > has given alot of runtime errors regarding to the compilation. (Because I[/color]
    am[color=blue]
    > using VS.NET and I am writing the code behind, not in the aspx files.)
    >
    > So, how can I create these table rows on-the-fly ? Or I am kind of hoping
    > that, there must be another command, that does similar thing[/color]
    Response.Write[color=blue]
    > command does, but it puts the code whereever I want to be put.
    >
    > Thanks and Regards.
    >
    > Murtix Van Basten
    >
    >
    >
    >[/color]


    Comment

    • Murtix Van Basten

      #3
      Re: creating on-the-fly asp:table in the code file

      PJ,

      Thank you very much. It is exactly what I was looking for.

      Murtix.


      "PJ" <pjwalNOSPAM@ho tmail.com> wrote in message
      news:#$90YauODH A.2476@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Create your table w/ the LiteralControl. The LiteralControl takes as it's
      > constructor a string argument...whic h is the html. Build that string with[/color]
      a[color=blue]
      > StringBuilder object.
      > Put a PlaceHolder control on the aspx page and add a the new[/color]
      LiteralControl[color=blue]
      > to the PlaceHolder control. Something like this:
      >
      > private void MakeTable(DataT able dt)
      > {
      > StringBuilder mytable = new StringBuilder(" <table width=/"100%/">");
      > for each ( DataRow dr in dt )
      > {
      > mytable.Append( GetRow(dr));
      > }
      > mytable.Append( "</table>");
      > myPlaceHolder.C ontrols.Add(new LiteralControl( myTable.ToStrin g()));
      > }
      >
      > private string GetRow(DataRow dr)
      > {
      > //do the processing on the row
      > }
      >
      > // ~PJ
      >
      > "Murtix Van Basten" <rdagdelenj31@c omNO-SPAMcast.net> wrote in message
      > news:V6KcnVurUs Q2p2SjXTWJhg@co mcast.com...[color=green]
      > > Hi,
      > >
      > > I dont know it is doable, but I wanna ask it anyway.
      > >
      > > I am pulling 5 different data from a datatable row by row. So each[/color][/color]
      row[color=blue][color=green]
      > > has 5 fields and row count is variable not a constant. So, each row's[/color]
      > fields[color=green]
      > > will be shown in a table in a different design. That means, I cannot use
      > > DataGrid, because the view should be different. Here is the html:table
      > > structure fo each datarow:
      > >
      > > <table>
      > > for (int i=0;i<DataSet.T ables["mytable"].Rows.Count;i++ )
      > > {
      > > DataRow row = DataSet.Tables["myTable"].Rows[i];
      > > // from here the html table's rows are being entered
      > > <tr>
      > > <td width=18%><img src=row[4].ToString()></td>
      > > <td colspan=2>row[2].ToString()</td>
      > > </tr>
      > > <tr>
      > > <td rowspan=2>&nbsp ;</td>
      > > <td rowspan=2 width=54%>row[5].ToString()</td>
      > > <td width=28%>row[3].ToString()</td>
      > > </tr>
      > > <tr>
      > > <td>row[1].ToString</td>
      > > </tr>
      > > <tr>
      > > <td colspan=3><a href=details.as px?dno=row[0].ToString()>Cli ck[/color]
      > Here[color=green]
      > > For More Info</a></td>
      > > </tr>
      > > }
      > > </table>
      > >
      > > As you have seen here, the table row count cannot be known, so I[/color][/color]
      will[color=blue][color=green]
      > > have to create tablerows as much as the datarow goes.
      > >
      > > I have tried to do it with Rersponse.Write , but it is writing all of
      > > these on top of the page, and it is ruining the design. I have tried to[/color]
      > use[color=green]
      > > the <asp:Table> object, but it needs the <asp:TableRow > and[/color]
      > <asp:TableCell> s[color=green]
      > > to be declared at design time. I wanted to do it inline of the aspx[/color][/color]
      page,[color=blue]
      > it[color=green]
      > > has given alot of runtime errors regarding to the compilation. (Because[/color][/color]
      I[color=blue]
      > am[color=green]
      > > using VS.NET and I am writing the code behind, not in the aspx files.)
      > >
      > > So, how can I create these table rows on-the-fly ? Or I am kind of[/color][/color]
      hoping[color=blue][color=green]
      > > that, there must be another command, that does similar thing[/color]
      > Response.Write[color=green]
      > > command does, but it puts the code whereever I want to be put.
      > >
      > > Thanks and Regards.
      > >
      > > Murtix Van Basten
      > >
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...