Monday, March 7, 2011

Figure out the focused control

I'm trying to add a CSS class to the Control that will get the focus, once a page is rendered. While the SetFocus() method of the Page class lets me set the Control, there is no corresponding GetFocus() method.

According to the .Net sources, the information is stored in the private member _focusedControl of the Page class. The property FocusedControl is marked internal.

Is there a way to get the value of the private member or internal property by using Reflection?

Any help would be greatly appreciated.

Clarification: Here is why I want to add a CssClass server side: I'm trying to apply the following JQuery script, that changes the background of the focused element:

$(document).ready(function() {
  var elements = jQuery("textarea, select, multi-select, :text, :password, :file");

  elements.bind
  (
    'focus',
    function() {
      jQuery(this).addClass('highlightinput');
    }
  );

  elements.bind
  (
    'blur',
    function() {
      jQuery(this).removeClass('highlightinput');
    }
  );
})

This works fine as long as I don't specifically set a focused control in my aspx.vb. If I do set a focused control (I think due to a timing issue), the focus is set before my handlers are attached to the input fields and thus, the input is not highlighted. So my approach would be to add the highlightinput class to the focused control before rendering the page.

From stackoverflow
  • The control with focus may have changed between post-backs, which is why I don't think you can find it easily. It is probably too expensive to keep the entire state of each control in ViewState.

    Maybe you can track which control has focus in a hidden field on the client using javascript, and read it on the server.

    : That is correct. However, I don't need the information to survive a postback. I just need to know the last control, that was passed to the SetFocus method.
  • If you are looking for a css solution for highlighting the focused element I believe you can use the ':focus' selector. I haven't tried this, but I believe this is a valid selector. You would use it like so in your css file:

    :focus{ background-color: yellow;}
    : Thanks Yobi21, this works fine in Firefox but it's not supported by IE
  • Why not just do it all via JavaScript? Something like:

    body.onLoad = function() { document.activeElement.style.color = '#ff0000'}

    That may be buggy but I think it's a good start.

    : Thanks Shawn, I tried it but I couldn't get it to work. I would rather know server side which control will receive the focus.
  • I don't know if i understood correctly your question...

    Couldn't you just add a cssclass to the control your setting focus to on the serverside?

    controlObj).CssClass = "highlightinput"
    Page.SetFocus(controlObj)
    
    : The problem is, that the Page.SetFocus is called by a component I have no control over. So I would like to figure out, what control will be focused, once the page is rendered so I can set the additional CssClass. Also, I would like to have a generic approach, that I could add to a base-class

Reuse/cache datatable in a web service

I have a web application that pulls a datatable and displays results. Due to the nature of the business all of the business rules are incorporated in dlls that the web app must pull from. Because there are multiple site and we don't want to include the core dll's in all of the many apps that are deployed we consolidate any calls to the core dll's into a webservice. This helps when the core dll's are updated that only one new app (i.e. the web service) needs to be pushed.

With this architecutre I have found that anyone logging into any web app is causing multiple calls to the database in order to get a datatable that is then parsed. The datatable in question changes slowly and I only need to refresh it maybe once every 4-6 hours.

I am familiar with storing things like this in session when a user wants to see different views of the same datatable. However with the webservice session doesn't really apply. What is a good method for caching a datatable in the webservice so that we are not making repeated expensive trips to the database.

The webservice and the apps are all in C# and ASP.NET 2.0, and we are using VS2005.

From stackoverflow
  • You could store any data in memory for as long as you want with memcached. It runs on Linux and Windows and they also have several api implementations with C#.

  • Cheap option #1: create the datatable as a local static variable to your web service class. Populate the datatable object in a static constructor to the class. The datatable object will be available to your webmethod when the service is queried for the first time as well as any app worker process recycles.

    //pseudo-code

    class MyWebMethods {

    private static DataTable localDataTable; static MyWebMethods() { localDataTable = new DataTable(); //Do whatever else is needed here to populate the table }

    [WebMethod] public string GetSomeData(int id) { //Query localDataTable using parameter info //return result } }

    Cheap option #2: similar approach, consider ASP.Net Cache (System.Web.Caching.Cache). Same loading process as above, but different storage.

  • As a really cheap way (but it exists), you can use the CacheDuration property of the WebMethod arribute, which will cache the results of the web method call. More info at:

    http://msdn.microsoft.com/en-us/library/byxd99hx(VS.71).aspx#vbtskusingwebmethodattributecacheduration

    That might help, just a thought...

When is the proper time to use the SQL statement "SELECT [results] FROM [tables] WHERE [conditions] FETCH FIRST [n] ROWS ONLY"

I'm not quite sure when selecting only a certain number of rows would be better than simply making a more specific select statement. I have a feeling I'm missing something pretty straight forward but I can't figure it out. I have less than 6 months experience with any SQL and it's been cursory at that so I'm sorry if this is a really simple question but I couldn't find a clear answer.

From stackoverflow
  • Custom paging, typically.

  • When you want to display the values to a user, you are only likely to need N rows. Generally the database server can fetch the first N rows, faster than it can fetch all of the rows, so your screen redraw can go a little bit faster.

    Oracle even has a hint, called FIRST_ROWS that suggests that getting data back quick, is more important that getting it all back efficiently.

  • We're using the statement for the following reasons:

    1. Show only the most relevant results (say the top 100) without having to transfer all rows from the DB to the client. In this case, we also use ORDER BY.

    2. We just want to know if there are matching rows and have a few examples. In this case, we don't order the results and again, FETCH FIRST is much more cheap than having the DB prepare to transfer lots of rows and then throw them away at the client. This is usually during software development when need to get a feeling if a certain SQL is right.

  • The designers of SQL agree with you, which is why standard SQL doesn't included top/limit/fetch first, etc.

  • Think google search results and the number of pages there are typically for results.

    Though obviously, there's much more to it in their case but that's the idea.

  • I know of two common uses:

    Paging : be sure to specify an ordering. If an ordering isn't specified, many db implementations use whatever is convenient for executing the query. This "optimal" ordering behavior could give very unpredictable results.

    SELECT top 10 CustomerName
    FROM Customer
    WHERE CustomerID > 200 --start of page
    ORDER BY CustomerID
    

    Subqueries : many places that a subquery can be issued require that the result is a single value. top 1 is just faster than max in many cases.

    --give me some customer that ordered today
    SELECT CustomerName
    FROM Customer
    WHERE CustomerID =
    (
      SELECT top 1 CustomerID
      FROM Orders
      WHERE OrderDate = @Today
    )
    
  • Aside from paging, any time you want the most or least [insert metric here] row from a table, ordering on [whatever metric] and limiting to 1 row is, IME, better than doing a subquery using MIN/MAX. Could vary by engine.

how to check if a jquery plugin is loaded

Is there any way to check if a particular plugin is available?

Imagine that you are developing a plugin that depends on another plugin being loaded.

For example i want the jQuery Validation plugin to use the dateJS library to check if a given date is valid. What would be the best way to detect, in the jQuery Valdation plugin if the dateJS was available?

From stackoverflow
  • This sort of approach should work.

    var plugin_exists = true;
    
    try {
      // some code that requires that plugin here
    } catch(err) {
      plugin_exists = false;
    }
    
  • Generally speaking, jQuery plugins are namespaces on the jQuery scope. You could run a simple check to see if the namespace exists:

     if(jQuery.pluginName) {
         //run plugin dependent code
     }
    

    dateJs however is not a jQuery plugin. It modifies/extends the javascript date object, and is not added as a jQuery namespace. You could check if the method you need exists, for example:

     if(Date.today) {
          //Use the dateJS today() method
     }
    

    But you might run into problems where the API overlaps the native Date API.

    Dennis : I had to use jQuery().pluginName not jQuery.pluginName
    Nagyman : if(jQuery.fn.pluginName) {...} is another option
  • I would strongly recommend that you bundle the DateJS library with your plugin and document the fact that you've done it. Nothing is more frustrating than having to hunt down dependencies.

    That said, for legal reasons, you may not always be able to bundle everything. It also never hurts to be cautious and check for the existence of the plugin using Eran Galperin's answer.

  • For those who came here through Google searching for "How to check if jquery plugin is loaded or not" I suggest reading this article. The idea behind it is the same as Eran's answer, but it explains it in more details. Good for jQuery newbies...

  • To detect jQuery plugins I found more accurate to use the brackets:

    if(jQuery().pluginName) {
        //run plugin dependent code
    }
    

Rounded CSS tabs

Hello,

I have created some rounded navigation tabs using CSS and am having trouble when implementing:

hover

Firstly here is a link to the site Tabs as it is quite difficult to explain. Please, hover over the left side of the tabs to understand my problem. I have explained below.

I have a background image set on #navigation li items this contains the left side corner and i then have a second background image (the right corner) this is set using #navigation a.

However when i hover over the left side of the tab, only a small piece of background is shown, i presume this is because only the li area is being hovered over. Obviously i would like the entire tab to highlight as it does when you hover over the rest of it.

This was quite hard to explain so if you need to question me further to understand the problem please do.

From stackoverflow
  • Couple of things:

    Fix the problem by taking the padding off the <li> and putting it back on the child <a> - the space they occupy needs to be the same to get the hovers to align.

    Now you have a different problem, the left corner doesn't show. Fix this by making the background colour for the a and a:hover equal to transparent instead of the colours - now the <li> can show through.

    Finally, I suggest you change the behaviour from being another image entirely, to the same image with a different background-position, so the rollover loads invisibly.

    edit: css rollover without image swap described here

    annakata : ithankyou - it's easy enough to see your way through these things if you imagine building the page as bits of physical paper, I just made one of those bits acetate :)
  • With jQuery it's super easy! but you can even try scalable box:

    http://www.usabilitypost.com/2008/11/27/scalable-content-box-using-only-one-background-image/

    and then change the background image on hover.

  • Or you can replace your li:hover and a:hover with these:

    #navigation li:hover {
         background: #009BDA url(images/tab_left_on.gif) no-repeat top left;
         cursor : pointer;
         }
    
    #navigation li:hover a {
         background: #009BDA url(images/tab_right_on.gif) no-repeat top right;
         color: #FFF;
    }
    

    Note that it may not work IE6.

    annakata : definitely won't work in IE6 (li:hover not supported) but that's not always a problem
    annakata : (not that the original solution works in IE6 either...)
    Ronnie : When you say the solutions will not work in IE6. Are you saying that the problem will simply be what i have currently, or after implementing your methods will it look horribly wrong in IE6?
    Mg : Neither your page, or my solution will work since li:hover is not supported on IE6.
    Ronnie : Ok, i understand. Thank you for your help.
    annakata : but this is impossible to fix for IE6 you'd have to go with a JS solution or a different design -the market share for that browser is so poor now I wouldn't care unless you have to
  • There is also another eye catching tab named boxy tab. Thanks.

Can dynamically pluggable modules be done in VHDL?

In c (embedded) a plugin can be implemented by defining a function pointer table and an address range that the module can be loaded into.

This requires linker directive to allocate the space and define the location of the function table.

Is there a similar mechanism in HDL / VHDL or Verilog.

I guess what I am thinking is to define a block of gates in an FPGA to be for my extension feature, define the ports it will interface to and later be able to load into this block the logic to perform a certain operation.

This would have to be at runtime as the FPGA is part of the communications system that the plugin will be delivered by.

From stackoverflow
  • If you are using Xilinx FPGA's, this can be supported in some of their chips.

    See: Benefits of Partial Reconfiguration with Xilinx

    --jeffk++

    Brian Carlton : What jeffk describes isn't a Verilog or VHDL feature, but a chip feature.
  • The feature you're looking for is orthogonal to the domain of VHDL. VHDL allows modules (entities, implemented by architectures), so in theory, yes, this can work. But in practice, it really depends on your running system.

    What do you expect to gain by this? Save reconfiguration time?

    If you have a sufficiently complex board with some software running, you can easily download a fresh configuration for the FPGA from some comm channel. You can also gain small performance benefits on Xilinx, by using partial reconfiguration.

    Or maybe you can just create a configurable VHDL module that the software will configure in runtime to behave as required.

    JeffV : Hoping to get field programability with out bricking the device. The comms channel would need part of the FPGA to remain operational through out the programming phase. If something goes wrong and it gets "bricked" the actual FPGA is impossible to reach physically.
    Eli Bendersky : There are solutions for this. For example, read about Altera's Remote Upgrade theme. There is a "loader" image that's never changed, and it loads several "application" images into memory, and can update them.

Visual Studio: How to view "Advanced" properties in the Properties window?

i need to see the ClientRectangle of a form as i'm designing it. This particular property is tagged as "Advanced", and by default is hidden from the Properties window:

[EditorBrowsable(EditorBrowsableState.Advanced), ...]
public Rectangle ClientRectangle

If the ClientRectangle property i want to look at is out, then i guess i can settle for the advanced ClientSize property:

[..., EditorBrowsable(EditorBrowsableState.Advanced)]
public Size ClientSize

The documentation for the EditorBrowsable attribute leads to an enumeration with 3 values:

Always The property or method is always browsable from within an editor.

Never The property or method is never browsable from within an editor.

Advanced The property or method is a feature that only advanced users should see. An editor can either show or hide such properties.

Apparently i am an "advanced user", so how do tell Visual Studio that i am an advanced user so i can see the advanced properties?


Update One

The linked page talks about being able to enable Intellisense and the Properties Window to show advanced memebers:

In Visual C#, you can control when advanced properties appear in IntelliSense and the Properties Window with the Hide Advanced Members setting under Tools | Options | Text Editor | C#. The corresponding EditorBrowsableState is Advanced.

Unfortunatly this option seems to, in reality, only apply to Intellisense. Having it unchecked does not make advanced properties visible in the Properties Window

Note: the option is unchecked by default.

From stackoverflow
  • The answer is in the link you provided:

    In Visual C#, you can control when advanced properties appear in IntelliSense and the Properties Window with the Hide Advanced Members setting under Tools | Options | Text Editor | C#. The corresponding EditorBrowsableState is Advanced.

    Ian Boyd : i have it unchecked and the items do not appear in the Properties Window.