Thursday 31 January 2013

Get GridViewRow from Autopostback control event

Sometime we have checkbox control or other control inside gridview.
We need to do when checkbox clicked then some action affected to that row, for example getting datakey, rowindex or getting cell value.

ASPX
<asp:TemplateField>
    <ItemTemplate>
      <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" 
        oncheckedchanged="CheckBox1_CheckedChanged" />
    </ItemTemplate>
</asp:TemplateField>

This sample code is getting gridviewrow from checkbox checked changed event then displaying value from another cell to a label.

C#
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
  GridViewRow gvrow = ((Control)sender).Parent.Parent as GridViewRow;
  Label1.Text = gvrow.Cells[2].Text;
}

Using Naming Container

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
  {
        GridViewRow row=(GridViewRow)(((CheckBox)sender).NamingContainer);

  }

Wednesday 30 January 2013

Export Datatable

C#
public static void ExportToSpreadsheet(DataTable table, string name)
{
   HttpContext context = HttpContext.Current;
   context.Response.Clear();
   foreach (DataColumn column in table.Columns)
   {
    context.Response.Write(column.ColumnName + ";");
   }
   context.Response.Write(Environment.NewLine);
   foreach (DataRow row in table.Rows)
   {
    for (int i = 0; i < table.Columns.Count; i++)
    {
     context.Response.Write(row[i].ToString().Replace(";", string.Empty) + ";");
    }
    context.Response.Write(Environment.NewLine);
   }
   context.Response.ContentType = "text/csv";
   context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".csv");
   context.Response.End();
}

JavaScript : Auto Maximize Window Script

JAVASCRIPT
<script language="JavaScript1.2">
<!--

/***********************************************
* Auto Maximize Window Script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for this script and 100's more.
***********************************************/

top.window.moveTo(0,0);
if (document.all) {
top.window.resizeTo(screen.availWidth,screen.availHeight);
}
else if (document.layers||document.getElementById) {
if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){
top.window.outerHeight = screen.availHeight;
top.window.outerWidth = screen.availWidth;
}
}
//-->
</script>

Source : http://www.dynamicdrive.com/

Monday 28 January 2013

Show Error Detail for 500 internal server error

Web.Config
<customErrors mode="off"/>

<system.webServer>      
        <httpErrors errorMode="Detailed" /> 
        <asp scriptErrorSentToBrowser="true"/>    
    <defaultDocument> 
      <files> 
        <remove value="default.aspx" /> 
        <add value="default.aspx" /> 
      </files> 
    </defaultDocument> 
</system.webServer>

Sunday 27 January 2013

CSS:Remove asp:Hyperlink blue border

When using asp Hyperlink with ImageUrl value. Usualy the image has blue border in browser. You can remove the blue border by CSS code below

ASPX
<head runat="server">

    <style type="text/css">

     a img { border:none; }

    </style>

</head>

Microsoft SQL Version

Microsoft SQL Server Version list :
version Sql
515 Sql7
539 Sql2000
611 Sql2005sp1
612 Sql2005sp2
655 Sql2008sp1(dev10sp1)
sql2008sp2
Sql2008sp3
661 Sql2008r2
705 Sql2012(RC0)
706 Sql2012(RC1/RTM)

Saturday 12 January 2013

Refresh page periodicaly

If you want to refresh a page periodicaly use META tag like below

ASPX/HTML
<meta http-equiv="Refresh" content="300" />

Display Long Date Formatted Regarding LocalCulture

ASPX
using System.Globalization;
using System.Threading;

private void Page_Load(object sender, System.EventArgs e)
{
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0].ToString());
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.UserLanguages[0].ToString());

    lblToday.Text = Today;

    lblMessage.Text = "Culture Info Display Name " + Thread.CurrentThread.CurrentCulture.DisplayName;
}

Adding Double Quote to a string using c#

This sample is how to add "sample" (double quote is included) to a string

C#
string mystring = @""sample""

DataKey get a gridview


Question :

Using VS2010, C #, AspNet4
I principal.aspx page, contains a GridView1 with fields Id and Name, Id is the DataKey.
Select any line on the grid and open a new page called editar.aspx.
How using a button, I get the Id of the selected row to edit the page editar.aspx
Answer :
You can use hyperlinkfield to open editor.aspx

ASPX
<hyperlinkfield Text="Edit Data" DataNavigateUrlFields="ID"
DataNavigateUrlFormatString="editar.aspx?id={0} />

in editar.aspx use querystring("id") to filter the data.
It's not using selected row and datakeyname. Hyperlink only

source : http://forums.asp.net/t/1820678.aspx/1?DataKey+get+a+gridview

Thursday 3 January 2013

Redirect to UnAuthorized Page

Modify your global.asax like below :

VB
 Public Sub Application_AuthorizeRequest(ByVal sender As Object, ByVal e As EventArgs)  
 If (sender.Request.Path.ToUpper().EndsWith("LOGIN.ASPX") And sender.Request.IsAuthenticated) Then  
   sender.Response.Redirect("~/users/Unauthorized.aspx")  
 End If  
 End Sub 

When you already loged-in and want to access unauthorized page, usualy the page redirect to login.aspx again without any message that you rectricted.