In my previous post “An important issue while using Master page: ID changed of the Controls” I have explained what problem a developer can face while using Master Page and JavaScript together. I have discussed the problem and shown a way to the solution.
I have used only one control to keep the example simple.
Recently some of my blog readers have asked me about how they can use multiple controls while adding JavaScript at server side along with Master Pages. Well guys, today I will explain the process of using multiple controls by an example. So let’s start!!!
The first step is similar to my previous post. Create a new website in VS2005 and add a Master Page in it. Name the master page as “Site.master”. Change the ID of contentplaceholder to CPH. Now switch the VS 2005 to source view and you will probably see the following code;
| <%@ Master Language=”C#” AutoEventWireup=”true” CodeFile=”MasterPage.master.cs” Inherits=”MasterPage” %><!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” > |
Now add a new page to your website (Default.aspx) and select “Site.master” as it’s master page. Now add two textboxes in your “Default.aspx” page. Change the IDs of the textboxes to “txtName” and “txtAge” respectively.Add a button control to the page “Default.aspx” and change its ID to “btnShow”. Change the button text to “show”. Your code will look like the following;
| <%@ Page Language=”C#” MasterPageFile=”~/MasterPage.master” AutoEventWireup=”true” CodeFile=”Default3.aspx.cs” Inherits=”Default3″ Title=”Untitled Page” %><asp:Content ID=”Content1″ ContentPlaceHolderID=”CPH” Runat=”Server”>
<asp:TextBox ID=”txtName” runat=”server”></asp:TextBox> </asp:Content> |
Now add the following code segment in the in Page_Load event of Deafault.aspx.cs file.
| ContentPlaceHolder cp = (ContentPlaceHolder)Page.Master.FindControl(“CPH”); TextBox tbName = (TextBox)cp.FindControl(“txtName”); TextBox tbAge = (TextBox)cp.FindControl(“txtAge”); string script=string.Format(@”function IDIssue() {{ var txtName=document.getElementById(‘{0}’); var txtAge=document.getElementById(‘{1}’); alert(‘your name is ‘+ txtName.value+’ and age is ‘+txtAge.value); }}”,tbName.ClientID, tbAge.ClientID); ClientScript.RegisterClientScriptBlock(this.GetType(), “IDIssue”, script, true); |
If you run the website from VS2005 you will see two Textboxes on the page. If you enter your name on the first textbox and age in the second textbox, then click th show button, an JavaScript alert box will display a message containing your name and age. That’s it.
For three controls, you have to use following code segment in the Page_Load event replacing the “string script=string.format(…..);” section;
| string script=string.Format(@”function IDIssue() {{ var ctrl1=document.getElementById(‘{0}’); var ctrl2=document.getElementById(‘{1}’); var ctrl3=document.getElementById(‘{2}’); // add necessary implemenation here }}”, ctrl1.ClientID, ctrl2.ClientID, ctrl3.ClientID); |
We are assuming that we have three controls name ctrl1, ctrl2 and ctrl3.
Now, you may have thought what if we have more than three controls??
Well, if you notice the methods overloaded for string.Format(), you will see that there is method which accepts an array as arguments.
string string.Format(string format, params object[] args);
If you have to format a string which has more than three arguments, you can use the above function. Obviously, you can also use it for less than three arguments. If we have some controls such as ctrl1,ctrl2,ctrl3 ctr4 etc. then, the syntax is;
| string[] paramsStr=new string[4]; paramsStr[0]=ctrl1.ClientID; paramsStr[1]=ctrl2.ClientID; paramsStr[2]=ctrl3.ClientID; paramsStr[3]=ctrl4.ClientID;string script=string.Format(@”function IDIssue() {{ var ctrl1=document.getElementById(‘{0}’); var ctrl2=document.getElementById(‘{1}’); var ctrl3=document.getElementById(‘{2}’); var ctrl4=document.getElementById(‘{3}’); // add necessary implemenation here }}”, paramsStr) |
Hope this will work!!!