Wednesday, November 24, 2010

ScriptManager.RegisterStartupScript() vs. ClientScript.RegisterStartupScript()

Hi!

I have been struggling in manipulating scripts to focus to a particular control when a user executes a button or events triggered by postbacks.

The scenario was in every button clicked, the focus will be transferred to another control. These butttons are inside an update panel and ajax tab container.

The solution presented in an article i've previously posted was using ClientScript.RegisterStartupScript() to register a script. However, this script is registered in the Page object. But since my controls are inside an update panel which is partial postback rendering, the solution I've come up was using ScriptManager.RegisterStartupScript().

So, to register client scripts on an aspx page w/o ajax functionalities, use ClientScript:
1
ClientScript.RegisterStartupScript(this.GetType(), "MyScript", script.ToString(), false);  

And to register client scripts on an aspx page w/ ajax functionalities such as inside an update panel, use ScriptManager:
1
ScriptManager.RegisterStartupScript(this, this.GetType(), "MyScript", script.ToString(), false);  

Thursday, November 18, 2010

Assign Hexa Color to .NET control background color property

To convert hexa decimal color to Color in .NET and assign it to a control's background property, do the following:
1:  this.lblMachinery.BackColor = ColorTranslator.FromHtml("#CCCOOO");  


Wednesday, November 17, 2010

Register Client Scripts in ASP.NET using ClientScriptManager class

Source: ClientScriptManager Class
C# Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public void Page_Load(Object sender, EventArgs e) {
  // Define the name and type of the client scripts on the page.  
  String csname1 = "PopupScript";
  String csname2 = "ButtonClickScript";
  // Get a ClientScriptManager reference from the Page class.  
  System.Web.UI.ClientScriptManager cs = Page.ClientScript;
  // Check to see if the startup script is already registered.  
  if (!cs.IsStartupScriptRegistered(csname1)) {
   String cstext1 = "alert('Testing');";
   cs.RegisterStartupScript(GetType(), csname1, cstext1, true);
  }
  // Check to see if the client script is already registered.  
  if (!cs.IsClientScriptBlockRegistered(csname2)) {
   StringBuilder cstext2 = new StringBuilder();
   cstext2.Append("");
   cs.RegisterClientScriptBlock(GetType(), csname2, cstext2.ToString(), false);
  }
 }

HTML Markup
1
2
3
4
5
6
<body>  
   <form id="Form1" runat="server">  
   <input type="text" id="Message"/>   
   <input type="button" value="ClickMe" onclick="DoClick()" />  
    </form>  
</body>  

Tuesday, November 16, 2010

(AJAX Tab Container Error)htmlfile: Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.

In my aspx page, I have an ajax tab container control, and inside each tab strip,i have server controls.

During pageload,i want to set focus on a particular control but got this error during page load:
htmlfile: Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.

the solution is in this page:
Setting focus to a textbox when in a tab panel errors

Saturday, November 13, 2010

JavaScript Format Decimal Precision

Here's a resource where i came to change decimal precision on of numbers using client script. It explained on how to use the toFixed() and toPrecision(). However, there are bugs on this. So it's much better to set precision of numbers using server side script.

http://www.mredkj.com/javascript/nfbasic2.html

Monday, November 8, 2010

Gridview samples from MSDN

Here's a link of samples from MSDN. I have'nt tested all of them, but this might help to other developers out there.
GridView Examples for ASP.NET 2.0

Thursday, November 4, 2010

Paste HTML codes in your post

You might be wondering how the heck could i paste html code in my post. Well, there's a handy tool for that in this site:

http://francois.schnell.free.fr/tools/BloggerPaste/BloggerPaste.html

Scalar and Inline Table Functions

After reading some articles on TSQL, I made some experiments on User defined functions. Actually, there are three types of UDF. First we have the Scalar functions that returns a single value of the data type referenced in the RETURNS clause of the CREATE FUNCTION statement. The returned data can be of any type except text, ntext, image, cursor, or timestamp. Next we have Inline table-valued function which returns a variable of data type table whose value is derived from a single SELECT statement. Lastly, we have multi-statement table-valued function which is slightly more complicated than the other two types of functions because it uses multiple statements to build the table that is returned to the calling statement.
(Source: http://www.sqlteam.com)
For the sake of simplicity, I'll tackle the first two types of functions.

1. Scalar function example:
1:  create function ComputeSum(@FirstNum int, @SecondNum int)  
2:  returns int  
3:  as  
4:  begin  
5:  Declare @sum int  
6:  set @sum = (@FirstNum + @SecondNum)  
7:  return @sum  
8:  end  

Note: As you can see, the function takes two arguments of type int. Then on the function body, the computation proceeds, then calls the return statement.

--to execute function
 declare @result int  
 set @result = dbo.ComputeSum(5,10)  
 print @result  

15 will be displayed as output

2. Inline Table Functions (make sure you have adventureworks db in your sql server)
create function maxorder()
returns table
as
return select max(SalesOrderID) as LastOrderID
from Sales.SalesOrderHeader

--execute the function
select * from maxorder()

The output returned is: 75123 which is the latest order generated.

Wednesday, November 3, 2010

Simple jquery manipulation (Asp.Net)

I started learning jquery recently. This is a simple tutorial using asp.net website to show alert message and to change background color of label control. Add a label control, an asp.net button control, and input html button. Make sure to reference the jQuery library in your asp.net website.

jQuery Script:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$(document).ready(function() {
    $('#btnAlert').click(function() {
        alert('my first jquery app!');
    });
    $('#btnChangeBackColorLabel').click(function() {
        $('span').css("background-color", "blue");
    });
    $("span").click(function() {
        $(this).css("background-color", "yellow");
    });
});

HTML CODE:
1
2
3
4
5
6
7
<div>  
     <asp:Button ID="btnAlert" runat="server" Text="Show Alert" /> <br />  
      Click Change Me label itself to change background color to yellow<br />  
     <input id="btnChangeBackColorLabel" type="button" value="ChangeColorToBlue" />   
      <asp:Label ID="lblHi" runat="server" Text="Change Me" BorderStyle="Solid">  
    </ asp:Label>  
 </div>  

During page render,labels are rendered as span elements.

Cheers

Getting jquery intellisense to work with VS 2008

I recently played with jquery to work with asp.net/asp.net ajax.
1. Perform these steps in this website:
jQuery Intellisense in Visual Studio 2008
2. Download jquery-1.4.1-vsdoc.js and jquery-1.4.1.min.js from jquery website.
3. Include them in your asp.net website.
4. remove the min in jquery-1.4.1.min.js. So the new jquery library would be like this: jquery-1.4.1.js
5. Add reference to the jquery library in your javascript file.

That's it...Cheers!!!

Monday, November 1, 2010

Calling a SQL Server Stored Procedure using LINQ

1. Make sure you have Northwind database in your Sql server.
2. Create a simple stored procedure to retrieve customers based on a specific country.
The parameter of the stored procedure is of type varchar.
3. Create a simple asp.net website or winforms or console project.
4. Add a Linq to SQL class to your project.
5. Make sure to add the stored procedure in your linq to sql class.
6. In your asp.net website, add a button,textbox and a gridview.
7. In your button event click, add the following snippet:

1
2
3
4
5
6
7
8
protected void Bind()  
{  
    string country = this.txtCity.Text.Trim();  
    var cr = new NorthWindDataContext();  
    var north = cr.CustomerByCountry(country);  
    this.gridCustomers.DataSource = north;  
    this.gridCustomers.DataBind();  
}  

That's it...