Wednesday, March 17, 2010

How to find control on GridView RowCommand Event?

Here I'm showing how to find a control within a gridview to get some data of that control.
<Columns>
 <asp:TemplateField HeaderText="Team">
    <ItemTemplate>
     <asp:Label ID="lblTeam" runat="server" Text='<%# Eval("TeamName")%>'></asp:Label>
   </ItemTemplate>
 </asp:TemplateField>
</Columns>
<Columns>
 <asp:TemplateField>
    <ItemTemplate>
       <asp:LinkButton ID="lnkEdit" Text="EDIT" runat="server" CssClass="coachEdit"   CommandArgument='<%# Eval("TeamId")%>' CommandName="Redirect"></asp:LinkButton>
   </ItemTemplate>
 </asp:TemplateField>
</Columns>

Here i need the Team Name from the lblTeam and I'm like to get this when I click the Edit link means lnkEdit  so on RowCommand event i have to write this code


GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
 Label lblDate = (Label)row.Cells[0].FindControl("lblDate");
string teamName= lblDate.Text;
int rowindex = row.RowIndex;

Friday, February 26, 2010

Tuesday, February 23, 2010

How to upload and resizing image in asp.net?

Here is the code:
protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (flUpImage.HasFile)
        {
            picName = flUpImage.FileName;
            string SavePath = Server.MapPath("~/Coach/Data/" + Convert.ToString(iPlayConstant._PLAYER_ID) + "/");
            DirectoryInfo di = new DirectoryInfo(SavePath);
            if (!di.Exists)
            {
                di.Create();
                SetPermissions(SavePath);
            }
            string LocalPath = Server.MapPath("~/Coach/Data/" + Convert.ToString(iPlayConstant._PLAYER_ID) + "/Profile.gif");
            flUpImage.SaveAs(LocalPath);
            System.Drawing.Image imgSaved = System.Drawing.Image.FromFile(LocalPath);
            System.Drawing.Image imgCroped;
            imgCroped = ImageResize(imgSaved, 250, 250);//pass the height and width value to create thumbnail picture
            imgProfile.Width = imgCroped.Width;
            imgProfile.Height = imgCroped.Height;
            imgSaved.Dispose();
            imgCroped.Save(LocalPath);
            imgCroped.Dispose();
            imgProfile.Src = "~/Coach/Data/" + Convert.ToString(iPlayConstant._PLAYER_ID) + "/Profile.gif";
        }
    }


    protected static System.Drawing.Image ImageResize(
        System.Drawing.Image imgPhoto, int dWidth, int dHeight)
    {
        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;
        int destWidth = dWidth;
        int destHeight = dHeight;
        int sourceX = 0;
        int sourceY = 0;
        int destX = 0;
        int destY = 0;
        float percent = 0;

        if (sourceHeight > sourceWidth)
        {
            percent = (float)sourceWidth / (float)sourceHeight;
            destWidth = (int)(percent * destWidth);
        }
        else
        {
            percent = (float)sourceHeight / (float)sourceWidth;
            destHeight = (int)(percent * destHeight);
        }
        if (destWidth > destHeight)
        {
            percent = (float)sourceHeight / (float)sourceWidth;
            destWidth = (int)(percent * destWidth);
        }
        System.Drawing.Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        grPhoto.DrawImage(imgPhoto,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
        GraphicsUnit.Pixel);
        grPhoto.Dispose();
        return bmPhoto;
    }