Showing posts with label Web Development. Show all posts
Showing posts with label Web Development. Show all posts

Tuesday, July 17, 2012

Controls dynamically added in ASP.NET are lost after a postback

If you are loosing your controls added dynamically to a web page after a full or partial postback, is because you are not creating them in the Page.Init.

According to the ASP.NET Page Life Cycle, PostData (all values inputed on the client side) is applied between the Page.Init and Page.Load, so, if you add a control in the Page.Init, they will not lose their state and you don't have to recreate them.



        protected void Page_Init()
        {

        Control ct = ParseControl("");

        Page.Controls.add(ct);

        }


TemplateControl.ParseControl the easy way to add controls

The TemplateControl.ParseControl() is a powerfull tool to convert a string into a control, if you want to add controls to your web page this object will help you avoid a lot of coding.





// Create the label control
Control ct = ParseControl("");

// Add the control to the page
Page.Controls.Add(ct);

//
// Create a string with a table and a label
string tbl = "b
Text
"; // Create the control Control ctrl = Page.ParseControl(tbl); // Add it to the page PageControls.Add(ctrl);

Thursday, October 6, 2011

Execution of the ASP page caused the Response Buffer to exceed its configured limit

If you are getting this error.

Response object error 'ASP 0251 : 80004005'  Response Buffer Limit Exceeded
Execution of the ASP page caused the Response Buffer to exceed its configured limit. 

The reason this is happening is because the IIS 6 cannot handle the large response.

To solve this problem:

  1. Edit the metabase.xml to increase the AspBufferingLimit value that usually is 4194304 (bytes, that means 4Mb)
    1. Before you edit the file on ISS right click the server and check the box "Enable Direct Metabase Edit"
    2. The file is located %SYSTEMROOT%\system32\inetsrv
  2. Use Response.Flush in your code to send blocks of data to the browser
  3. At the top of the page response.buffer = true in your asp code

Thursday, January 13, 2011

GridView.RowDataBound the last opportunity to access the data item

Before the GridView control can be rendered, each row in the control must be bound to a record in the data source. The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.

A GridViewRowEventArgs object is passed to the event-handling method, which enables you to access the properties of the row being bound. To access a specific cell in the row, use the Cells property of the GridViewRow object contained in the Row property of the GridViewRowEventArgs object. You can determine which row type (header row, data row, and so on) is being bound by using the RowType property.

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Display the company name in italics.
      e.Row.Cells[1].Text = "" + e.Row.Cells[1].Text + "";

    }

  }

GridView.RowDataBound the last opportunity to access the data item

Before the GridView control can be rendered, each row in the control must be bound to a record in the data source. The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.

A GridViewRowEventArgs object is passed to the event-handling method, which enables you to access the properties of the row being bound. To access a specific cell in the row, use the Cells property of the GridViewRow object contained in the Row property of the GridViewRowEventArgs object. You can determine which row type (header row, data row, and so on) is being bound by using the RowType property.

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Display the company name in italics.
      e.Row.Cells[1].Text = "" + e.Row.Cells[1].Text + "";

    }

  }

Source:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

Wednesday, January 5, 2011

How to give permissions to write to the App_Data folder

If you get an error when trying to write programatically to the App_Data folder, that means the web application doesn't have the rights to write on it.
To resolve the issue, you should grant Full Control permission to ASPNET, NETWORK SERVICE and Internet Guest Account system accounts over App_Data folder:

1. In Windows Explorer, go to the folder, right-click on App_Data folder and choose Properties:


2. In General tab, make sure "Read-only" option is not set:


3. In Security tab, gran "Full Control" permission to ASPNET, NETWORK SERVICE and Internet Guest Account accounts:


4. Click OK. Now, ASP.NET should have enough permissions to read/write files and folders in the App_Data folder.

How to register ASP.NET on IIS after installing the .NET Framework

If you install the .NET Framework on a system that has IIS already installed, IIS is automatically configured to handle requests to ASP.NET pages.
If for some reason is not installed, there is a tool that can do the job for you.
The utility is aspnet_regiis.exe, it is located under %WindowsDir%\Microsoft.NET\Framework\vx.y.zzzz\ and you should call it with the -i parameter in the command window, as showed below:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i





Scroll bars in a asp .net datagrid control

There is a way to add scroll bars to a datadrig without using DIV tags, the solution is very simple, all you have to do is to put your grid inside of a panel control and set the ScrollBars attributes to "Both", see the code below:

<asp:Panel ID="pn_orders" runat="server" ScrollBars="Both" Height="350" Width="500">
                    <asp:GridView ID="dgOrders" runat="server">
                    </asp:GridView>
                </asp:Panel>


If you rather to use DIV tags, there is a great web site where you can find a simple example:
http://www.dnzone.com

How to get directory of App_Data in ASP .NET

To get the full path of the App_Data directory of your application use the next code:

 string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"App_data");

Wednesday, September 22, 2010

Debugging AJAX in VS



To debug Ajax you have to do it as the code is executing in the browser.
You write out a debug message using Sys.Debug.trace.
Your page must include the Microsoft AJAX Library JavaScript file. This is done by adding a ScriptManager control to your page.

The following code shows part of an .aspx page that includes a ScriptManager control and a JavaScript function named button1_onclick.
When this function is fired (from the user clicking button1), the trace method is called.











You can view the trace messages output by the AJAX library in the Visual Studio Output window.




Friday, September 10, 2010

A back button for ASP.NET pages



In order to implement a back button in ASP.NET we need to use Java Script history object and avoid the Post Back, the code below is the solution for this problem:



<asp:Button ID="button_history_back"
runat="server"
Text="Back"
CausesValidation="false"
OnClientClick="Javascript:history.go(-1); return false;"
/>




Wednesday, September 1, 2010

Page Life Cycle Events in ASP.NET

EventDescription
PreInitThis is the first real event you might handle for a page. You typically
use this event only if you need to dynamically (from code) set
values such as master page or theme.
This event is also useful when you are working with dynamically
created controls for a page. You want to create the controls inside
this event.
InitThis event fires after each control has been initialized. You can use
this event to change initialization values for controls.
InitCompleteRaised once all initializations of the page and its controls have
been completed.
PreLoadThis event fires before view state has been loaded for the page
and its controls and before PostBack processing. This event is useful
when you need to write code after the page is initialized but
before the view state has been wired back up to the controls.
LoadThe page is stable at this time; it has been initialized and its state
has been reconstructed. Code inside the page load event typically
checks for PostBack and then sets control properties appropriately.
The page’s load event is called first. Then, the load event for each
child control is called in turn (and their child controls, if any). This
is important to know if you are writing your own user or custom
controls.
Control (PostBack) event(s)ASP.NET now calls any events on the page or its controls thatcaused the PostBack to occur. This might be a button’s click event,for example.
LoadCompleteAt this point all controls are loaded. If you need to do additional
processing at this time you can do so here.
PreRenderAllows final changes to the page or its control. This event takes place after all regular PostBack events have taken place. This event takes place before saving ViewState, so any changes made here are saved.
SaveStateCompletePrior to this event the view state for the page and its controls is set. Any changes to the page’s controls at this point or beyond are ignored. This is useful if you need to write processing that requires the view state to be set.
RenderThe Render method generates the client-side HTML, Dynamic Hypertext Markup Language (DHTML), and script that are necessary to properly display a control at the browser. This method is useful if you are writing your own custom control. You override this method to control output for the control.
UnLoadThis event is used for cleanup code. You use it to release any managed resources in this stage. Managed resources are resources that are handled by the runtime, such as instances of classes created by the .NET common language runtime.

Tuesday, August 31, 2010

Special folders in a VS web site

Special folders in a VS web site.

Special folders can be added to a Web site from the Visual Studio menu system. Typically
this involves right-clicking the Web application project and selecting Add ASP.NET Folder.

Folder NameDescription
App_BrowsersContains browser definition files (.browser) that ASP.NET uses
to identify browsers and determine their capabilities. These
files are often used to help support mobile applications.
App_CodeContains source code for classes and business objects (.cs,
.vb, and .jsl files) that you want to compile as part of your
application
App_DataContains application data files (.mdf and .xml files)
App_GlobalResourcesContains resources (.resx and .resources files) that are compiled
into assemblies and have a global scope. Resource files
are used to externalize text and images from your application
code. This helps you support multiple languages and
design-time changes without recompilation of source code
App_LocalResourcesContains resources (.resx and .resources files) that are
scoped to a specific page, user control, or master page in an
application.
App_ThemesContains subfolders that each define a specific theme (or
look) for your site. A theme consists of files (such as .skin,
.css, and image files) that define the appearance of Web
pages and controls.
App_WebReferencesContains Web reference files (.wsdl, .xsd, .disco, and .discomap
files) that define references to Web services
BinContains compiled assemblies (.dll files) for code that you
want to reference in your application. Assemblies in the Bin
folder are automatically referenced in your application


Friday, June 4, 2010

ItemDataBound the last opportunity to access the data item

The ItemDataBound event is raised after an item is data bound to the DataGrid control. This event gives you with the last opportunity to access the data item before it appears on the client. After this event is raised, the data item is null and is no longer available.



For each item that is data bound, you must check the ItemType property. If ItemType is of type Item or AlternatingItem, you receive the value from the last cell of the item, which contains the SaleAmount value. In this sample, you add this value to the running summary variable. When the ItemType is Footer, you receive the total from all of the rows. Therefore, you assign the value of the summary variable to the text value of the last cell.




Code behind:



protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
// Use the ItemDataBound event to customize the DataGrid control.
// The ItemDataBound event allows you to access the data before
// the item is displayed in the control. In this example, the
// ItemDataBound event is used to format the items in the
// CurrencyColumn in currency format.
if((e.Item.ItemType == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.AlternatingItem))
{

// Retrieve the text of the CurrencyColumn from the DataGridItem
// and convert the value to a Double.
Double Price = Convert.ToDouble(e.Item.Cells[2].Text);

// Format the value as currency and redisplay it in the DataGrid.
e.Item.Cells[2].Text = Price.ToString("c");

DropDownList dd = (DropDownList)e.Item.FindControl("dd_control");
Label lb = (Label)e.Item.FindControl("lb_control");

// Do somenting here...
}
else
{
// It is a header or footer
// Do something here......
}

}



Source: http://support.microsoft.com/kb/313154

Tuesday, February 16, 2010

Error when compiling: The type 'xxxx.xx' is ambiguous: it could come from...



If you are getting the next error:
Error 2
The type 'xxxxxx.xxxxx' is ambiguous: it could come from assembly 'c:\WINDOWS\Microsoft.NET\Framework\ v2.0.50727\Temporary ASP.NET Files\MyAPP\5a8a9753\38142b3b\App_SubCode_XXXX.dxin6yoo.DLL' or from assembly 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\MyAPP\5a8a9753\38142b3b\App_Code.9tr7twyo.DLL'. Please specify the assembly explicitly in the type name.

This problem is because you are trying to move from on version to another of .NET, or you have projects with compilation issues.

You can try the next options to solve the problem:
  1. Delete the files inside of the \bin directory
  2. Delete the files inside of the Temporary ASP.NET Files in the next path: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\MyApplication
  3. Be sure you have the next tags on your config file:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>

<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>