Category Archives: Asp.net User Control

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

.

HyperLink Control

We can use HyperLink control when we need to provide navigation to other page. For only navigation specially when you don’t need any server side processing to do we can prefer HyperLink control over LinkButton.

<asp:HyperLink ID="hlNewDate" runat="server" ToolTip="[#]Click to see news" Text=’<%# Eval("DateHeader") %>’ Target="_blank"
NavigateUrl=’<%# Eval("LinkUrl") %>’ CssClass="nyHeaderFont"></asp:HyperLink>

 

So, now you can navigate directly with also the Target property. Which has some enumeration options:

_blank

Renders the content in a new window without frames.

_parent

Renders the content in the immediate frameset parent.

_search

Renders the content in the search pane.

_self

Renders the content in the frame with focus.

_top

Renders the content in the full window without frames.

But in case If we use LinkButton

<asp:LinkButton ID="lnkNewsDate" runat="server" ToolTip="[#]Click to see news" Text=’<%# Eval("DateHeader") %>’
                                      PostBackUrl=’<%# Eval("LinkUrl") %>’  CssClass="nyHeaderFont"></asp:LinkButton>

As LinkButton doesn’t have any property as Target and NavigateUrl.

Here it will directly postback to the page so you can’t have an option to open a new window and keep you current page open. Yet, you can do so by the writing JavaScript even code for "onClientClick.

Follow

Get every new post delivered to your Inbox.

Join 54 other followers