Thursday, May 5, 2011

asp.net custom grid vs GridView/ListView

A few years ago, I decided to create my own DataGrid as I didn’t like the standard one provided by Microsoft. It’s a very simple function which takes a DataTable as an input parameter and which returns a string (the html code to display a table on a webpage).

It’s very flexible (there are some optional parameters to do the paging, sorting and to format each column the way I want) and fast (only the records which are used are retrieved from the database). The function itself is very short (about 20ish lines of code). I’ve been using it for at least 4 years now.

Assuming you have a PlaceHolder on your webpage, this is how you would call the custom function:

MyPlaceHolder.Controls.Add(new LiteralControl(CreateCustomGrid(MyDataTable)))

CreateCustomGrid(MyDataTable))) would return something like this (if MyDataTable has 2 columns and 2 rows):

<table class="MyClass"  rules="all">
  <tr>
      <th>Column1</th>
      <th>Column2</th>
  </tr>
   <tr>
      <td align="center">Value1</td>
      <td align="center">Value2</td>
  </tr>
    <tr>
      <td align="center">Value3</td>
      <td align="center"><a href=’MyLink’>Value4</a></td>
  </tr>
  </table>

Internally the function knows how to format each column (this function is only being used on one website) but it’s also possible to change it for each individual column by using optional parameters. Same for the paging and sorting. All in all it’s very flexible and very easy to use.

Now things have changed and the DataGrid has been replaced by the GridView and/or ListView. I’ve looked at them but I don’t see anything that they do that my function doesn’t so I would be tempted to carry on using my function but I might be overlooking something. At the same time, it looks a bit odd to carry on using a custom function to generate an html table. What’s your views on this?

From stackoverflow
  • I'd say that you should investigate the GridView/ListView to see what they can do but, ultimately, if you and your customers are happy with your own code and it works for you, does everything you need it to then there's no need to change just because there is something else out there.

  • If your code works and is seasoned, I wouldn't change any existing code. For additional functionality, you might want to consider wrapping it in a custom data-bound WebControl. That way you can use datasources, etc.

    Adam McKee : +1 My thoughts exactly. Well stated!

MySql Error 150 - Foreign keys

When I execute the follow two queries (I have stripped them down to absolutely necessary):

mysql> CREATE TABLE foo(id INT PRIMARY KEY);
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE bar ( id INT, ref INT, FOREIGN KEY (ref) REFERENCES foo(id)) ENGINE InnoDB;

I get the following error: ERROR 1005 (HY000): Can't create table './test/bar.frm' (errno: 150)

Where the **** is my error? I haven't found him while staring at this for half an hour.

From stackoverflow
  • From FOREIGN KEY Constraints

    If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.

    My suspicion is that it's because you didn't create foo as InnoDB, as everything else looks OK.

    Edit: from the same page -

    Both tables must be InnoDB tables and they must not be TEMPORARY tables.

    Rob : A bit of quick testing, creating a `foo` as above with MyISAM, and a `bar` with InnoDB, suggests that that's the case - try double-checking your table type for `foo`.

CSS Nested not working the way i want it to!

Well lets say i have this

<table class="infoBox">
    <tr>
        <td>
            test
        </td>
        <td>
            <table>
                <tr>
                    <td>
                        this should not be bold!
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

And then we got some css for this

Table.infoBox tr td { font-weight: bold; }

now my problem is that the nested table seems to get the css also when i only want the outer table to get this css how do i define it in a way so that nested tables are not affected by the css?

From stackoverflow
  • While you could do this with immediate child, this isn't supported by all browsers. Try:

    table.infoBox tr td { font-weight: bold; }
    
    table.infoBox table tr td { font-weight: normal; }
    

    All that the selector table.infoBox tr td says is:

    Apply this style to any <td> tag which is a child of any <tr> tag, which is a child of any table with a class of infoBox.

    Therefore, you need to get more specific below this style block in order to declare what you want to do for tables within tables of class infoBox.

  • Either use the child selector:

    table.infoBox > tbody > tr > td { font-weight: bold; }
    

    (not supported in IE6)

    Note: the mysterious tbody element. This is inserted by the browser if you don't put it in. That's because tables have three sections: thead, tbody and tfoot.In the absence of one of those, rows are added to the tbody element.

    or a combination:

    table.infoBox tr td { font-weight: bold; }
    table.infoBox tr td td { font-weight: normal; }
    

    There are many potential variations of this one.

  • Table.infoBox table tr td { font-weight: normal; }

  • It may also suffice to use:

    table.infoBox { font-weight: bold; }
    table.infoBox table { font-weight: normal; }
    

    or

    table.infoBox tr { font-weight: bold; }
    table.infoBox table tr { font-weight: normal; }
    

    Though all css depends on other font-weight rules throughout your css, it's handy to note that css declerations are based on decendensy, not direct parent child relation ship. So table{} will have just the same effect on the content as td{} so long as you don't have anything stronger or closer to the content defined.

Loading 32bit DLL using Excel 2007 (Vista 64bit version)

I discovered that I can NOT load any 32-bit DLLs using my version of Office 2007. I’m using Vista 64bit and I assume that Office is also in 64-bit mode. Is it possible to start Excel in 32-bit mode Or Is there a way to declare a DLL using VB to load a 32-bit DLL?

Since I don’t have the source code to some of the DLLs I am using, I can’t simply re-compile stuff for 64-bit mode. So I think my options are to force Excel to run in 32-bit mode or declare the DLL I’m using as a 32-bit DLL even though my environment is 64-bit.

The error I’m getting is “DLL not found” even though the DLL is in fact there.

Below is a sample declaration. Is there a 32-bit attribute for this statement?

Private Declare Function ESQMain Lib "ESQuotes2.dll" Alias "_ESQMain@12" _ (ByVal func As Long, ByVal ticker_symbol As String, ByVal Result As String) As Integer

From stackoverflow
  • You can't "declare" a DLL to be 32 bits. It's either a Win32 binary or a Win64 binary. The first kind of binary can be loaded in Win32 processes, the second in Win64 processes. You can't mix them. Attributes on the Visual Basic import do not help.

    So, your only option is to start the 32 bits Excel binary. It has to exist, obviously, since Office is still available for Vista 32, but the question is whether it's installed and where.

    JonnyBoats : There is no 64 bit Excel, he is running 32 bit Excel.
    Jerry : You are correct. My mistake. There is no 32bit version of Excel so my question is wrong. I read articles talking about 32bit vs. 64 bit DLL problems and it sounded like this was my issue.
  • You have a bad assumption - Office is not currently available in 64 bit. The next version of office (Office 2010) is rumored to be shipped with 32 & 64 bit.

What's the easiest way to grab a web page in C ?

I'm working on an old school linux variant (QNX to be exact) and need a way to grab a web page (no cookies or login, the target URL is just a text file) using nothing but sockets and arrays.

Anyone got a snippet for this?

note: I don't control the server and I've got very little to work with besides what is already on the box (adding in additional libraries is not really "easy" given the contraints -- although I do love libcurl)

From stackoverflow
  • I do have some code, but it also supports (Open)SSL so it's a bit long to post here.

    In essence:

    • parse the URL (split out URL scheme, host name, port number, scheme specific part

    • create the socket:

      s = socket(PF_INET, SOCK_STREAM, proto);

    • populate a sockaddr_in structure with the remote IP and port

    • connect the socket to the far end:

      err = connect(s, &addr, sizeof(addr));

    • make the request string:

      n = snprinf(headers, "GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n", ...);

    • send the request string:

      write(s, headers, n);

    • read the data:

      while (n = read(s, buffer, bufsize) > 0) { ... }

    • close the socket:

      close(s);

    nb: pseudo-code above would collect both response headers and data. The split between the two is the first blank line.

    Rob : You appear to be missing the request, i.e. sending the GET /blah.htm.
    Alnitak : I was adding it as you posted your comment
    Andrioid : Nice detailed answer, +1 (maybe put the SSL code on pastebin?)
    eviljack : PLEASE post your snippet that also supports SSL
    Alnitak : I'll see what I can do - it's somewhat old and probably needs some more comments adding if it's going to be shown publicly :)
  • I'd look at libcurl if you want SSL support for or anything fancy.

    However if you just want to get a simple webpage from a port 80, then just open a tcp socket, send "GET /index.html HTTP/1.0\n\r\n\r" and parse the output.

    Alnitak : rE: libcurl - and add mostly likely several 100 KB and goodness knows how many library dependencies to your program's executable...

How can I change SOAP element names to reserved words in Delphi?

I use this line to register a class with the Delphi registry for Soap elements:

RemClassRegistry.RegisterXSClass(ToHeader, ADD_URI);

In the Soap request message, I see this:

 <NS1:ToHeader> ... </NS1:ToHeader>

Is it possible to change the class registration so that it renders the element with a different name, like:

 <NS1:To> ... </NS1:To>

or is the only way to tweak the request stream?

To is a reserved word in Delphi, I can not rename the class to the element name 'To'.

From stackoverflow
  • Looks like that's what the third parameter is for. Try this:

    RemClassRegistry.RegisterXSClass(ToHeader, ADD_URI, 'To');
    

    As of Delphi 8, you can use reserved words for identifiers. Use & as an escape character, or use a fully qualified name:

    type
      &To = class;
    
    RemClassRegistry.RegisterXSClass(UnitName.To, Add_URI);
    
    mjustin : The & does help. I am not sure about the third parameter meaning, but have tried it already without visible effect. Many thanks for your input!

iPhone Can't deselect a UITableViewCell

I have a RootViewController class which is inherited from UITableViewController. When a cell is deselected by the user I want to enable/disable certain buttons on the toolbar. How do I trap the deselect event?

-(void)tableView:(UITableView *)tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated { if(indexPath.row <= rowNumber) { [viewButtton setEnabled:NO]; [editButtton setEnabled:NO]; } }

I tried using this method but it doesn't seem to execute at all. Any ideas how cam this be done?

From stackoverflow
  • I do not think there is a deselectRowAtIndexPath event, there is a method that you can call to deselect the indexPath, but looking at the SDK I do not see an event for this in the UITableViewDelegate: http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html.

    Could you enable/disable certain buttons on the toolbar during the didSelectRowAtIndexPath: event?

    -Rog

  • This is in the current beta SDK only which means it could be buggy / change / unsupported...

    I did noticed that your method declaration doesn't match the SDK (at least, the version I have).

    Try removing 'animated:(BOOL)animated'; I don't think it's applicable here.

    See line ~345 in UITableView.h, and/or right click on didDeselectRowAtIndexPath and "Jump to Definition", where you'll probably find how the delegate method should be defined.

    That said, if your goal is simply to "enable/disable certain buttons when a cell is selected",

    • (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

    should work just fine. This will occur after they select the cell and before it's deselected. 'deselect' has to do more with animation than user interaction. The only reason I can think you would want to use deselect is maybe the aesthetic value of ensuring your event only occurs after the select cell is no no longer highlighted.