Have a fun while searching

Thursday, July 7, 2011

Finding master page control dynamicaly

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AjaxControlToolkit;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Collections;
using System.Xml.Linq;

namespace System.Window
{
public class GetData
{
public void GetDateRecord(string str, Page Page)
{
string strpath = "Resourcefile/" + str;
string path = HttpContext.Current.Server.MapPath(strpath);
//Loading the XML file using the Load method of XElement.
XElement resourceElements = XElement.Load(path);
//LINQ to load the resource items
var demo = (from resElem in resourceElements.Elements("data")
select new
{
Key = resElem.Attribute("name").Value,
Value = HttpUtility.HtmlEncode(resElem.Element("value").Value),
Comment = resElem.Element("comment") != null ?
HttpUtility.HtmlEncode(resElem.Element("comment").Value) : string.Empty
});
Hashtable ht = new Hashtable();
foreach (var str1 in demo)
{
ht.Add(str1.Key, str1.Value);
}
if (Page.Master.Controls.Count > 0)
{
foreach (System.Web.UI.Control c in Page.Master.Controls)
{
newSearch(str, ht, c);
}
}
if (Page.Form.Controls.Count > 0)
{
foreach (System.Web.UI.Control c in Page.Controls)
{
newSearch(str, ht, c);
}
}
}
private static void newSearch(string str, Hashtable ht, System.Web.UI.Control ch)
{
foreach (System.Web.UI.Control childc in ch.Controls)
{
switch (childc.GetType().ToString())
{
case "System.Web.UI.WebControls.Label":
Label mylable = (Label)childc;
if (!string.IsNullOrEmpty(mylable.ID))
{
if (ht.Contains(childc.ID))
{
mylable.Text = HttpUtility.HtmlDecode(Convert.ToString(ht[childc.ID]));
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.HtmlControls.HtmlGenericControl":
HtmlGenericControl myHtmlGenericControl = (HtmlGenericControl)childc;
if (!string.IsNullOrEmpty(myHtmlGenericControl.ID))
{
if (ht.Contains(childc.ID))
{
myHtmlGenericControl.InnerHtml = HttpUtility.HtmlDecode(Convert.ToString(ht[childc.ID]));
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.WebControls.TextBox":
TextBox myTextBox = (TextBox)childc;
if (!string.IsNullOrEmpty(myTextBox.ID))
{
if (ht.Contains(childc.ID))
{
myTextBox.Text = Convert.ToString(ht[childc.ID]);
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.WebControls.Button":
Button myButton = (Button)childc;
if (!string.IsNullOrEmpty(myButton.ID))
{
if (ht.Contains(childc.ID))
{
myButton.Text = Convert.ToString(ht[childc.ID]);
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.WebControls.DropDownList":
DropDownList myDropDownList = (DropDownList)childc;
if (!string.IsNullOrEmpty(myDropDownList.ID))
{
if (ht.Contains(childc.ID))
{
myDropDownList.Items.Clear();
ListItem lst;
string[] str2 = Convert.ToString(ht[childc.ID]).Split(new string[] { "-_-" }, StringSplitOptions.None);
for (int i = 0; i < str2.Length; i++)
{
lst = new ListItem();
lst.Text = str2[i];
if (str2[++i] != null)
lst.Value = str2[i];
myDropDownList.Items.Add(lst);
}
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.HtmlControls.HtmlAnchor":
HtmlAnchor myHtmlAnchor = (HtmlAnchor)childc;
if (!string.IsNullOrEmpty(myHtmlAnchor.ID))
{
if (ht.Contains(childc.ID))
{
Label lbl = new Label();
lbl.Text = HttpUtility.HtmlDecode(Convert.ToString(ht[childc.ID]));
myHtmlAnchor.InnerHtml = lbl.Text;
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.HtmlControls.HtmlTitle":
HtmlTitle myHtmlTitle = (HtmlTitle)childc;
if (!string.IsNullOrEmpty(myHtmlTitle.ID))
{
if (ht.Contains(childc.ID))
{
myHtmlTitle.Text = Convert.ToString(ht[childc.ID]);
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.WebControls.HyperLink":
HyperLink myHyperLink = (HyperLink)childc;
if (!string.IsNullOrEmpty(myHyperLink.ID))
{
if (ht.Contains(childc.ID))
{
myHyperLink.Text = Convert.ToString(ht[childc.ID]);
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.WebControls.RadioButtonList":
RadioButtonList myRadioButtonList = (RadioButtonList)childc;
if (!string.IsNullOrEmpty(myRadioButtonList.ID))
{
if (ht.Contains(childc.ID))
{
myRadioButtonList.Items.Clear();
ListItem lst;
string[] str2 = Convert.ToString(ht[childc.ID]).Split(new string[] { "-_-" }, StringSplitOptions.None);
for (int i = 0; i < str2.Length; i++)
{
lst = new ListItem();
lst.Text = str2[i];
if (str2[++i] != null)
lst.Value = str2[i];
myRadioButtonList.Items.Add(lst);
}
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.WebControls.LinkButton":
LinkButton myLinkButton = (LinkButton)childc;
if (!string.IsNullOrEmpty(myLinkButton.ID))
{
if (ht.Contains(childc.ID))
{
myLinkButton.Text = Convert.ToString(ht[childc.ID]);
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.WebControls.CheckBoxList":
CheckBoxList myCheckBoxList = (CheckBoxList)childc;
if (!string.IsNullOrEmpty(myCheckBoxList.ID))
{
if (ht.Contains(childc.ID))
{
myCheckBoxList.Items.Clear();
ListItem lst;
string[] str3 = Convert.ToString(ht[childc.ID]).Split(new string[] { "-_-" }, StringSplitOptions.None);
for (int i = 0; i < str.Length; i++)
{
lst = new ListItem();
lst.Text = str3[i];
if (str3[++i] != null)
lst.Value = str3[i];
myCheckBoxList.Items.Add(lst);
}
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.WebControls.RadioButton":
RadioButton myRadioButton = (RadioButton)childc;
if (!string.IsNullOrEmpty(myRadioButton.ID))
{
if (ht.Contains(childc.ID))
{
string[] str4 = Convert.ToString(ht[childc.ID]).Split(new string[] { "-_-" }, StringSplitOptions.None);
for (int i = 0; i < str4.Length; i++)
{
myRadioButton.Text = str4[i];
}
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.WebControls.CheckBox":
CheckBox myCheckBox = (CheckBox)childc;
if (!string.IsNullOrEmpty(myCheckBox.ID))
{
if (ht.Contains(childc.ID))
{
string[] str5 = Convert.ToString(ht[childc.ID]).Split(new string[] { "-_-" }, StringSplitOptions.None);
for (int i = 0; i < str5.Length; i++)
{
myCheckBox.Text = str5[i];
}
}
}
BindAgain(str, ht, childc);
break;
case "AjaxControlToolkit.CollapsiblePanelExtender":
CollapsiblePanelExtender myCollapsiblePanelExtender = (CollapsiblePanelExtender)childc;
if (!string.IsNullOrEmpty(myCollapsiblePanelExtender.ID))
{
if (ht.Contains(childc.ID))
{
string[] str6 = Convert.ToString(ht[childc.ID]).Split(new string[] { "-_-" }, StringSplitOptions.None);
myCollapsiblePanelExtender.CollapsedText = Convert.ToString(str6[0]);
if (str6[1] != null)
myCollapsiblePanelExtender.ExpandedText = Convert.ToString(str6[1]);
}
}
BindAgain(str, ht, childc);
break;
case "AjaxControlToolkit.TabPanel":
TabPanel myTabPanel = (TabPanel)childc;
if (!string.IsNullOrEmpty(myTabPanel.ID))
{
if (ht.Contains(childc.ID))
{
myTabPanel.HeaderText = Convert.ToString(ht[childc.ID]);
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.WebControls.RequiredFieldValidator":
RequiredFieldValidator myRequiredFieldValidator = (RequiredFieldValidator)childc;
if (!string.IsNullOrEmpty(myRequiredFieldValidator.ID))
{
if (ht.Contains(childc.ID))
{
myRequiredFieldValidator.ErrorMessage = Convert.ToString(ht[childc.ID]);
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.WebControls.RegularExpressionValidator":
RegularExpressionValidator myRegularExpressionValidator = (RegularExpressionValidator)childc;
if (!string.IsNullOrEmpty(myRegularExpressionValidator.ID))
{
if (ht.Contains(childc.ID))
{
myRegularExpressionValidator.ErrorMessage = Convert.ToString(ht[childc.ID]);
}
}
BindAgain(str, ht, childc);
break;
case "System.Web.UI.WebControls.Panel":
case "AjaxControlToolkit.TabContainer":
case "System.Web.UI.Control":
case "System.Web.UI.UpdatePanel":
case "System.Web.UI.WebControls.Repeater":
case "System.Web.UI.WebControls.RepeaterItem":
case "System.Web.UI.WebControls.DataGrid":
case "System.Web.UI.WebControls.DataList":
case "System.Web.UI.WebControls.DataListItem":
case "System.Web.UI.WebControls.GridView":
case "System.Web.UI.WebControls.ListView":
case "System.Web.UI.HtmlControls.HtmlTableCell":
case "System.Web.UI.DataBoundLiteralControl":
case "System.Web.UI.HtmlControls.HtmlTable":
case "System.Web.UI.HtmlControls.HtmlTableRow":
case "System.Web.UI.WebControls.ChildTable":
case "System.Web.UI.WebControls.GridViewRow":
case "System.Web.UI.WebControls.DataControlFieldHeaderCell":
case "System.Web.UI.WebControls.DataControlFieldCell":
case "System.Web.UI.WebControls.ContentPlaceHolder":
case "System.Web.UI.HtmlControls.HtmlLink":
case "System.Web.UI.UserControl":
case "System.Web.UI.MasterPage":
case "System.Web.UI.ControlCollection":
case "System.Web.UI.ScriptManager":
case "System.Web.UI.LiteralControl":
BindAgain(str, ht, childc);
break;
default:
break;
}
}
}
private static void BindAgain(string str, Hashtable ht, System.Web.UI.Control childc)
{
if (childc.Controls.Count > 0)
{
newSearch(str, ht, childc);
}
}
}
}

0 comments:

Post a Comment