Thursday, 17 January 2013

Highlight current page link on asp.net

How to highlight current page or link in asp.net.

Let's see it.

I have a site.master page like below.
-------------------------

<head runat="server">

    <title></title>
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
    </asp:ContentPlaceHolder>
</head>

<body>
    <form id="form1" runat="server">
    <table><tr>
            <td><asp:LinkButton ID="LinkButton2" runat="server" PostBackUrl="~/WebForm1.aspx">Page2</asp:LinkButton></td>
           <td><asp:LinkButton ID="LinkButton3" runat="server" PostBackUrl="~/WebForm2.aspx">Page3</asp:LinkButton></td>
        </tr></table>
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
        </asp:ContentPlaceHolder>
     
       
    </div>
    </form>
</body>
------------------------------------------------------------
Now on site.master.cs file write following code.


  public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void changecolor(Page obj)
        {
            if (obj is WebForm1)
            {
                LinkButton2.ForeColor = Color.Green;
                LinkButton3.ForeColor = Color.Empty;

            }
            if (obj is WebForm2)
            {
                LinkButton2.ForeColor = Color.Empty;
                LinkButton3.ForeColor = Color.Green;

            }
        }
    }
---------------------------------------------------------
Now on each page load event write just following code.


 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Site1 myMasterPage = (Site1)Page.Master;
                myMasterPage.changecolor(this.Page);
            }
        }
--------


Done..

Disable previous dates in calendar

How to disable the past dates in calendar control.

For this you need to write following simple code on DayRender event of calendar.


 protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.Date<System.DateTime.Today)
            {
                e.Cell.BackColor = System.Drawing.Color.LightGray;
                e.Day.IsSelectable = false;
            }
        }

---------------------------------------------------------------------




Wednesday, 16 January 2013

How to check trust level for sharepoint 2010 custom application

Go to path:

C:\inetpub\wwwroot\wss\VirtualDirectories\10000  (10000 is your application port no)

Go to Web (config file)

and search for trust

You can see there Full/Custom/minimal kind of options.

How to create WSP from solution file visual studio

Often we need to create wsp(work solution package) from Solution file.

What we do

Step1: In Release mode Clean solution.
Step2:Check in bin folder of solution
bin>Release

Delete craeted wsp file from there.

Step3: Now Go to Build->Package

Now go to bin>Release

Your wsp file is created.you can check CAS policy there in solution file.

Go to package then.

Under Package.Template.xml

You can check CAS policy.

Select/Deselect all listbox items by checkbox

Simple code for executing this is

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{

foreach (ListItem item in ListBox1.Items)   {
item.Selected = CheckBox1.Checked;
   }
}

Populate Gridview based on listbox selection

Often we need to populate grid based on listbox selections.

For this we have a listbox,one button and grid.

On button click event write following code.
-------------------------------------------
protected void Button2_Click(object sender, EventArgs e)
        {
          
            string Seperator = ""; 
            StringBuilder Builder = new StringBuilder();
            foreach (ListItem li in ListBox1.Items)
            {
                if(li.Selected)
                {
                Builder.Append(Seperator).Append("'"+li.Value+"'");
                Seperator = ",";
                }
            }
            string conStr = @"Server=DEVVM5;Database=Sales;Integrated Security=true";
            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();
            string query = "select * from Product where Name in (" + Builder + ")";
            SqlDataAdapter adp = new SqlDataAdapter(query, conn);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            Gridview1.DataSource = ds;
            Gridview1.AllowPaging = false;
            Gridview1.AllowSorting = false;
            Gridview1.DataBind();
            ViewState["Dataset"] = ds;
            Button1.Visible = true;
          

        }

---------------------------------------

Create sharepoint list programmatically

I have written a sample code showing how to create custom sharepoint list programmatically .

-------------------------------------
            SPSite site = new SPSite("URL");
            SPWeb web = site.OpenWeb();
            SPListCollection lists = web.Lists;
            lists.Add("Programmatically List", "Here the list is being created programmatically", SPListTemplateType.GenericList);
            SPList newList = web.Lists["Programmatically List"];
            newList.Fields.Add("My Column", SPFieldType.Text, true);
            newList.Fields.Add("DOB", SPFieldType.DateTime, true);
           
           // Lookup from another list and adding that column along with the fileds present
            SPList targetList = web.Lists["InfoValidation"];
            newList.Fields.AddLookup("Message", targetList.ID, false);
            SPFieldLookup lkp = (SPFieldLookup)newList.Fields["Message"];
            lkp.LookupField = targetList.Fields["Message"].InternalName;
            lkp.Update();

            SPView view = newList.DefaultView;
            view.ViewFields.Add("My Column");
            view.ViewFields.Add("DOB");
            view.ViewFields.Add("Message");
            view.Update();
            Console.WriteLine("List Created Successfully");
            Console.Read();
            site.Dispose();
            web.Dispose();

Run this as a console application.