Lions Den

The Code and Times of Hanan Schwartzberg

About Hanan | Hanan's CV | Contact Hanan

February 12, 2014

Resize an iFrame to match its contents

Filed under: JavaScript — Tags: , , , — Hanan Schwartzberg @ 9:52 am

The Issue

You need to load content into an iFrame, but you don’t know the height of the content ahead of time.

The Solution

Catch the lonload event of the iFrame and resize it to the height of the body of the content

<iframe id="ifArtworkDetails" width="650px" height="420px" scrolling="no" 
        src="someDynamicContent.html" marginheight="0" marginwidth="0" 
        frameborder="0" onload="resizeIframe(this)"></iframe>
    function resizeIframe(objIframe) {
        objIframe.style.height = 
            objIframe.contentWindow.document.body.scrollHeight + 50 + 'px';
    }

July 17, 2012

Google Custom Search Engine – Error: ‘this.zd’ is null or not an object

Filed under: Google,JavaScript — Tags: , , , — Hanan Schwartzberg @ 4:36 pm

The Issue

When using the Google Custom Search Engine control and executing a default query it throws a JavaScript error “Error: ‘this.zd’ is null or not an object”.


Error: 'this.zd' is null or not an object

The Problem Code

google.load('search', '1');
 
function OnLoad() {
  // Create a custom search control that uses a CSE restricted to code.google.com
  var customSearchControl = new google.search.CustomSearchControl('012157912978810372049:-cv6ao3zqua');
 
  // run a query
  customSearchControl.execute('ajax api');
 
  // Draw the control in content div
  customSearchControl.draw('content'); 
}
google.setOnLoadCallback(OnLoad);

The Solution

Sometimes the most obvious mistakes are the hardest to find. After several Google searches it started to seem like no one else ever had this problem. Threre was no obvious connection between this error and the Google CSE. After staring at the examples here http://code.google.com/apis/ajax/playground/#custom_search_control for a while the answer became obvious.

Before executing the default query the control must first be drawn. The last two commands in the OnLoad method in the above code need to be flipped. Here is the correct code:

google.load('search', '1');
 
function OnLoad() {
  // Create a custom search control that uses a CSE restricted to code.google.com
  var customSearchControl = new google.search.CustomSearchControl('012157912978810372049:-cv6ao3zqua');
 
  // Draw the control in content div
  customSearchControl.draw('content'); 
 
  // run a query (ONLY AFTER DRAWING THE CONTROL)
  customSearchControl.execute('ajax api');
}
google.setOnLoadCallback(OnLoad);

August 25, 2009

Show a Wait Message While Waiting for Postback to Complete

Filed under: ASP.NET,Client Side — Tags: , , , , — Hanan Schwartzberg @ 10:13 am

Scenario

Among the controls on a page is an update panel with a button that when clicked causes a partial postback that takes at least a few seconds. During this time you want to show the user a wait message that will disappear as soon as the postBack completes.

(more…)

August 12, 2009

Default a TabContainer to No Tabs Selected

Question

Under normal circumstances when a page loads with a tabContainer on it one tab must be selected. The selected tab can be specified via the ActiveTabIndex property. What if the tabContainer needs to default to an empty tab with none of the tabs selected?

Solution

Add a dummy tabPanel to the tabContainer and leave it blank. Use the pageLoad JavaScript function to hide the tab’s header

<cc1:TabContainer ID="TabContainer1" runat="server" 
    Height="200px" Width="200px" ActiveTabIndex="0">
    <cc1:TabPanel runat="server" ID="tpDummy" HeaderText="">
        <ContentTemplate>
        </ContentTemplate>
    </cc1:TabPanel>
    <cc1:TabPanel runat="server" ID="tpName" HeaderText="Name">
        <ContentTemplate>
            Name:
            <asp:TextBox runat="server" ID="txtName" />
        </ContentTemplate>
    </cc1:TabPanel>
    <cc1:TabPanel runat="server" ID="tpEmail" HeaderText="Email">
        <ContentTemplate>
            Email:
            <asp:TextBox runat="server" ID="txtEmail" />
        </ContentTemplate>
    </cc1:TabPanel>
</cc1:TabContainer>
function pageLoad(sender, e)
{
    var objTpDummy = 
        document.getElementById('<%= tpDummy.ClientID %>' + '_tab');
    objTpDummy.style.display = 'none';
}

June 7, 2009

ComboBox ClientSide onChange Event

Filed under: Ajax Control Toolkit,ASP.NET,C#,Client Side — Tags: , , , , , , — Hanan Schwartzberg @ 4:57 pm

The Issue

The comboBox has a couple of server side events to deal with field changing.

  • OnSelectedIndexChanged
  • OnTextChanged

How can we catch the comboBox changing on the client side?

The Solution

Without access to these function on the client side, we need to use the client side events of the textBox that is contained within the comboBox. As with any textBox this one also has the onChange event, but the event does not run when the text was changed and the focus moved to another field. There are a few options:

  • onKeyPress
  • onKeyUp
  • onBlur

The problem with onKeyPress is when it gets to the javascript function the value of the textBox is has not changed yet, leaving onKeyUp and onBlur. While onKeyUp will give the desired effect when the user presses a key on the keyboard, part of the point of a comboBox is the ability to use the mouse, which will not be captured by this event, leaving onBlur as the remaining option. It’s still not perfect, since the event won’t run until the focus is moved off the comboBox, but it appears to be the best option. In some cases you may want to use both onKeyUp and onBlur to get the best of both events, but be careful because when the user uses the keyboard and the moves the focus your event will run twice.

In the example below I have a comboBox named cmbExample and a javascript function that accepts the textBox calling it as a parameter, cmbExample_OnBlur(textBox). I add the onBlur event to the textBox in the Page_Load method in the code behind.

protected void Page_Load(object sender, EventArgs e)
{
    TextBox textBox = cmbExample.FindControl("TextBox") as TextBox;
 
    if (textBox != null)
    {
        textBox.Attributes.Add("onBlur", "cmbExample_OnBlur(this);");
    }
}

April 1, 2009

Validate Multiple ValidationGroups on One Page

Filed under: ASP.NET,Client Side — Tags: , , , — Hanan Schwartzberg @ 3:37 pm

Scenario

A page has multiple validation groups:

  • vgPrimary
  • vgMain

The submit button on the page is a member of vgMain, so automatically it will only run the validation on that group. A solution is needed that will run validation on multiple groups and block the postback if needed.

(more…)

January 28, 2009

ModalPopup Flicker When Exiting the Page

Issue

When the user causes a complete postback all of the panels associated with ModalPopupExtenders flicker and become visible for a moment before the page disappears as the browser loads the next page. NOTE: This is occurring while exiting the page, not when the page loads and could not be replicated on other pages.

What did not work

A quick Google search yielded this post relating to the flicker occassionaly seen when the page loads:  http://mattberseth.com/blog/2007/08/how_to_stop_the_modalpopup_fli.html

To get around this, I explicitly set the display style to ‘none’ on the popup Panel.  This has solved the problem.

I tried this two ways (not at the same time):

  • I added display:none; to my css class that each panel uses.
  • I added style=”display:none;” the to the panel’s tag (more…)
Home | Site Design | Banner Design | Code Den | Offsite Posts | Downloads | Photography | About Hanan | Hanan's CV | Contact Hanan
Copyright © 2009 Hanan Schwartzberg. All rights reserved.