Tuesday, December 28, 2010

Pre-populate ReadOnly TextBox in a FormView with current date and time

I had to pre-populate a FormView that contains a textbox with the current date and time.

Googled and found this link which proved to be quite useful, though not exactly what I wanted.

Pre-populate DetailsView InsertItemTemplate TextBox with User.Identity.Name value - ASP.NET Forums

**

Anyway, what you need to do is to first add an event handler for "ondatabound" in the FormView.
Assuming you have a textbox named "TextBoxDataOfEntry" in the InsertItemTemplate of the FormView, this is how I got the current date and time to be populated in the textbox.

protected void FormView1_DataBound(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Insert)
{
DateTime datetime = DateTime.Today;
TextBox tbDateOfEntry = (TextBox)FormView1.FindControl("TextBoxDateOfEntry");
tbDateOfEntry.Text = datetime.ToString();
}
}//end FormView1_DataBound

Tuesday, December 21, 2010

Sorting a GridView

http://geekswithblogs.net/azamsharp/archive/2006/04/27/76612.aspx

This code from Azam Sharp works well ^_^


private const string ASCENDING = " ASC";
 
private const string DESCENDING = " DESC";
 public SortDirection GridViewSortDirection
    {
        
get
        
{
            
if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;

            
return (SortDirection) ViewState["sortDirection"];               
        }
        
set { ViewState["sortDirection"] = value; }
    }


    
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        
string sortExpression = e.SortExpression;

        
if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING);
        }
        
else
        
{
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING);
        }  
      
    }

    
private void SortGridView(string sortExpression,string direction)
    {
        
//  You can cache the DataTable for improving performance
        
DataTable dt = GetData().Tables[0];

        DataView dv = 
new DataView(dt);
        dv.Sort = sortExpression + direction;        

        GridView1.DataSource = dv;
        GridView1.DataBind();        
    }

Monday, November 22, 2010

Get the resolution of a jpeg image using C# and the .NET Environment

Source: http://stackoverflow.com/questions/123838/get-the-resolution-of-a-jpeg-image-using-c-and-the-net-environment

Using System.Drawing.Image;

It depends what you are looking for... if you want the DPI of the image then you are looking for the HorizontalResolution which is the DPI of the image.

Image i = Image.FromFile(@"fileName.jpg");
i.HorizontalResolution;

If you want to figure out how large the image is then you need to calculate the measurements of the image which is:

int docHeight = (i.Height / i.VerticalResolution);
int docWidth = (i.Width / i.HorizontalResolution);

This will give you the document height and width in inches which you could then compare to the min size needed.

Thursday, July 29, 2010

Checking if a string is numeric in C#

 int sidNumber;
bool isInteger = Int32.TryParse(sid, out sidNumber);

Wednesday, July 28, 2010

Add a JavaScript alert box

http://www.beansoftware.com/ASP.NET-Tutorials/Message-Box.aspx

http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=32564

Credits to chakravarthy


string tmp = "";
tmp = "";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", tmp);
and if u want to alert message for Exceptions
string tmp = "";
tmp = "";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", tmp);

Adding Client-Side Confirmation When Deleting

http://www.asp.net/data-access/tutorials/adding-client-side-confirmation-when-deleting-cs

Here's some example code

<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="True" 
CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you wish to delete this record?');" />