Pages

Showing posts with label page. Show all posts
Showing posts with label page. Show all posts

Thursday, October 9, 2014

How To: Using asp.net Validation Controls to be used with Custom Usercontrols

To use asp.net Required Field Validator, Range Validator ..., etc you must have a ValidationProperty attribute that specifies which value from your custom user control the validation controls to be validated validate.

So.. In your custom usercontrol  class add the ValidationProperty attribute and a property that returns the data in a format that is suitable for the validation controls.

Usercontrol
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="jpTimeChooser35UC.ascx.cs"
    Inherits="jpTimeChooser35UC" %>
<table cellpadding="0" cellspacing="0" dir="rtl" style="text-align: center;">
            <tr>
                <td>
                    <asp:TextBox ID="minTxtBx" MaxLength="2" runat="server" Width="30px" CssClass="tblField" Text="30" Style="text-align: center" >
                    </asp:TextBox>
                </td>
                <td>
                    :
                </td>
                <td>
                    <asp:TextBox ID="hourTxtBx" runat="server" MaxLength="2" Width="30px" CssClass="tblField" Text="07" Style="text-align: center" >
                    </asp:TextBox>
                </td>
                <td style="padding-right: 5px">
                    <asp:DropDownList ID="timeDDL" CausesValidation="false" Font-Size="10pt" Width="50px" runat="server" CssClass="tblField">
                        <asp:ListItem Selected="True">ص</asp:ListItem>
                        <asp:ListItem>م</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr style="color: Gray; text-align: center; padding: 0px; font-family: Tahoma; font-size: 9pt;">
                <td>
                    دقيقة
                </td>
                <td></td>
                <td>
                    ساعة
                </td>
            </tr>
        </table>
        
 
Preview 



Usercontrol's Code:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
[ValidationProperty("TimeValue")]
public partial class TmeChooser35UC : System.Web.UI.UserControl
{
    public object TimeValue
    {
        get
        {
            string TimeString = this.ucHours+":"+this.ucMinutes+" "+this.ucTime;
            if (TimeString == ": ص")
            {
                return "";
            }
            else
            {
                return TimeString;
            }
        }
    }
    public string ucMinutes
    {
        get { return minTxtBx.Text.ToString(); }
        set { minTxtBx.Text = ucMinutes; }
    }
    public string ucHours
    {
        get { return hourTxtBx.Text.ToString(); }
        set { hourTxtBx.Text = ucHours; }
    }
    public string ucTime
    {
        get { return timeDDL.SelectedValue.ToString(); }
        set { timeDDL.SelectedValue = (timeDDL.Items.Contains(new ListItem(ucTime))) ? timeDDL.SelectedValue = ucTime : ""; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

 
and then use a RequiredFieldValidator and the Usercontrol  in a WebPage 
<%@ Page Language="C#" Culture="ar-sa" Title="Untitled Page" %>
<%@ Register Src="jpTimeChooser35UC.ascx" TagName="jpTimeChooser35UC"   TagPrefix="uc1" %>

<uc1:jpTimeChooser35UC ID="fromTimeUC"  runat="server" />
<asp:RequiredFieldValidator runat="server"ErrorMessage="ERROR_MESSAGE" 
ID="RequiredFieldValidator1" ControlToValidate="fromTimeUC" 
SetFocusOnError="True"></asp:RequiredFieldValidator>

<asp:Button ID="submitBtn" runat="server" Text="submit"
CssClass="tblButtons" Visible="true" OnClick="submitBtn_Click"/>
Preview 


  













That's It .. Hope this was helpful :)

Reference: Microsoft Support #310082

Saturday, August 16, 2014

How To: Load SharePoint web part User Control Inside another SharePoint web part without C# Coding

simple way to Load SharePoint web part User Control Inside another  SharePoint web part without C# Coding is done by :

  1. registering the user control
    <%@ Register src="webPartUC.ascx" tagname="webPartUC" tagprefix="uc" %>

  2. loading the user control <uc:webPartUC ID="webPartUC1" runat="server" />
if you wanted to pass webpart properties.. all you have to do is to specify the values as follows:
<uc:webPartUC ID="webPartUC1" LabelText="TEXT" runat="server" />

if you wanted to pass webpart properties from within a DataList Control Items .. all you have to do is to specify the values as follows making sure to add the below line inside the DataList:

 <asp:DataList ID="usersDL" runat="server" Width="100%" >
   <ItemTemplate>
         <uc:webPartUC ID="webPartUC1"
              LabelText=<%# Eval("userName")%> runat="server" />

    </ItemTemplate>
 </asp:DataList>


Thats it :)

    

Tuesday, September 3, 2013

Passing url parameters to webpart in SharePoint

To pass the URL parameters to a webpart page in SharePoint, we can't use the "Page Viewer" webpart because it does not allow us to use the parameters in the URL.

So we use teh "Content Editor" webpart since it allows us to embed javascript code in it. We can then use an "iframe" with a generated URL from a JavaScript with URL parameters from the webpart page's URL.

First, create a webpart page, then insert a content editor webpart in it with the following code:
<script type="text/javascript">
    function getQueryStringRegExp(name) {
        var reg = new RegExp("(^|\\?|&)"
+ name + "=([^&]*)(\\s|&|$)", "i");
        if (reg.test(location.href))
return unescape(RegExp.$2.replace(/\+/g, " "));
        return "";
    }
    document.write("<iframe src='http://www.sitename.com/default.aspx?param1="+ getQueryStringRegExp('param1') + "' width=100% height=100%></iframe>");
</script>

The function getQueryStringRegExp is used to retrieve the parameter value from the page url.

Referece: