Friday, December 3, 2010

Difference between RadioButtonList and RadioButton

Difference between RadioButtonList and RadioButton
1. RadioButtonList

RadioButtonList is a single control with a list of RadioButtons.
This is derived from ListControl class. So this will work similar to other list controls like ListBox, DropDownList and CheckBoxList.
For giving a caption for buttons you can use the Text property. You cannot insert a text in between two buttons.
Using the “SelectedIndexChanged” event you will get the selected buttons value (“RadioButtonList1.SelectedValue”).
It is easy to bind this to a DataSource.
private void Bind()
{
RadioButtonList1.DataSource = dsEmployees;
RadioButtonList1.DataTextField = "EmployeeName";
RadioButtonList1.DataValueField = "EmployeeID";
RadioButtonList1.DataBind();
}
If you are using HTML
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal"
onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Text="Male" Value="1" ></asp:ListItem>
<asp:ListItem Text="Female" Value="2" ></asp:ListItem>
</asp:RadioButtonList>

2. RadioButton
“”RadioButton” is a single control, it is derived from “CheckBox” Class. You have to set the GroupName property to identify a group. Also the event handler for the event “CheckedChanged” will help us to do some job. Another one thing is you have to write separate handlers for each radio button.

For e.g.:
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Gender"
AutoPostBack="true" oncheckedchanged="RadioButton1_CheckedChanged" Text="Male" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="Gender"
AutoPostBack="true" oncheckedchanged="RadioButton2_CheckedChanged" Text="Female" />

Friday, October 29, 2010

Format Excel file using XSLT, XslCompiledTransform

See the sample code for creating an Excel file formatted with the help of XSLT file.
------------------------------------------------------------------------------------


private void button1_Click(object sender, EventArgs e)
{
ExportToExcel(GetDataSet(), "Employees.xslt", "c:\\Employees.xls");
}

Method to create a dataset for writing to excel
---------------------------------------------------------

private DataSet GetDataSet()
{
DataSet ds = new DataSet();
DataTable dtEmployees = new DataTable("Employees");
DataColumn colEmpID = new DataColumn("EmpID");
DataColumn colEmpName = new DataColumn("EmpName");
DataColumn colAge = new DataColumn("Age");
DataColumn colLocation = new DataColumn("Location");
DataColumn colDesignation = new DataColumn("Designation");
dtEmployees.Columns.AddRange(new DataColumn[] { colEmpID, colEmpName, colAge, colLocation, colDesignation });

DataRow dr = dtEmployees.NewRow();
dr[colEmpID] = "E001";
dr[colEmpName] = "George";
dr[colAge] = "45";
dr[colLocation] = "USA";
dr[colDesignation] = "PM";
dtEmployees.Rows.Add(dr);

dr = dtEmployees.NewRow();
dr[colEmpID] = "E002";
dr[colEmpName] = "James";
dr[colAge] = "30";
dr[colLocation] = "INDIA";
dr[colDesignation] = "TL";
dtEmployees.Rows.Add(dr);

dr = dtEmployees.NewRow();
dr[colEmpID] = "E002";
dr[colEmpName] = "Mary";
dr[colAge] = "35";
dr[colLocation] = "UK";
dr[colDesignation] = "Developer";
dtEmployees.Rows.Add(dr);

ds.Tables.Add(dtEmployees);
return ds;
}


Method to create a XSLT formatted excel file
---------------------------------------------------------

public bool ExportToExcel(DataSet ds, string xsltPath, string pathToSave)
{
try
{
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.LoadXml(ds.GetXml());
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(xsltPath);
StreamWriter strWriter = new StreamWriter(pathToSave);
xsl.Transform(xmlDoc, null, strWriter);
strWriter.Flush();
return true;
}
catch (Exception ex)
{
throw ex;
}
}


Save the below mentioned XSLT in a file with name "Employees.xslt" in "C" drive to run the sample
---------------------------------------------------------

<xsl:stylesheet version="1.0"

xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ms="urn:schemas-microsoft-com:xslt"
>
<xsl:template match="NewDataSet">
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Styles>
<Style ss:ID="s21">
<Font ss:FontName="Arial" ss:Size="10" ss:Bold="1"/>
<Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="s22">
<Font ss:FontName="MS Sans Serif" ss:Size="10"/>
<Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="s25">
<Font ss:Color="Red" ss:FontName="MS Sans Serif" ss:Size="10"/>
<Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
</Style>
</Styles>
<Worksheet>
<xsl:attribute name="ss:Name">Employees List</xsl:attribute>
<Table>
<Column ss:Width="50"/>
<Column ss:Width="50"/>
<Column ss:Width="30"/>
<Column ss:Width="90"/>
<xsl:apply-templates select="NewDataSet"/>
<Row>
<Cell ss:StyleID="s21">
<Data ss:Type="String">Name</Data>
</Cell>
<Cell ss:StyleID="s21">
<Data ss:Type="String">Employee ID</Data>
</Cell>
<Cell ss:StyleID="s21">
<Data ss:Type="String">Age</Data>
</Cell>
<Cell ss:StyleID="s21">
<Data ss:Type="String">Location</Data>
</Cell>
<Cell ss:StyleID="s21">
<Data ss:Type="String">Designation</Data>
</Cell>
</Row>
<xsl:for-each select="Employees">
<Row>
<Cell ss:StyleID="s22">
<Data ss:Type="String">
<xsl:value-of select="EmpID"/>
</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">
<xsl:value-of select="EmpName"/>
</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">
<xsl:value-of select="Age"/>
</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">
<xsl:value-of select="Location"/>
</Data>
</Cell>
<xsl:choose>
<xsl:when test="Designation='PM'">
<Cell ss:StyleID="s25">
<Data ss:Type="String">
<xsl:value-of select="Designation"/>
</Data>
</Cell>
</xsl:when>
<xsl:otherwise>
<Cell ss:StyleID="s22">
<Data ss:Type="String">
<xsl:value-of select="Designation"/>
</Data>
</Cell>
</xsl:otherwise>
</xsl:choose>
</Row>
</xsl:for-each>
</Table>
</Worksheet>
</Workbook>
</xsl:template>
</xsl:stylesheet>

Wednesday, October 13, 2010

Disable Save/Open Button in ASP.NET Download Dialog box

Code Behind

protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=sample.xls");
Response.Write("Hello World");
Response.End();
}

HTML Page

To disable Save button, write this meta tag
<head runat="server">
<meta name="DownloadOptions" content="nosave" />
</head>

To disable Open button, write this meta tag
<head runat="server">
<meta name="DownloadOptions" content="noopen" />
</head>

Friday, October 8, 2010

DataKey from a GridView RowCommand Event

Method to get DataKey from a GridView RowCommand Event

protected void grdEmployees_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
int index = row.RowIndex;
int employeeID = Convert.ToInt32(grdEmployees.DataKeys[index].Value);
}

}

For this eg: There should be GridView Control with a TemplateField and that should contain a Linkbutton with CommandName="Select"

SQL Query to Get SP and Text from DB

SELECT sysobjects.name, syscomments.text
FROM sysobjects JOIN syscomments ON
sysobjects.id = syscomments.id
WHERE xtype='P'

Write to Event Viewer

/// <summary>
/// Method to write Errors to Event viewer
/// </summary>
/// <param name="message">message</param>
static void WriteToEventViewer(string message)
{
string sSource = "Sample";
string sLog = "MyApplication";

if (!EventLog.SourceExists(sSource))
{
EventLog.CreateEventSource(sSource, sLog);
}
EventLog.WriteEntry(sSource, message,

EventLogEntryType.Error);
}

for more information refer this
http://msdn.microsoft.com/en-us/library/xzwc042w.aspx

Get Client Machine IP Address

/// <summary>
/// Method to get Client IP number
/// </summary>
/// <returns>IP Address</returns>
private string GetIPNumber()
{
return Request.UserHostAddress;
}