Uğur UMUTLUOĞLU
Web Technologies, Microsoft and .NET Technologies

Caching More Than One Table with SqlCacheDependency

Monday, 14 December 2009 19:31 by ugur

SqlCacheDependency object provides caching according to updates in database in ASP.NET applications. We can use this object which is independent from time and provides us important gains according to speed and performance, for caching records from one table. But, in most of the applications by using JOIN statements, we can match records of different tables. Well, how can we do caching operation by SqlCacheDependency object in this kind of situation? Because, depending on its structure, this object was envisioned to work with only one table. The answer of the question: AggregateCacheDependency object (If I am not wrong, comes with .NET Framework 2.0). AggregateCacheDependency object will remove the information of cache object stored in memory in case the state of one of the objects changes and will make caching operation successfully be done by keeping more than one CacheDependency objects inside. The only thing we must do is that adding SqlCacheDependency objects formed for each table to AggregateCacheDependency object by using Add method and adding this object to Cache by using Insert method. You can see how to do this operation below by following these code snippets.

SqlCacheDependency dep1 = new SqlCacheDependency("dbCon", "Products");

SqlCacheDependency dep2 = new SqlCacheDependency("dbCon", "Orders");

SqlCacheDependency dep3 = new SqlCacheDependency("dbCon", "Customers");

AggregateCacheDependency aggDep = new AggregateCacheDependency();

aggDep.Add(dep1,dep2,dep3);

Cache.Insert("ProductOrder", dt, aggDep);


SqlCacheDependency provides us to do caching operations in ASP.NET applications very efficiently. But, the important point that must be considered in caching operations is that if we store large amount of data in Cache and system resources aren’t sufficient, then OutOfMemoryException error might be occurred. So, both errors and to take up a lot of space in memory is not good for the performance of the application. Because of this reason, the size of the data that will be stored in Cache is an important criterion for us, we shouldn’t forget this detail.

Paging in ListView Over QueryString

Thursday, 16 July 2009 11:11 by ugur

ListView that comes with ASP.NET 3.5 presents considerably a flexible structure. In addition to all operations of GridView, also Insert operation can be done by using this control. On the other side, we can compose the HTML output as the way we want. To perform this kind of process was nearly impossible in GridView.

Another advantage of ListView is that we can do paging operation over QueryString! ListView control does the paging operations on a control named DataPager(DataPager has also come with ASP.NET 3.5). ListView control that is formed ordinarily do the paging operations by LinkButtons included in DataPager; thus JavaScript functions called as a result of Postback operation help doing paging operation. So, there won’t be any change in the URL of the page.  If we want paging operation to be done by a value like PageNo carried on QueryString as UrunListele.aspx?SayfaNo=3, the only thing we must do is that to assign a value like SayfaNo to the QueryStringField property of DataPager control. I also want to mention about this property; you can define how many records will be seen in one page by using PageSize property of DataPager.  The changes made in DataPager control and an example screen output can be seen below.

<asp:DataPager ID="DataPager1" runat="server" QueryStringField="SayfaNo" PageSize="15">

    ...

</asp:DataPager>


Actually, it is a very important and useful tip for users to reach the pages they desired over QueryString and for search engine improvement(SEO).

Tags:   , ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Using More Than One DefaultButton in a Page

Wednesday, 1 July 2009 08:52 by ugur

We can assign the ID value of a button control to DefaultButton property in <form> element in order to send the form information filled by the user to the server easily by button object in ASP.NET pages. By this way, the information given in the form can be sent to server when user push Enter key from the keyboard without clicking the button. If there are different forms are included in our page (for example there is a login form at the left of the page and sign up form at the right of the page), how can we make the related button clicked when the user pushes Enter key from the keyboard after filling one of the forms? We can handle this situation again by using DefaultButton property but not the DefaultButton property of <form> element!

Firstly, we must locate both of the forms in Panel controls for solving this situation. Actually, Panel control has DefaultButton property too. Therefore, the forms will function as we want, if we assign the ID value of the button that is included inside of the panel and will do the Submit operation, to the DefaultButton property of the relevant Panel. If you have another form besides these panels and if you want the submit button in this form to be triggered when the Enter key is pushed outside the panels, you can use DefaultButton property of <form> element anyway.  When you test the code snippet given below in your project, you can clearly see the situation.

Default.aspx
<
form id="form1" runat="server" defaultbutton="button1">

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <asp:Button ID="button1" runat="server" Text="Button-1" OnClick="btn_Click" />

    <br />

    <asp:Panel ID="panel1" runat="server" BackColor="LightGreen" DefaultButton="button2" Width="300">

        <asp:TextBox ID="text1" runat="server"></asp:TextBox>

        <asp:Button ID="button2" runat="server" Text="Button-2" OnClick="btn_Click" />

    </asp:Panel>

    <asp:Panel ID="panel2" runat="server" BackColor="LightBlue" DefaultButton="button3" Width="300">

        <asp:TextBox ID="text2" runat="server"></asp:TextBox>

        <asp:Button ID="button3" runat="server" Text="Button-3" OnClick="btn_Click" />

    </asp:Panel>

</form>



Default.aspx.cs

protected
void btn_Click(object sender, EventArgs e)

{

    Response.Write("Clicked button: " + ((Button)sender).Text);

}

Tags:  
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

CheckBoxList: Select All / Clear All

Wednesday, 24 June 2009 23:17 by ugur

Actually it is not related to ASP.NET directly but this code snippet could be useful ASP.NET programmers anyway. Scenario is something like that; we have many record listed in CheckBoxList and we want all of the records can be selected or selected items can be deleted by one move. We can handle this easily by using a simple JavaScript function that is seen below.

<script language="javascript" type="text/javascript">

    function ListeSecim(kontrol, durum) {

        var ctl = document.getElementById(kontrol);

        var inputs = ctl.getElementsByTagName('input'); //Bring input elements in CheckBoxList

        //Assign checked property of all input elements in CheckBoxList to the value in state variable (true/false)

        for (var i = 0; i < inputs.length; i++)

            inputs[i].checked = durum;

    }

</script>

Of course, additionally we need links that will do selecting and deleting all items. We will call the function above by using these links. The only difference between these two links is the value of second parameter that will sent to ListSelect function.

<a href="javascript:ListeSecim('CheckBoxList1', true)">Select All</a>&nbsp;

<a href="javascript:ListeSecim('CheckBoxList1', false)">Clear All</a>

 

Tags:   ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Using More Than One Sitemap File in ASP.NET Applications

Wednesday, 6 May 2009 23:14 by ugur
When we use navigation controls with SiteMapDataSource in ASP.NET applications, we can use only one sitemap file. The reason of this situation is that, the web.config file included in the directory in which .NET Framework is installed that is used as an inheritance by web.config file in the applications has only one SiteMap provider definition. If you look <siteMap> nodule included in web.config file in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG directory in a computer in which .NET Framework 2.0 has been installed, you can see only one provider defined and there is web.sitemap file name that has siteMapFile attribution. Actually, we can solve how to use more than one sitemap file starting in here. If we want to add second sitemap file to our application and use it with navigation controls, firstly we should make a new sitemap provider definition in the web.config file of our application. You can see the code snippet that will be added to <system.web> nodule in web.config file below.

web.config
<
system.web>

    ...

    <siteMap>

        <providers>

            <add name="SiteMapProvider2" siteMapFile="web2.sitemap" type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

        </providers>

    </siteMap>

</system.web>

We defined a new siteMap provider named SiteMapProvider2 that will work with web2.sitemap file. We can now use another sitemap file named web2.sitemap beside the web.sitemap file in this application. We are testing the use of more than one sitemap file by adding two Menu controls and two SiteMapDataSource controls to the ASP.NET page. The point that must be paid attention in here is equaling the SiteMapProvider property of SiteMapDataSource control that will be connect to web2.sitemap file to the SiteMapProvider2 defined in web.config.

 

Default.aspx

<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1"></asp:Menu>

<br />

<asp:Menu ID="Menu2" runat="server" DataSourceID="SiteMapDataSource2"></asp:Menu>

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" />

<asp:SiteMapDataSource ID="SiteMapDataSource2" runat="server" ShowStartingNode="False" SiteMapProvider="SiteMapProvider2" />

Tags:   ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

Delay Loading in ASP.NET AJAX

Thursday, 29 January 2009 09:01 by ugur

You have content that will be uploaded synchronously in your page and content that will be uploaded asynchronously in UpdatePanel. However, the upload process of UpdatePanel content may take more time… If I give more concrete example, the upload process of normal content may take 2 seconds and the upload process of asynchronous content may take 7-8 seconds. If we wait for the whole page uploaded under normal circumstances and then send to the client, we will keep the client waiting at least 7-8 seconds. Well, what if we send the synchronous content to the browser of the client during the first upload of the page while the UpdatePanel continues to upload its own content? I guess you are curious about how to do such a process. In this post, we will search for how to delay loading process in ASP.NET AJAX applications.

First, the needed ingredients; 1 ScriptManager, 1 Label (for synchronous content), 1 UpdatePanel, 1 Button inside the panel and 1 more Label for asynchronous content (if we want our page more attractive, 1 UpdateProgress). The first label will hide the normal content, in other words this control will go to the client after the first upload. The other label is for asynchronously updated content, in other words the upload process in here will be performed just after the page reached to the client. The HTML codes and the code snippet that we will use to update the UpdatePanel asynchronously are given below.

Default.aspx

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:Label ID="lblStatik" runat="server"></asp:Label>

<br /><br />      

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

    <ContentTemplate>

        <asp:LinkButton ID="lbGuncelle" runat="server" Text="Güncelle" onclick="lbGuncelle_Click"></asp:LinkButton><br />

        <asp:Label ID="lblAsenkronIcerik" runat="server"></asp:Label><br />

        <asp:UpdateProgress ID="UpdateProgress1" runat="server">

            <ProgressTemplate>

                Loading. Please wait...

            </ProgressTemplate>

        </asp:UpdateProgress>

    </ContentTemplate>

</asp:UpdatePanel>


Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)

{

    lblStatik.Text = "Static content will be here";

}

protected void lbGuncelle_Click(object sender, EventArgs e)

{

    System.Threading.Thread.Sleep(3000); //For delaying 

    lblAsenkronIcerik.Text = DateTime.Now.ToString();

}

As can be seen the LinkButton control named lbGuncelle updates UpdatePanel content. The page as it is will not create any output in UpdatePanel after the first upload process. But, if we make the LinkButton automatically postbacks when the page is uploaded at the first time, at the same time UpdatePanel content will reach to the client. If we can perform postback process at the same time with the upload of the page at the client side, the upload process will be delayed. There are two questions to be answered in here; first, "how can I catch the exact moment of upload of the page in the client side?", second, "how can I trigger the partial postback process without clicking LinkButton?" Because both of these processes eventuate at the client side, JavaScript must be used. At the moment that the page is loading, the pageLoad function of JavaScript is stepped in. On the other hand, one of the other functions of JavaScript named __doPostBack is used for to apply postback process to a control programmatically. The JavaScript pageLoad function given below will provide the delay in loading the UpdatePanel content during the loading of the page at the first time.

Default.aspx

<script language="javascript" type="text/javascript">

    var firstLoad = true;

    function pageLoad()

    {

        if(firstLoad)

        {

            __doPostBack('lbGuncelle', ''); //Programatically postback for LinkButton control

            firstLoad = false;

        }

    }

</script>

pageLoad function executes while the page is loading to the client. Because the firstLoad variable is true at this moment, LinkButton control makes the postback process and UpdatePanel content is started to be updated. Because we delay loading with Sleep method of Thread class, we can see the delay in updating UpdatePanel content clearly when the page is uploaded to the client.

Tags:   ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (5) | Comment RSSRSS comment feed

ASP.NET MVC 1.0 RC Has Been Released

Wednesday, 28 January 2009 11:12 by ugur

ASP.NET MVC 1.0 RC has been released today, after Internet Explorer 8.0 RC1 that was released yesterday. This important announcement excited me like many ASP.NET developers. You can follow the links above to get more detailed information about RC version. 

Scott Guthrie - ASP.NET MVC 1.0 Release Candidate Now Available
David Hayden - Download ASP.NET MVC 1.0 Release Candidate

Opening New Window with Response.Redirect() Method

Thursday, 15 January 2009 23:47 by ugur

In some occasions, we can direct the user to a specific page by using Response.Redirect method in the code-behind side. Well, how can we make the directed page to be displayed in a new browser window? In fact, the answer of this question is not related to Response.Redirect method. This instance can be solved completely by an operation done on the trigger control of this method. For example, if button control carry out the direct process, then the code snippet can be seen below will be enough. By the way, I think it isn't necessary to mention that Button1 control is involved in the form element named form1:)

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Click Me" OnClientClick="form1.target='_blank';" />

It will be adequate to add standard Redirect method to the Click event method of Button.

protected void Button1_Click(object sender, EventArgs e)

{

    Response.Redirect("Default.aspx");

}

But, how is the process done? form1.target='_blank' expression added in Button1 control and functions in client side transfers the operation to be done in a new window. Thus, the page makes postback on the new page and is directed to Default.aspx page.

Tags:  
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

"You tried to assign the Null value to a variable..." Error and Solution

Tuesday, 6 January 2009 12:44 by ugur
When you use a table formed in MS Access with AccessDataSource control in Insert processes, you might come across an error like "You tried to assign the Null value to a variable that is not a Variant data type". Even if there is not a null value in your query, this error will occur because the identity column of your table has been added to the Insert statement in Visual Studio as the data input column. The solution is actually very simple, you should remove the name and parameter (the ? character in Value region) of identity column (is usually primary key column) from InsertCommand property of AccessDataSource control in HTML codes. At last, you can successfully execute Insert comment after you have removed the parameter added for identity column in InsertParameters collection.
Tags:   ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Inserting Item to DataBounded DropDownList

Thursday, 11 December 2008 09:57 by ugur

In the conditions that we load the records to DropDownList control from the database, all of us want the first choice of the DropDownList control a user informative text. I am sure; it is a necessity for every application to display the first item like “Choose a city”, “Choose a category” etc. choices and then list other items coming from the database. Actually, it is not a logic way to add a “Choose a city” record to the Cities table in the database; thus; we must create a different solution way for this issue! If we want to solve this issue in SQL Server side, UNION terms can be used. However, in this post, I want to discuss another different solution way.      

Actually, this process is a very simple and useful solution way. First, we add the informative text to the DropDownList control as a ListItem from the designer part. We add the informative text to the items collection and then we change the AppendDataBoundItems property of the DropDownList as true. At last, the added choice will be listed as a DropDownList control item.

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"

    DataSourceID="SqlDataSource1" DataTextField="SehirAdi" DataValueField="SehirId">

    <asp:ListItem Value="-1">Bir şehir seçin</asp:ListItem>

</asp:DropDownList>

AppendDataBoundItems

 This is an alternative way for UNION and UNION ALL terms and we should keep this in our minds as a hint…

ASP.NET MVC Beta Released

Monday, 20 October 2008 23:08 by ugur

ASP.NET MVC made an important progress towards being a product and was marketed as beta version. This structure that can be used especially in small and large scaled projects, in which database operations are commonly done, officially will be ready for ASP.NET developers with 1.0 version in forthcoming months. Of course, it can be needed to wait release of .NET Framework 4.0 for properly stabilized ASP.NET MVC.

The innovations that at first I come across are, some improvements related to speeding up coding in Visual Studio and the addition of JavaScript libraries like ASP.NET AJAX, jQuery etc. to the Scripts folder that is now added to the project pattern. If I am not wrong, JQuery first started to be formally come to the fore with a Microsoft product. (You can be sure that JQuery will be more commonly known in the future.)

You can follow this link to install ASP.NET MVC Beta to your computer and http://www.asp.net/mvc link to get more detailed information related to ASP.NET MVC Beta.

Confirmation of GridView's Delete and Update Commands

Friday, 11 July 2008 00:37 by ugur

We may want user confirmation before the completion of the operation during the deletion and update processes in GridView control. If we consider that, especially deletion process can cause fatal errors that can’t be fixed; it would be extremely right not to allow user to trigger this kind of process by clicking a button mistakenly. We can obtain user confirmation for Update and Delete commands by doing two very simple changes in GridView control.

At first step, we need to convert the CommandField which contains Update and Delete buttons to the TemplateField. In this way, we can access Update and Delete buttons and we can add JavaScript code to the necessary client event. For this, we should first click Edit Columns link from smart tag icon of GridView, then select CommanField column in the new window and click Convert this field into a TemplateField link which is below the Properties window that is positioned in the right. By this way, we can have access to the control definitions of buttons like Update, Delete and Cancel from the HTML codes.

At second step, we need to add a small JavaScript code to OnClientClick event of the button. Thus, confirmation process will be done directly in client side instead of passing to server side and an unnecessary postback process will be prevented. The code snippet below shows how to request confirmation from the user when Delete button is clicked. 

<ItemTemplate>

    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Değiştir"></asp:LinkButton>&nbsp;

    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Sil" OnClientClick="return confirm('Silmek istediğinizden emin misiniz?');"></asp:LinkButton>

</ItemTemplate>

GridView can become useful to some extent after these simple two processes. The result can be seen below…

 
Add to Technorati Favorites