Tag Archive for 'asp.net'

How to set the DefaultButton in a Page Based on ASP.NET Master Page

Challenges:

Asp.NETAs I mentioned in my previous post, I fixed the issue of hitting enter key on an ASP.NET page with a single textbox and submit button. But now new issue occurred: I could not use the Enter key on other button web controls. Whenever I hit the Enter key in the web form, the onClick event of the search button fires not the other submit buttons. I know I am Master page and the search button has been included in every page based on this master page, and the search button is in the first button web control position.

Trouble Shootings:

OK, the first thing came to my mind to resolve the issue above was to use the  new DefaultButton properties in ASP.NET 2.0 form or panel controls. But since I am using Masterpage, when I put defaultButton=”btnSubmit” in master page or put the following code in my Page_load event

Page.Form.DefaultButton = “btnSubmit”

I got the following error:

Server Error in ‘/MyApplication’ Application.
The DefaultButton of ‘form1′ must be the ID of a control of type IButtonControl.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The DefaultButton of ‘form1′ must be the ID of a control of type IButtonControl.

Continue reading ‘How to set the DefaultButton in a Page Based on ASP.NET Master Page’

Related posts

How to fire onClick event with hitting Enter in ASP.NET forms

Challenge:

Asp.NETI have a search box in every asp.net web page with one text box and one submit button. When the visitor clicks the submit button, he will be re-directed to a search result web page since the onClick event is fired on that submit button. But when the visitor finishes his typing and simply hits the enter key, a postback event will be fired on the page level but the onClick event of that submit button could be not fired.

Solutions

After reviewing the HTML codes and HTTP requests, I found out the pair of button name/value has not been sent to the form along with the text box. That was the reason why the page could not decide whether the submit button triggered then not fired the onClick event.

I am using ASP.NET 2.0 and experiencing such issue, and not sure whether ASP.NET 3.5 still have the same issue. But a simple work-around can fix this issue - which is to add one more textbox web control.

For example, if you only have one textbox and submit button, the query string post back to the server will be

default.aspx?txtBOX1=some+value&

But if you have more than one text boxes in your asp.net page, then your string will be

default.aspx?txtBOX1=some+value&txtBOX2=some+value…btnSubmit=Search…

Of course, you do not want the visitors to see your addtional textbox, you should use a style to hide it. Do not use visibility property since that will disable that textbox web control. So your new textbox will look like the below

<asp:TextBox ID=”txtInvisible” runat=”server” Style=”visibility: hidden; display: none;” />

In this way, when the visitor express the Enter key, your first button web control will be fired.

[update: if you have multiple button web controls and want to control which one to be fired while the visitor hits the Enter key, please read on my next post]

Related posts

How easily to create a required validator requiredfieldvalidator for CheckBox control in asp.net 2.0

Challenge:

Asp.NET I tried to add a CheckBox control to the registration page in my newly implemented open source YetAnotherForum asp.net application. And this CheckBox is for Terms and Conditions agreement, so it needs to be checked before the users submit registration form. It looked like the easy way was to use a RequiredFieldValidator to validate the CheckBox control. But unfortunately this RequiredFieldValidator validation Web control does not work with the CheckBox (or CheckBoxList) Web controls. After I set its ControlToValidate property to the ID of the CheckBox, the page threw an HttpException, saying:

"Control ‘controlID’ referenced by the ControlToValidate property of ‘validationControlID’ cannot be validated." 

Ok, I can perform the validation check in the postback code or replace the RequiredFieldValidator with CustomValidator control. But this is for a constantly updated open source application, I did not want to change too much source code in case I will get overwritten in the next release. So, I need a quick and dirty way to do this.

Continue reading ‘How easily to create a required validator requiredfieldvalidator for CheckBox control in asp.net 2.0′

Related posts

How to conditionally display image in Gridview based on data fields in ASP.NET 2.0

Challenge: Asp.NETHow to display an image in a column of ASP.NET Gridview based on the data from a database table. For example, I have a GridView to display some speakers’ profile with name, address etc. Within it, I need to display a clip icon in the column of “Speech” to indicate that this speaker has a speech document attached. Continue reading ‘How to conditionally display image in Gridview based on data fields in ASP.NET 2.0′

Related posts

How to fix issues of sending email in ASP.net 2.0 with localhost Microsoft SMTP service

Asp.NETWith the upgrading to ASP.net 2.0, I decided to change all my sending email codes too. Basically I am going to use system.net.mail instead of system.web.mail or traditional CDONT. I thought it would be an easy task, but I still encountered some issues.

Continue reading ‘How to fix issues of sending email in ASP.net 2.0 with localhost Microsoft SMTP service’

Related posts

How to display and edit multiline in datagrid controls with Databinding

Asp.NETI have a gridview web control in one asp.net 2.0 page which has a field with mutliline texts need to be displayed and updated. I am using SQLDatasource to retrieve the data from my database and then use databinding to this gridview. When the mutliline texts showed on the gridview, all line breaks were gone. For example, the original texts came from the user in the database were: Continue reading ‘How to display and edit multiline in datagrid controls with Databinding’

Related posts

How to Append Master Page title to Content Page title in ASP.NET 2.0

Usually asp.net render the content page title first, but you can append the page title in Page_Load event.

Simply put the following code in your master page’s Page_load event

Page.Header.Title &= “Whatever Constant String you want”

Related posts

Errors occurred when I was upgrading to VS 2005/ASP.NET 2.0

Today finally I got the Visual Studio 2005 installed on my new computer, and tried to move all visual video project from my previous VS 2003 to it. After I open the previous project in VS 2005 and converted and then re-built, then I tried to open the application in the browser. But I got the following error message:

Failed to access IIS metabase.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.Hosting.HostingEnvironmentException: Failed to access IIS metabase.

Continue reading ‘Errors occurred when I was upgrading to VS 2005/ASP.NET 2.0′

Related posts