Posts Tagged ‘programmatically’

Programmatically Submit Form on Enter Key in C#

By default, a majority of the pages and all the functions on a C# Web Application page are all contained within a single form element. Therefore, there may be multiple buttons or other postback controls within a single form. And this may be the reason why there isn’t by default a control which is automatically called when the web users hits the ENTER button.

To Assign a Default On a Standard C# Webpage

This is the default way and involves the name of the control. If you have a ASP Button called btnSearch, the code you would include in the Page_Load function.

this.Form.DefaultButton = “btnSearch”;

To Assign a Default On a Content Page (from a MasterPage) C# Webpage

If your Web application uses MasterPages and you want to assign a control to submit the form on the Enter key, you concept is the same as above, but you’ll need to pass the UniqueId of the control. Assume a control called btnSearch on a ContentPage, add the following to the Page_Load function.

this.Form.DefaultButton = btnSearch.UniqueId;

Some benefits to programmatically assigning the Enter key is your ability to change which Postback function to assign based on how the web user enters the page. Other than that, assigning the default submit control is a major convenience factor for web users.

References:
http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton.aspx

Javascript Confirm on ASP LinkButton

In your C# web application, a common task is adding items to a database. But often just as important is the ability to delete items! The first method to accomplish this goal is linking an event to a LinkButton. This event links to a method that deletes. However, because a delete operation is a times permanent change, it is a good idea to force a user to confirm the decision to delete an item. This is to protect against accidental clicks.

Javascript Confirm on LinkButton Screenshot

Javascript Confirm on LinkButton Screenshot

As shown in the screenshot above, when a user clicks on a LinkButton, a Javascript confirm dialog box will appear with two buttons. Clicking OK will tell the method to continue and fire the event while clicking Cancel will prevent the event for firing. In this example, our text shows as “Are you sure?“, which is completely customizable to any text.

Via the LinkButton’s Properties Window
This is a quick and easy change to do via the LinkButton’s Property Window, so here’s how to do it!

Javascript Confirm on LinkButton

Javascript Confirm on LinkButton

  1. Goto the LinkButton’s properties window.
  2. Find the OnClientClick attribute.
  3. Add the following: return confirm(‘Are you sure?’);

Programmatically
Even though changing this via the properties window is easy, there are still those who prefer to programmatically make the change. Here’s the code below for the example in our screenshot.

<asp:LinkButton ID="lbJavascriptConfirm" runat="server"
 onclientclick="return confirm('Are you sure?');">Delete</asp:LinkButton>

And now this is how to make the change both programmatically and via a LinkButton’s property window. Please remember that this method will require that the web user has Javascript enabled. Additionally, the example uses this in a LinkButton, but any ASP Web Control that has the OnClientClick property.

Loading URL in iframe Programmatically on Postback in C#

To programmatically setup and use an iframe in C#, copy the below iframe code snippet into your aspx page. You are free to add or edit any of the iframe attributes below, but note that the id and the runat attributes are required!

<iframe id="myIframe" runat="server" scrolling="auto">
</iframe >

Now in the aspx page’s codebehind, you can access the control as myIframe. To set a new URL in the iframe during postback, use the below snippet. The example below will dynamically load http://www.victorchen.info in the iframe window.

myIframe.Attributes["src"] = http://www.victorchen.info

You now you have a working iframe in your C# code and accessible in your codebehind.