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.

Wednesday, February 27, 2008

Simple and Complex Drilldown for ASP.Net Objects

It can be difficult to drill down through an ASPX page to see where your desired object is before you call FindControl. One quick way to figure out the control tree structure is the Document Outline tab of Microsoft Visual Studio. The only caveat is that you will still need to know your Master Page structure.

The complex overkill way is to use Trace="true" under the Page Directive (at the top of the aspx page.) This has other applications as well. For example, instead of debug prints you can see the state of many variables.

Web User Control Library error

When you create a User Control from a class and try to drag and drop the control into the design page, you may get an Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC)) error. Talk about confusing. My solution was to make my (user control) class public.

Thursday, February 21, 2008

Prevent RadioButtonList from forcing a line break

Prevent RadioButtonList from forcing a line break by placing the control in a span and set the RepeatLayout attribute to Flow.

Reference: http://www.webdeveloper.com/forum/showthread.php?t=138562

Tuesday, February 19, 2008

Using Web.config hash for variables using add, key, and value

Use configuration (web.config) hash to set variables. First, there is the web.config file section: ...

<configuration>
<appSettings>
<add key="fooLink" value="http://www.foo.htm"/>
</appSettings>
</configuration>
Now in the code-behind I can set a Hyperlink variable as follows:

HyperLinkFoo.NavigateUrl = ConfigurationManager.AppSettings["fooLink"].ToString().ToLower();

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);

}
}

Intercepting return in list menu items in ASP.NET

Here is how I was able to intercept the return button in my list menu item when selected, despite a default button on the aspx page:

<li><a href="" onkeydown="if (event.keyCode==13) { window.location.href='http://www.foo.com';}"</a></li>

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();
}