Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Stupid .net script question

Options
  • 10-04-2008 10:33am
    #1
    Registered Users Posts: 6,465 ✭✭✭


    I've got a page, statistics.aspx, with, among other thing, a table of statistics that I want to create dynamically from a database.

    I've got a sub LoadStats in statistics.aspx.vb to read the database and do a series of Response.writes to output the table.

    The .vb is linked to the page with whatever Visual Studio usually uses to do this (codebehind?).

    So I would have thought that at the appropriate point in my HTML I just have something like:
    <script runat="server" type="text/vbscript">
        LoadStats
    </script>
    

    But I'm getting an error at that point (think it was type mismatch).

    (It does seem to recognise the code in the .vb though, as when I temporarily tried moving the LoadStats sub directly into the HTML it warned me it was overloading the other function. )

    I'm either doing something simple wrong, or I've misunderstood how this works.

    Any help welcome!
    Thanks


Comments

  • Registered Users Posts: 2,494 ✭✭✭kayos


    Is there a reason you can not use a server control such as a DataGrid? Your LoadStats function would then just have to create the Dataset or object that you bind to the grid and hey presto.

    I have a funny feeling your still in the classic ASP frame of mind if your talking about response.write.

    Something like what your talking about would be the <%# %> syntax. As an example see the below.
    <%@ Import Namespace="sample.biz" %>
    ...
    ...
    ...
    <asp:Label ID="lblOrgName" runat="server" Text="<%#((Person)Container.DataItem).Organisation.Name%>"></asp:Label>
    

    This is from a GridView and just used to pull data from an object hierarchy (Person.Organisation.Name) and use it as the text for the label. Some reason the GridView didnt like to try and pull out values from child objects. Be careful with this though as its late bound and problems will only appear at runtime.

    As I said in other posts I'm not a front end guru so there could be better ways of doing this.


  • Registered Users Posts: 6,465 ✭✭✭MOH


    Had been avoiding using a server control as the formatting may change depending on which data I'm pulling back and I was trying to keep all that in a separate .css, I'd have more control over it if I'm generating the HTML myself.

    Might look into the datagrid though if that's the only way to do it, thanks.

    [edit] Forgot to mention, if it makes a difference, it's all within a ContentPlaceHolder on a page with a Master Page


  • Registered Users Posts: 2,494 ✭✭✭kayos


    MOH wrote: »
    Had been avoiding using a server control as the formatting may change depending on which data I'm pulling back and I was trying to keep all that in a separate .css, I'd have more control over it if I'm generating the HTML myself.

    Might look into the datagrid though if that's the only way to do it, thanks.

    [edit] Forgot to mention, if it makes a difference, it's all within a ContentPlaceHolder on a page with a Master Page

    You could change the css class in code if thats all your worried about. Or you could do a custom server control to render out your HTML which would most likely be a better way of doing it esp if this is not limited to one page.

    On a OT rant....
    The joys of master/content pages and css..... VS2008 can not resolve the css file in the designer when looking at the content page if the css is linked from the master page. Honestly MS really should have sorted that one. I dont like seeing 50 warnings that the css class is not defined and it also means that to see the actual layout you need to run the bloody page. VS2008 is a resource hog and I'm working on a POS pc..... hit run, go make coffee and it will have started up by the time I'm back. I thought it was a HD problem as a lot of disk trashing was going on, but same happened on my system at home (striped array) untill I dropped in 2Gig ram extra, total 4Gig! Honestly MS do have to stop making desktop software that requires server amounts of ram to function with any speed.


  • Registered Users Posts: 6,465 ✭✭✭MOH


    OK, tried out the DataGrid last night, and can get my data on the page no problem *but* it's nightmare to format it. I presume I'm missing something.

    If I want to format a particular column, I have to have one routine to set up the datagrid, then have a OnItemDataBound sub and in that jump through hoops to get a particular <td> and then add attributes to it...

    Seems like I'd be a lot simpler just outputting the HTML myself, when I could easily add a class to each <td> as I write it from the database, given that I don't need any other functionality that a Datagrid might offer (is there any?).


  • Moderators, Science, Health & Environment Moderators Posts: 8,951 Mod ✭✭✭✭mewso


    For each column in the gridview you have columns and for each column you can set the ItemStyle-CssClass property which will end up as a <td class="yada" so you can then style each cell in css. If the formatting needs to change per row then you would need to override the onitemdatabound event.

    Also you mentioned above you didn't want to use controls because you wanted to have more control over the html. There is a control called a literal control which you can place on a page. Then by setting the text property of this control it will put nothing else in bar what you have set.


  • Advertisement
  • Registered Users Posts: 2,494 ✭✭✭kayos


    musician wrote: »
    Also you mentioned above you didn't want to use controls because you wanted to have more control over the html. There is a control called a literal control which you can place on a page. Then by setting the text property of this control it will put nothing else in bar what you have set.

    As sad as it sounds this popped into my head last night as a possible. If this is only gonna be used on one page it makes way more sense than extending an existing control.


  • Registered Users Posts: 6,465 ✭✭✭MOH


    musician wrote: »
    For each column in the gridview you have columns and for each column you can set the ItemStyle-CssClass property which will end up as a <td class="yada" so you can then style each cell in css. If the formatting needs to change per row then you would need to override the onitemdatabound event.

    I thought there might be something like that, but my problem is that the columns are being autogenerated on binding (I may have different numbers of columns based on the query) and my columns count was showing as 0. According to the MSDN datagrid help "Automatically generated columns are not added to the Columns collection". Of course I've only noticed that now, despite it being highlighted spending an hour staring at the page last night!
    Also you mentioned above you didn't want to use controls because you wanted to have more control over the html. There is a control called a literal control which you can place on a page. Then by setting the text property of this control it will put nothing else in bar what you have set.

    Thanks, had a look at that, but it doesn't look like you can apply any styles to the content of a literal.
    At this stage I think I'll just throw in a div, generate the table as before and set it as the div's innerhtml. That should (might?) work?


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Its easy to create the datagrid in code and apply the styles per column as you create the grid. Another way is to create a class that implements the ITemplate interface and work it that way.


  • Moderators, Science, Health & Environment Moderators Posts: 8,951 Mod ✭✭✭✭mewso


    MOH wrote: »
    Thanks, had a look at that, but it doesn't look like you can apply any styles to the content of a literal.
    At this stage I think I'll just throw in a div, generate the table as before and set it as the div's innerhtml. That should (might?) work?

    There are a number of controls that might suit. The asp:label renders as a span and can be styled. The asp:panel renders as a div but you add content to it by creating controls. Yes a normal div would work. You could even have a <div id="mydiv" runat="server" and then add content to it's innerhtml which is available as a property in .net. Even just adding text to a literal could be styled if part of the text you add included "<div class="blah">......"


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    The problem you're having is because of the ASP.Net page lifecycle, which dictates where and when the HTML gets rendered. It's worth googling to have a bit of a read about it. But in short you don't want to write HTML to the output directly yourself (normally). Your best bet is to use a server control of some type which you write code to set properties for in an ASP.Net event handler, such as on page load or button click etc. The datagrid has been mentioned . The literal control has also been mentioned and that will let you write your HTML directly.

    Two other options to look at: The gridview control, the ASP.Net 2 replacement for the datagrid, much more flexible and powerfull. Also there's a "Table" server control you can use and control progmatically, for e.g.
    protected void Page_Load(object sender, EventArgs e)
        {
            if(!this.IsPostBack()){
                TableRow tr1 = new TableRow();
                TableCell tc1 = new TableCell();
    
                c1.CssClass = "cell1";
                tr1.CssClass = "row1";
    
                tc1.Text = "Hello World";
    
                tr1.Cells.Add(tc1);
                this.Table1.Rows.Add(tr1);
            }
        }
    
    That will do exactly what you want right now. But really the gridview is the recommended way to do it and when you have a chance you should try to learn more about it.


  • Advertisement
  • Registered Users Posts: 6,465 ✭✭✭MOH


    Thank ye all very much. I'm still a complete noob at this, but I've learnt a bit over the last couple of days. I've gone with the div.innerhtml for now, and its working fine. I'll probably go back and look at the gridview later and maybe do it properly, but since I've wasted 2 days on what I thought would be five mins work I can't take any more frustration right now! :)


Advertisement