Category Archives: C#

Ensure a button is not a default button: A Case Study

To make a button default button we can use the following C# code.

Page.Form.DefaultFocus = txtCprNumber.ClientID;//Setting default focus element
Page.Form.DefaultButton = btnSearch.UniqueID;// Setting default button

image

But let’s think that we have a logout button on top of the page in control and placed on top of every page.

So what happening:

1) For the browser’s default behavior the "logout" button are getting default focus.

2) When user pressing enter to a textField the logout button is getting clicked because of the default browser behavior and actually the user is unaware of that.

Lets try some solution:

1) The solution would be for every page make a button of that page as default button. But that very costly.

2) Making sure the "Logout" button is not in focus, hmm an centralized solution.

But, to move a focus you have to find next focus element. But I may not know what are the underlying element in a page. If such a javascript solution would be good for it.

 

BUT, I found a straight forward solution by just adding an ASP button property. i.e. UseSubmitBehavior and set it to true.

 

Now the logout button is no longer in the foucs.

 

 <asp:Button ID="btnLogout" runat="server" Text="Logout" CssClass="logoutButton"
                OnClick="btnLogout_Click"  UseSubmitBehavior="false" />

OK. Its works for every browser!!!

Working with Dropdownlist

It quite easy to work with dropdown list. You just need to drag-and drop it. But let just work with it.

Suppose, we want to add an datasource which have multiple fields, or coming from database SELECT with multiple fields.

Lets we have a List<User> from Facebook which have properties but we want to see the Name as dropdown text which is in the User object written as “name” and set the value to UserId which is written as “uid”.

image

Now what we could do is just set the datasource to the List<User> and then specify the “DataTextField” and “DataValueField”.

IList<user> friendList = Master.Api.Friends.GetUserObjects();           
          ddlFriends.DataSource = friendList;
          ddlFriends.DataTextField = "name";
          ddlFriends.DataValueField = "uid";
          ddlFriends.DataBind();

image

Ok, Also if you want to make the text color different in every row or change the row background you can use the following code section: after the “DATABIND()” method

For text only:

ddlFriends.Items[count].Attributes.Add("style", "color:#FC8105");

For Background:

ddlFriends.Items[count].Attributes.Add("style", "background-color:#51D9E8");

.

Starting Development in .NET ( ASP.NET, C#)

I will start writing from the beginning for those who now want to start to migrate in ASP.net and C# and also to start as a programmer.
In this post i will write about setting the asp development environment.
Primarily, We will need two things though we can install some other optional>>
1) As asp is a server side language we will need a local server. Like as we use APACHE for PHP.
So, We have to install IIS (Internet Information Service). To install IIS we will need Windows CD.
And Do the following steps:
Go to Control Panel>>AddRemoveComponent>>Install/Remove Windows Component
Now Select IIS in the list of CheckBox and Click OK.
2) Once we have IIS installed we can now go for a IDE. Microsoft suggest for Visual Studio for ASP development.
There is various distribution of VS. like: VS Team System 2008, VS 2008, VS2005, Or VS Express web development.
you can buy any CD of them and get Installed. Though i will suggest you to install any distribution of VS 2008 as it is the most upgraded version and also by default you can get feature of .NET 3.5 platform like AJAX support, automatic web.config edition. We will discuss about .NET Platform and others in subsequent posts.
3) Installing VS 2008 will automatically insall MS Server 2005 Express Edition (Database Server) and by this you can make DB and Query as we did in oracle by writing SQL. However if you want to do things in Graphical manner (like we do in MYSQL) you have to get another tool named "Management Studio 2005 express Editon". You can download it form Microsoft SQL Server site.
4) From VS 2008/2005 CD you will get MSDN(Developer Network: Documentation of ASP/C# and other .NET language). I will suggest you to get MSDN install for you first-hand helping tool.
This steps will make our Development environment ready for ASP.net Development.
Please let me know if you need any other information regarding this post.
Follow

Get every new post delivered to your Inbox.

Join 54 other followers