Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Showing posts with label C# MasterPage. Show all posts
Showing posts with label C# MasterPage. Show all posts

Monday, February 18, 2008

Drilling down a nested master page in ASP.NET to reset a form

Here is an example of drilling down a master page in ASP.NET to reset a form's TextBox controls:

protected void btnqReset_Click(object sender, EventArgs e)
{

try
{
ContentPlaceHolder childMasterContent = (ContentPlaceHolder)Page.Form.FindControl("ParentMasterContent").FindControl("ChildMasterContent");
Control ctrlForm = childMasterContent.FindControl("upnlQueryMoreCriteria").FindControl("pnlQueryMoreCriteria");
Control[] queryControls = { ctrlForm };
for (int i = 0; i < 1; i++)
{
if (queryControls[i].Controls != null)
{
foreach (Control ct in queryControls[i].Controls)
{
if (ct.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{
((TextBox)ct).Text = "";
}
}
}
}
}
catch (Exception ex)
{
log.Error("Unable to reset query criteria: " + ex.Message + ex.StackTrace);

}
}

Saturday, February 16, 2008

FindContol Solution to add Javascript to a control from a user control

Here is a great solution to finding a control using FindControl method from a user control. TargetcontrolID is the public property for TextBoxFooExtender (User Control). _txt is the targeted TextBox assuming it is found.

protected override void CreateChildControls()
{
txt = (TextBox)this.Parent.FindControl(TargetControlID);
if (txt != null) {
_txt.Attributes.Add("ondblclick", "alert('foo')");
}
base.CreateChildControls();
}

Thursday, November 29, 2007

Implementing Dynamic Hyperlink of Current Page

Here's how to implement a dynamic HyperLink of the current page based on the current pages title and current URL without the query string parameters. This is located in the Master Page.


protected void Page_Load(object sender, EventArgs e)
{
hlPageTitle.Text = Page.Title.ToString();
// old way would fetch query string paramaters
//hlPageTitle.NavigateUrl = Request.Url.AbsoluteUri.ToString();
hlPageTitle.NavigateUrl = Request.Url.GetLeftPart(UriPartial.Path);
}
Reference link.

Monday, November 12, 2007

Modifying labels in MasterPage From MasterPage user Page

First, here is how to modify a label in the MasterPage from the MasterPage user Page:


Label mpLabel = (Label)Master.FindControl("lblPageTitle");
if (mpLabel != null)
{
mpLabel.Text = this.Title.ToString();
}

link