Thursday 13 March 2014

Access content page control from Master Page

How to access content page control from Master Page

You can do it using this.Master.FindControl
Label lblMaster = (Label)this.Master.FindControl("lblMasterMessage");

if (lblMaster != null)

{

lblMaster.Text = "Master Page Controltext from Content Page";
}

You can use above code in your defaut.aspx
Now to get controls in javascript
or.
document.getElementById('<%=yourcontrolid.clientid%>').value;
U can use This this.master.FindControl("Ur Controlid")
example
Image img = this.Master.FindControl("Image1") as Image;
            img.Visible = false;
 Access a control on the MasterPage using JavaScript
If you have a control on the MasterPage and need to access it using JavaScript, then here’s how to do so. In this sample, we will test the length of the TextBox to see if there are 5 or more letters. If the number of letters is less than 5, the form submit will be cancelled.
Add a TextBox and a Button control in the MasterPage (outside the ContentPlaceHolder) as shown below
<body>
    <form id="form1" runat="server">
    <div>
       <asp:Panel ID="panelMaster" GroupingText="MasterPage controls"runat="server">      
            <asp:TextBox ID="txtMaster" runat="server"></asp:TextBox>
            <asp:Button ID="btnMaster" runat="server" Text="Button"OnClientClick="return accessMasterControl();" />
            <br />
        </asp:Panel>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
       
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
In the <head> element of the MasterPage, add the following code:
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function accessMasterControl() {
            if (document.getElementById('<%=txtMaster.ClientID%>').value.length <= 5) {
                alert('Minimum 5 characters required')
                return false;
            }
            else { return true;}
        }
    </script>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
 Access a control on the Content Page from a MasterPage using JavaScript
If you have a control on the Content Page which has to be accessed in the MasterPage using JavaScript, then here’s how to do so:
On the Content Page, create a TextBox as shown below :
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Panel ID="panelContent" GroupingText="ContentPage Controls" runat="server">
        <asp:TextBox ID="txtContent" runat="server"></asp:TextBox>
    </asp:Panel>
</asp:Content>

Now access and populate the TextBox ‘txtContent’ from the MasterPage
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function accessControlContentPage() {
var txtCont = document.getElementById('<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("txtContent").ClientID %>');
    txtCont.value = "I got populated using Master Page";               
}
    </script>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
user before submitting the form on a MasterPage
In order to ask the user for a confirmation before submitting the form, you can either use code behind or can use simple mark up as shown below:
Declare a Button control that causes postback and write the following code in MasterPage.master.cs or .vb
C#
protected void Page_Load(object sender, EventArgs e)
{
    string myScript = @"<script type='text/javascript'>
    function askBeforeSubmit() {
    var msg = 'Are you sure?';
    return confirm(msg);
    }
    </script>";
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MakeSure", myScript);
    form1.Attributes.Add("onsubmit", "return askBeforeSubmit();");

}

No comments:

Post a Comment