An array of C#, PHP, and HTML programming articles, tutorials, and resources

Posts Tagged ‘ control ’

Until recently, I had always used javascript to allow the enter key to submit a form (as seen in a previous posting called Submit Form On Enter Key Solution). Since then, I was queued into a GUImethod of controlling enter key form submissions in Visual Studio 2008 (and I’m sure this works in other versions as well).

How to submit a form in Visual Studio 2008To do this in the GUI style, click on the form of the page and then select the elements properties. Under the ASP.NET subheading, there is a form property called DefaultButton. Insert the button control id of the default button. This will essentially submit the form on enter (by performing the action on the specified button). The image to the left is an example of the form property section with a default button set to btnSetCookie. Note that for one reason or another, intellisense is not enabled on this property and you will have to manually type in the button control id.

Of course, you are not constrained to doing this in the GUI style. To submit a form on enter programmatically, simply find the form and insert the keyword defaultbutton=”BUTTON_CONTROL_ID_HERE”. Below is an example of how the above example would look programmatically.

<form id="frmTestForm" runat="server" defaultbutton="btnSetCookie">


		

When you use a content page based on a previously created master page, there is sometimes a want to access attributes of control defined in the master page, such as a label, hyperlink, or dropdownlist. By default, there is no way to easy way to access the those controls. However, using the FindControl function will allow access to those controls. First part of the example is the basic master page code:

<html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
      <title>Accessing Master Page Controls</title>
      <asp:ContentPlaceHolder ID="head" runat="server">
      </asp:ContentPlaceHolder>
   </head>
   <body>
      <form id="form1" runat="server">
         <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
            <asp:Label ID="lblUserName" runat="server" Text="Victor Chen"></asp:Label>
         </div>
      </form>
   </body>
</html>

Next, is a relevant snippet from the content page’s code behind file that will access a label defined earlier in the master page.

public partial class WebForm2 : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      String username = ((Label)Master.FindControl("lblUserName")).Text;
   }
}

The Master property listed in red will access the master page as defined in the <%@ Page MasterPageFile> property defined in the content page.

The FindControl function of the Master property takes one string input. The string input is the id of the control defined in the master page. In this case, the label id in the master page is defined as lblUserName.

Finally, the FindControl function returns an object, so we must cast the object back to a Label. Once back to a label, the text of the label can be extracted by accessing the Text property.