Tuesday 31 March 2015

ASP.NET - simple example that opens Google page with submitted search query

Watch on YouTube


using System.Diagnostics;

namespace WebApplication1
{
    public partial class GoogleTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Process.Start("http://google.com/search?q=Computer Programming");
        }
    }
}

ASP.NET - Create TreeView Programmatically

Watch this example on YouTube:

            TreeNode tNode1 = new TreeNode();
            tNode1.Text = "HeadNode1";
            tNode1.Value = "HeadNode1";

            tNode1.CollapseAll();

            TreeNode h1ChildNode1 = new TreeNode();
            h1ChildNode1.Text = "Head1Child1";
            tNode1.ChildNodes.Add(h1ChildNode1);

            TreeNode h1GrandChild1 = new TreeNode();
            h1GrandChild1.Text = "Head1Child1Grand1";
            h1ChildNode1.ChildNodes.Add(h1GrandChild1);

            TreeNode h1ChildNode2 = new TreeNode();
            h1ChildNode2.Text = "Head1Child2";
            tNode1.ChildNodes.Add(h1ChildNode2);

            TreeNode h1ChildNode3 = new TreeNode();
            h1ChildNode3.Text = "Head1Child3";
            tNode1.ChildNodes.Add(h1ChildNode3);

 


         TreeNode tNode2 = new TreeNode();
            tNode2.Text = "HeadNode2";
            tNode2.Value = "HeadNode2";
            tNode2.CollapseAll();

            TreeNode h2ChildNode1 = new TreeNode();
            h2ChildNode1.Text = "Head2Child1";
            tNode2.ChildNodes.Add(h2ChildNode1);

            TreeNode h2ChildNode2 = new TreeNode();
            h2ChildNode2.Text = "Head2Child2";
            tNode2.ChildNodes.Add(h2ChildNode2);

            TreeNode h2ChildNode3 = new TreeNode();
            h2ChildNode3.Text = "Head2Child3";
            tNode2.ChildNodes.Add(h2ChildNode3);

            TreeView myTree = new TreeView();
            myTree.CheckedNodes.Add(tNode1);
            myTree.Nodes.Add(tNode1);
            myTree.Nodes.Add(tNode2);
            Panel1.Controls.Add(myTree);

Monday 30 March 2015

ASP.NET - TreeView - How to remove hyperlink

Watch this example on YouTube

Replace
        <asp:TreeView ID="TreeView1" runat="server">
            <Nodes>
                <asp:TreeNode Text ="aaa"></asp:TreeNode>
                <asp:TreeNode Text ="bbb"></asp:TreeNode>
            </Nodes>
        </asp:TreeView>

With
        <asp:TreeView ID="TreeView1" runat="server">
            <Nodes>
                <asp:TreeNode Text ="aaa" SelectAction="None"></asp:TreeNode>
                <asp:TreeNode Text ="bbb" SelectAction="None"></asp:TreeNode>
            </Nodes>
        </asp:TreeView>

Friday 27 March 2015

ASP.NET - Gridview with DropDownList inside Header

Watch this example on YouTube:


html

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewWithDDL.aspx.cs" Inherits="WebApplication1.GridViewWithDDL" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField HeaderText="Header">
                        <HeaderTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("NAME") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
            </asp:GridView>

        </div>
    </form>
</body>
</html>







C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class GridViewWithDDL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("NAME");
            dt.Rows.Add(new object[] { "Frank" });
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (GridView1.EditIndex == e.Row.RowIndex)
                {
                    DropDownList ddlMonths = (DropDownList)e.Row.Cells[1].FindControl("DropDownList1");
                    ddlMonths.Items.Add(new ListItem("January", "1"));
                    ddlMonths.Items.Add(new ListItem("February", "2"));

                }

            }
        }
    }
}

Saturday 21 March 2015

Microsoft Excel - How to covert time to seconds

watch on YouTube:


=HOUR(A1)*3600 + MINUTE(A1)*60 + SECOND(A1)

ASP.NET - MailTo - Code behind example with body that has link to some html page with "?"

Watch this example on YouTube:

MailTo example with body that has html link in it - how to fix question mark issue:


Replace
string url = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "?argument=Number1";
            string command = "mailto:test@test.com?subject=title&body=click this link: " + url ;
            System.Diagnostics.Process.Start(command);

with

string url = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "%26argument=Number1";
            string command = "mailto:test@test.com?subject=title&body=click this link: " + url ;
            System.Diagnostics.Process.Start(command);

Explanation - some mail clients will remove part of the link just after "?" ?argument=Number1

C# - How to fix error - The name 'Process' does not exist in the current context

Watch this example on YouTube


Replace
  string command = "mailto:test@test.com?subject=title&body=some text";
           Process.Start(command);

with

  string command = "mailto:test@test.com?subject=title&body=some text";
            System.Diagnostics.Process.Start(command);

ASP.NET - How to fix - The type or namespace name 'MailMessage' could not be found (are you missing a using directive or an assembly reference?)

 WATCH THIS EXAMPLE ON YOUTUBE:


The type or namespace name 'MailMessage' could not be found (are you missing a using directive or an assembly reference?)   

Replace

MailMessage message = new MailMessage();

with

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

Wednesday 11 March 2015

How to fix error - Could not find a parto of the path

Replace

       using (SpreadsheetDocument sd = SpreadsheetDocument.Open(FileUpload1.PostedFile.FileName, false))
            {
     
            }

with

       using (SpreadsheetDocument sd = SpreadsheetDocument.Open(FileUpload1.PostedFile.InputStream, false))
            {
     
            }