Set default button in Asp.Net pages and in asp:login form

Saturday, 3 October 2009 15:32 by myro

Setting the default botton that needs to be pushed when the Enter key is pressed into an ASPX page, can be implemented easly in Asp.Net.
Imagine that your page holds 2 controls:

  • a Asp.net TextBox named tb1
  • a Asp.net Button named btn1

To instruct the page, to point the default button  on btn1, you should use:

Page.Form.DefaultButton = btn1.ClientID;

Setting the focus on tb1 can be accomplished by:

Page.Form.DefaultFocus = tb1.ClientID;

As you can see, you have to use control's ClientID and not  the control's ID.

But what happens if you want to set the Page's DefaultButton property to a login button contained in a Login form control? Consider surrounding the <asp:Login /> control with a simple asp.net Panel:

<asp:Panel ID="Panel1" runat="server" Height="100%" Width="100%" DefaultButton="llogin$LoginButton">
    <asp:Login ID="llogin" runat="server"
         PasswordRecoveryUrl="~/passwordrecover.aspx"
         CreateUserUrl="~/register.aspx"
         TitleTextStyle-CssClass="contentGroupHeader"
         OnLoggedIn="SetCookie"  >
    <LoginButtonStyle CssClass="button" ></LoginButtonStyle>
    </asp:Login>
</asp:Panel>

...and  set the DefaultButton property using the syntax provided in the example.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:  
Categories:   Web
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed

Add code into server control's propery markup in Asp.Net: use CodeExpressionBuilder

Sunday, 30 August 2009 16:45 by myro

If you need to add codes through Expression Bulders, you' ll notice that are different ways to do that. Depending on what you need to write into your html rendered page, you' ll need to use different annotations. Let's see:

Write a string Resource

<asp:Button id="Btn1" runat="server" Text="<%$ appSettings: ButtonText %>" />

Call a DataBind

<%#Eval( property )%>

Write a string through Response.Write

<%= DateTime.Now %>

The problem comes out when you try to use the Response.Write method to manipulate an asp.net server control's propery. The following annotations won't work:

 <asp:Label id="lbl1" runat="server" Text="<%= DateTime.Now %>"/>

The best solution to this problem is ito implement CodeExpressionBuilder posted in InfinitiesLoop's blog. Here's a quick howto:

1- Create a class in your Web Application Project, and paste:

 [ExpressionPrefix("Code")]
public class CodeExpressionBuilder : ExpressionBuilder {
    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,
       object parsedData, ExpressionBuilderContext context) {
        return new CodeSnippetExpression(entry.Expression);
    }
}

2- Register the new Expression Builder into your Web.Config

<compilation debug="true">
  <expressionBuilders>
    <add expressionPrefix="Code" type="YOURNAMESPACE.CodeExpressionBuilder"/>
  </expressionBuilders>
</compilation>

And now you can call your code in server control's properties using the following syntax:

 <asp:Label id="lbl1" runat="server" Text="<%$ Code: DateTime.Now %>"/>

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   .NET
Actions:   Bookmark and Share | Permalink | Comments (2) | Comment RSSRSS comment feed

Implement a Message Box to confirm an Asp.Net server control event

Tuesday, 9 June 2009 15:45 by myro

You just need to provide a simple Message box that asks for confirmation when a button is clickked? There are several ways to accomplish this, but if you don't need a complicated solution consider in adding a small javascript to the Asp.Net control on the OnClientClick attribute:

<asp:ImageButton ID="imbdelete"
         OnClientClick="return confirm('Are you sure you want to delete this configuration?');"
         runat="server" CausesValidation="false"
         ImageUrl="~/_layouts/Images/DELETE.GIF" OnClick="imbdelete_click" />

A message box will be prompted when users clicks this Imagebutton, which will raise the postback only if the 'yes' button is pressed. Cool..

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   , ,
Categories:   .NET | Web
Actions:   Bookmark and Share | Permalink | Comments (1) | Comment RSSRSS comment feed

Add CSS StyleSheet Link in ASP.NET page header at runtime

Friday, 24 April 2009 10:55 by myro

Is not really intuitive how can you add a reference to a css stylesheet at runtime in ASP.NET in the HTML head section: you need to call the page's ClientScript manager and register a Javascript script block defining your script code as a link tag. Remember that RegisterCssReference method will only work if you call it before the page's OnRender event method, so use it in OnLoad and OnPreRender page's events.

protected void RegisterCssReference(string CssLinkurl)
{
       if (!String.IsNullOrEmpty(CssLinkurl))
       {
           String sb = String.Empty;
           sb = String.Concat("<link rel=\"stylesheet\" href=\"", CssLinkurl,
                 "\" type=\"text/css\" media=\"screen\" />");
           Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "YOUR_KEY", sb , false);
       }
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   .NET | Web
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed

Open links into a new window based on the extensions

Thursday, 12 March 2009 16:48 by myro

Immagine a scenario where you need to handle how a browser should open different urls into your site. You need to open every link into a new browser's window except links to documents, to avoid opening a blank window.
Infact, using the attribute 'target' into an anchor: 

<a href=http://www.myrocode.com/document.doc target="_blank" >Open my document</a> 

will open first a new window, and then let you download the file.As you can see  this is not a good solution for this scenario.
Here you can find a simple Javascript that will resolve this kind of problem

 <script type="text/javascript">


function OpenIntoNewWindow(url)
{
    // this are the extensions that will be opened in the current windows
    var fileExtensions = new Array('rtf', 'log', 'txt', 'doc', 'pps', 'ppt', 'xls', 'xml',
                                        'docx', 'xsl', 'xlsx', 'ppt', 'pptx', 'mdb', 'accdb','pdf');
    var match = new Boolean(false);
    for (index in fileExtensions)
    {
        if (endsWith(url.toString().toLowerCase(), fileExtensions[index]))
        {
            // we've found a match!
            match = true;
            break;
        }
    }
    if (match == true)
    {
        window.location.href = url;
    } else window.open(url);
}


function endsWith(testString, endingString) {
    if (endingString.length > testString.length) return false;
    return testString.indexOf(endingString) == (testString.length - endingString.length);
}
</script>
<a style="cursor:pointer" onclick="OpenIntoNewWindow('http://www.myrocode.com/default.aspx');">
     my window html</a>
<br />
<a style="cursor:pointer" onclick="OpenIntoNewWindow('http://www.myrocode.com/SampleDocument.doc');">
     my window Doc</a>

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   Web
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed