Monday, December 12, 2011

ASP.NET tutorial, ASP.NET Code

Selecting a drop down list text field by giving the text field
To select a drop down list text field by giving the text field parameter 
ddlist.Items.FindByText(dt.Rows(0)("text").ToString()).Selected = True 
To select by value it is 
dropdownlist.selectedvalue = value 



Adding a new list item to a drop down
To add a new list item at run item to a drop down list with values 

ddllist.Items.Add(New ListItem(dt.Rows(0)("ID").ToString(), dt.Rows(0)("text").ToString()))



Converting a DataGrid to HTML and outputting it to screen

This  you how to convert a DataGrid to HTML and outputting it to screen 

string ConnStr = "Server=servername;Database=databasename;UID=username;PWD=password;";
SqlConnection sqlConn = new SqlConnection(ConnStr);
string query = "getTest";
SqlCommand sqlComm = new SqlCommand(query,sqlConn);
sqlComm.CommandType=CommandType.StoredProcedure;
sqlConn.Open();
SqlDataReader sqlReader;
sqlReader = sqlComm.ExecuteReader();
dgView.DataSource = sqlReader;
dgView.DataBind();
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
dgView.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter);
Response.End(); 

To add sorting to a datagrid

/////////code behind 

Private Sub dgView_SortCommand(ByVal source As Object, ByVal e As System .Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgView.SortCommand
Dim arrSort(1) As String

arrSort = txtSort.Value.Split(" "c)

'' If the selected item to sort by equals the previous item sorted then ...
If e.SortExpression = arrSort(0) Then
'' if the current sort type equals ASC then set sort type equal to DESC
If arrSort(1) = "ASC" Then
txtSort.Value = e.SortExpression & " DESC"
Else
txtSort.Value = e.SortExpression & " ASC"
End If
Else
txtSort.Value = e.SortExpression & " ASC"
End If

'' Bind the newly sorted data to the DataGrid
dgView.EditItemIndex = -1
Call bindData()
End Sub


Private Sub bindData()

Dim dt As New DataTable
dt = getfromdatabase()

Dim dv As DataView = New DataView(dt)
dv.Sort = txtSort.Value

dgView.DataSource = dv
dgView.DataBind()


If Not dt Is Nothing Then dt.Dispose()
dt = Nothing
End Sub


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Me.IsPostBack Then
txtSort.Value = "ID ASC"
bindData()
End If
End Sub 

Splitting a string in C#

//string = "a#b#c#"
string[] split;
split = string.Split('#');
string1 = split[0]; //a
string2 = split[1]; //b
string3 = split[2]; //c

Adding attributes to a control in a datagrid

private void dgVanity_ItemCreated(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch(e.Item.ItemType)
{
case (ListItemType.Item) :
TextBox tt = new TextBox();
tt = (((TextBox)(e.Item.FindControl("txtPreferredSpelling"))));
tt.Attributes.Add("onkeyup","validateFloat(this,10,2)");
break;

}

}



Adding a Confirmation message box

Private Sub dgView_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgView.ItemDataBound
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem
Dim deleteButton As LinkButton = CType(e.Item.Cells(6).Controls(0), LinkButton)
deleteButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete this ?');")
End Select
End Sub 



Private Sub dgView_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgView.DeleteCommand
If e.CommandName = "Delete" Then
'delete from database commands
BindData()
End If
End Sub 

Getting multiple resultset from a query


'' language VB.NET 
''Query with 3 select statements 
Dim strSQL as String = _ 
"Select * from Table1; 
Select * from Table2; 
Select * from Table3;" 

Dim DS as New DataSet() 
Dim daAdapter as new SqlDataAdapter(strSQL, cnConnection) 
daAdapter.Fill(DS) 


With dataGrid1 
.DataSource = DS.Tables("Table") 
.DataBind() 
End With 

With dataGrid2 
.DataSource = DS.Tables("Table1") 
.DataBind() 
End With 

With dataGrid3 
.DataSource = DS.Tables("Table2") 
.DataBind() 
End With 



Hosting multiple websites on one domain

///// add this in the asp.net page. load even as shown below 
private void Page_Load(object sender, System.EventArgs e) 

if(!IsPostBack) 

string url = Request.Url.ToString().ToLower(); 
string domain; 
// get the domain name 
if ( url.StartsWith( "http://" ) ) 
url = url.Substring( 7 ); 
int x = url.IndexOf( '/' ); 
if ( x > 0 ) 
domain = url.Substring( 0, x ); 
else domain = url; 

// redirect based on domain 
switch( domain ) 

case "www.domain1.com": 
case "domain2.com": 
Response.Redirect( "home.aspx", true ); 
return; 

case "www.domain2.com": 
case "domain2.com": 
Response.Redirect( "index.html", true ); 
return; 



Server.Transfer("index.aspx"); 

Storing a uploaded file in binary format in a database

''''''''''btnupload click is the fileupload button 
'''''VB.NET 
Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click 

If FileInput.PostedFile Is Nothing Then 
lblError.Text = "No file specified." 
Else 
If FileInput.PostedFile.ContentLength > 2097152 Then ' 2097152 Bytes = 2 MB 
lblError.Text = "File size cannot exceed 2 MB." 
Return 
End If 


Dim byteFile As Byte() 
''''' look fro code here for this function 
byteFile = GetByteFromPostedFile(FileInput.PostedFile) 
''''store to database 
Storetodatabase(filename,byteFile) 

End Sub 

DataGrid Currency Format

<%# String.Format("{0:c}",Convert.ToInt32(Container.DataItem("ColumnName"))) %> 
... 
Format Pattern Name 
C or c Currency format 
D or d Decimal format 
(Works for integers only!) 
E or e Scientific (exponential) format 
F or f Fixed-point format 
G or g General format 
N or n Number format 
P or p Percent format 
X or x Hexadecimal format 
(Works with integers only!) 


No comments :

Post a Comment