Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Saturday, March 31, 2012

Binding Data from SharePoint Links List to SpGridView without a single line of loop.

ascx
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>Binding Data from SharePoint Links List to SpGridView without a Single line of Loop.</td>
</tr>
<tr>
<td>
<SharePoint:SPGridView ID="sgvLinks" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
<Columns>
<asp:BoundField HeaderText="Link Name" DataField="URL" />
</Columns>
</SharePoint:SPGridView>
</td>
</tr>
</table>


Ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
namespace DLLWebPart.DLLWebPart
{
    public partial class DLLWebPartUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                sgvLinks.RowDataBound += new GridViewRowEventHandler(sgvLinks_RowDataBound);
                SPWeb currentWeb = SPContext.Current.Web;
                SPList LinksList = currentWeb.Lists["Links"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                sQuery.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='URL' />";
                SPListItemCollection myColl = LinksList.GetItems(sQuery);
                if (myColl.Count > 0)
                {
                    if (!IsPostBack)
                    {
                        sgvLinks.DataSource = myColl.GetDataTable();
                        sgvLinks.DataBind();
                    }
                }
        }
        string strItemID = "";
        void sgvLinks_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        strItemID = sgvLinks.DataKeys[e.Row.RowIndex].Value.ToString();
                        SPWeb currentWeb = SPContext.Current.Web;
                        SPList lst = currentWeb.Lists["Links"];
                        SPQuery sQuery = new SPQuery();
                        sQuery.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>"+strItemID+"</Value></Eq></Where>";
                        sQuery.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='URL' />";
                        SPListItemCollection myColl = lst.GetItems(sQuery);
                        if (myColl.Count > 0)
                        {
                            SPListItem item = myColl[0];
                            SPFieldUrlValue URL = new SPFieldUrlValue(item["URL"].ToString());
                            e.Row.Cells[0].Text = "<a href='"+URL.Url+"'>"+URL.Description+"</a>";
                       
                        }
                    });
                }
            }
            catch (Exception ex)
            {

                Response.Write(ex.ToString());
            }
        }
    }
}




Friday, March 30, 2012

Create a Modal Dialog Window using JavaScript

<script language="javascript" type="text/javascript">
   var answer = window.showModalDialog("Your Page URL","","dialogWidth:700px; dialogHeight:700px; center:yes");
    </script>

How to make PDF File Open in a Browser in SharePoint 2010.

Issue: When you try open a aspx/html/pdf or other such files from your SharePoint Foundation 2010 site; you are prompted for downloading the file rather than opening the file in Browser.
Resolution:
This is an enhanced feature in SharePoint Foundation 2010 and IE8.  In the previous versions of SharePoint; you could open the files directly.  By default; the browser file handling option is set to strict in Central Administration
To change this setting; you need to follow the steps given below.  Please note that the steps needs to be performed in the Central Administration; which is available in your SharePoint Server.
1.    Goto Web Application and Select the WebApplication under which the Ste Collection and files are located.
2.   Click on General Settings from the top “Ribbon”.
3.   Locate the section “Browser File Handling”
4.   Change it to Permissive.
Note:-
    You should have Adobe Reader Installed in your Machine.

Thursday, March 29, 2012

Binding Data to TD on Page Load using JQuery

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Binding Data to TD on Page Load using JQuery</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function()
{
    $(function()
    {
        var strDynamicTable="";
        strDynamicTable+="<Table width='100%' cellpadding='0' cellspacing='0'>";
        strDynamicTable+="<Tr>";
        strDynamicTable+="<Td>";
        strDynamicTable+="<Strong>";
        strDynamicTable+="Test Bind Data Dynamically";
        strDynamicTable+="</Strong>";
        strDynamicTable+="</Td>";
        strDynamicTable+="</Tr>";
        strDynamicTable+="</Table>";
        $("#tdBindDataDynamically").html(strDynamicTable);
    });
});
</script>
</head>

<body>
<table width="100%" cellpadding="0" cellspacing="0"><tr><td id="tdBindDataDynamically"></td></tr></table>
</body>

</html>

Validations using JQuery

<html>

<head>
<title>Validations using JQuery</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function()
{
    $("#btnValidate").click(function()
    {
        if($("#txtValidate").val()=="")
        {
            alert('Please enter some thing in TextBox');
            return false;
        }
       
        if($("#ddlValidate").get(0).selectedIndex==0)
        {
            alert('Please select a valid Option');
            return false;
        }
       
        if(!$("#chkValidate").get(0).checked)
        {
            alert('Please select CheckBox');
            return false;
        }
    });
});
</script>
</head>

<body>
<table cellpadding="0" cellspacing="0">
<tr><td colspan="2" align="center" style="font-weight:bold;">Validations using JQuery</td></tr>
<tr><td>Validate TextBox</td><td><input type="text" id="txtValidate" /></td></tr>
<tr><td>Validate DropDown</td><td><select id="ddlValidate"><option value="Select">Select</option><option value="A">A</option></select></td></tr>
<tr><td>Validate CheckBox</td><td><input type="checkbox" id="chkValidate" value="Validate CheckBox" /></td></tr>
<tr><td colspan="2" align="center"><input type="button" value="Validate Controls" id="btnValidate" /></td></tr>
</table>
</body>

</html>

Getting Dropdown selected Value and Selected Text using JQuery

<html>
<head>
<title>Enter Value in DropDown</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function()
{
    $("#btnGetDropDownValue").click(function()
    {
        alert('DropDown Value '+$("#ddlSample").val());
        alert('DropDown Text '+$("#ddlSample").find('option').filter(':selected').text());
    });
});
</script>
</head>

<body>
<table cellpadding="0" cellspacing="0">
<tr><td>Enter Value in TextBox</td><td><select id="ddlSample"><option value="A">A1</option><option value="B">B1</option><option value="C">C1</option></select></td></tr>
<tr><td colspan="2" align="center"><input type="button" id="btnGetDropDownValue" value="Get DropDown Value" /></td></tr>
</table>
</body>

</html>

Getting a TextBox Value using JQuery


<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Enter Value in TextBox</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function()
{
    $("#btnGetTextBoxValue").click(function()
    {
        alert($("#txtSample").val());
    });
});
</script>
</head>

<body>
<table width="100%" cellpadding="0" cellspacing="0">
<tr><td>Enter Value in TextBox</td><td><input type="text" id="txtSample" /></td></tr>
<tr><td colspan="2" align="center"><input type="button" id="btnGetTextBoxValue" /></td></tr>
</table>
</body>

</html>

JQuery sample to Show & Hide Divs

<html>

<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Apple</title>
<script language="javascript" type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#btnAlphabets").click(function(){
$("#divAlphabets").show();
$("#divNumbers").hide();
});
$("#btnNumbers").click(function(){
$("#divAlphabets").hide();
$("#divNumbers").show();
});
});
</script>
</head>

<body>
<div id="divAlphabets">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>Apple</td>
</tr>
<tr>
<td>Boy</td>
</tr>
<tr>
<td>Cat</td>
</tr>
<tr>
<td>Dog</td>
</tr>
<tr>
<td>Elephant</td>
</tr>
<tr>
<td>Fan</td>
</tr>
<tr>
<td>Gun</td>
</tr>
<tr>
<td>Hut</td>
</tr>
<tr>
<td>Ink</td>
</tr>
<tr>
<td>Kite</td>
</tr>
<tr>
<td>Lamp</td>
</tr>
<tr>
<td>Monkey</td>
</tr>
<tr>
<td>Nose</td>
</tr>
<tr>
<td>Owl</td>
</tr>
<tr>
<td>Pen</td>
</tr>
<tr>
<td>Queen</td>
</tr>
<tr>
<td>Rat</td>
</tr>
<tr>
<td>Sun</td>
</tr>
<tr>
<td>Tree</td>
</tr>
<tr>
<td>Umbrella</td>
</tr>
<tr>
<td>Van</td>
</tr>
<tr>
<td>Watch</td>
</tr>
<tr>
<td>X-Ray</td>
</tr>
<tr>
<td>Yacht</td>
</tr>
<tr>
<td>Zebra</td>
</tr>
</table>
</div>
<div id="divNumbers" style="display:none;">
<table>
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>3</td>
</tr>
<tr>
<td>4</td>
</tr>
<tr>
<td>5</td>
</tr>
<tr>
<td>6</td>
</tr>
<tr>
<td>7</td>
</tr>
<tr>
<td>8</td>
</tr>
<tr>
<td>9</td>
</tr>
<tr>
<td>10</td>
</tr>
<tr>
<td>11</td>
</tr>
<tr>
<td>12</td>
</tr>
<tr>
<td>13</td>
</tr>
<tr>
<td>14</td>
</tr>
<tr>
<td>15</td>
</tr>
<tr>
<td>16</td>
</tr>
<tr>
<td>17</td>
</tr>
<tr>
<td>18</td>
</tr>
<tr>
<td>19</td>
</tr>
<tr>
<td>20</td>
</tr>
<tr>
<td>21</td>
</tr>
<tr>
<td>22</td>
</tr>
<tr>
<td>23</td>
</tr>
<tr>
<td>24</td>
</tr>
<tr>
<td>25</td>
</tr>
</table>

</div>
<div>
<table><tr><td><input type="button" id="btnAlphabets" value="Show Alphabets" /></td><td><input type="button" id="btnNumbers" value="Show Numbers" /></td></tr></table>
</div>
</body>

</html>