:: Home     :: MS Dynamics CRM     :: .Net 1.1     :: .Net 2.0     :: Sharepoint Portal     :: Ajax

  login:        
  passwords:  
 

Asp.Net Interview Questions
Asp.Net 2.0/3.5 Sample Interview Questions and Answers. Interview Tips for a successful job Interview


 

Code Walkthroughs

Datagrid Formatting the Data
We are able to format the content of the datagrid cell by one of two simple methods, dependant upon whether the column is a bound column or whether it is a template column. In our example we shall format the column to have to digits after the decimal point , followed by a...

Datagrid Highlight a Row With Click Through
It is relatively easy to add alternating colours to the rows in your datagrid. However, when we move the mouse over the rows we may want to highlight this row, and possibly to add the option of a click through based on the row selected...

Add a Delete Button to a Datagrid
To add a delete button to a datagrid follows a similar process to adding an edit button. In the datagrid header...

Add an Edit Button to a Datagrid
The datagrid has a predefined editColumn for handling the editing of a datagrid. Adding this simple column definition to a datagrid adds a powerful feature. When a row is not in edit mode the column item shows the word...

Making a Datagrid Row Editable
Two of the most popular methods of editing a datagrid in asp.net are to either select the row and take the user off to a different presentation of the data, or to change the formatting of the row presented in the database with appropriate edit text boxes, checkboxes and...

Adding Tooltips to Datagrid Rows
Adding tooltips to datagrid rows is easy, assuming that you have already created the code for adding row highlighting. In this article I shall assume that you have already read the article entitled Datagrid Highlight a Row With Click Through...

Binding a Datagrid to an Access Database
This list covers the full lifecycle of a content management system, from initially creating the content, through to delivering it to end users...

Adding Data to a DropDownList
The aim of this article is to answer the question 'How do I add items to a DropDownList?' Initially as part of the declaration for the DropDownList we can also define a number of items, much in the same way as in classic ASP...

Getting Current Date Time
In classic ASP we had now() which would return the current date and time. For asp.net this no longer exists. So what should we use...

Test if File Exists
Sometimes, in order to reduce our chance of error, when working with the filesystem in ASP.NET, we need to determine wether a file exists before performing an action on it. The following short piece of code will enable us to test whether a file exists...

Using Javascript with ASP.NET Form Elements
Adding simple pieces of Javascript to an Asp.net page can be acheived by adding to the attributes of the particular imagebutton or linkbutton. if its normal ASP.Net Button then you can...

Regular Expressions
In the table below we list the characters used in .Net regular expressions, together with their meaning, But first...

Authentication in Asp.net
Forms authentication in ASP.Net is far more easier and safe than Asp 3. It is possible to place a web.config file in any directory of a web site.Therefore, we are able to make most of a web site public, whilst providing authentication on, say, one directory...

Discussion Forums

General ASP.NET

.Net Programming

cSharp Home

Sql Server Home

Javascript / Client Side Development

IT Jobs

Ajax Programming

Ruby on Rails Development

Perl Programming

C Programming Language

C++ Programming

Python Programming Language

Laptop Suggestions?

TCL Scripting

Fortran Programming

Scheme Programming

29. Dropdownlist

FAQ Home
   29.1 I have 3 DropDownLists on the page : one for a Month, second one for a Day, and the last one for a Year. How can I let the user choose the data and enter it to 'datetime' column in the database.
   29.2 How to apply css style to each item of the dropdownlist or listbox control?
   29.3 How to concate and display 2 fields in a dropdownlist?
   29.4 I have a page with a Dropdownlist and some simple code for the SelectedIndexChanged but it does not do a postback?
   29.5 How to add an extra item to the DropDownList filled with data from a database?
   29.6 Can I use a dropdownlist to allow user to select multiple items from a list of available items?
   29.7 Why do I get System.Data.DataRowView/System.Data.Common.DbDataRecord as the item in a dropdownlist?
   29.8 How can I display a Tool tip for a dropdownlist?
   29.9 How to select a specific Item in a DropDownList?
   29.10 How to display multiple spaces in a dropdownlist?



29.1 I have 3 DropDownLists on the page : one for a Month, second one for a Day, and the last one for a Year. How can I let the user choose the data and enter it to 'datetime' column in the database.


VB.NET


Dim dt as DateTime = DateTime.Parse(ddlMonth.SelectedItem.Text & "/" & ddlDay.SelectedItem.Text & "/" & ddlYear.SelectedItem.Text )


C#


DateTime dt = DateTime.Parse(ddlMonth.SelectedItem.Text & "/" & ddlDay.SelectedItem.Text & "/" & ddlYear.SelectedItem.Text );



29.2 How to apply css style to each item of the dropdownlist or listbox control?


The dropDownList has a bug which prevent us from assigning the style property to each item in the DropDownList. This bug confirmed by Microsoft in Microsoft Knowledge Base Article - 309338 For the workaround use a HTML dropdownlist with runat=server tag


<SELECT id="DropDownList1" runat="server" >
</SELECT>


Use namespace System.Reflection VB.NET


If Not Page.IsPostBack Then
Dim col As FieldInfo
For Each col In GetType(KnownColor).GetFields
     If col.FieldType Is GetType(Drawing.KnownColor) Then
          DropDownList1.Items.Add(New ListItem(col.Name, col.Name))
     End If
Next
End If

Dim i As Integer
For i = 0 To DropDownList1.Items.Count - 1
     DropDownList1.Items(i).Attributes.Add("style", "background-color:" + DropDownList1.Items(i).Text)
Next



C#


if (!IsPostBack)
{
     foreach(FieldInfo col in typeof(KnownColor).GetFields() )
     {
          if (col.FieldType == typeof(KnownColor) )
          {
          DropDownList1.Items.Add(new ListItem(col.Name ,col.Name));
          }
     }
}
for (int i= 0 ;i < DropDownList1.Items.Count;i++)
{
     DropDownList1.Items[i].Attributes.Add("style", "background-color:" + DropDownList1.Items[i].Text);
}



29.3 How to concate and display 2 fields in a dropdownlist?


Method1


"Select FirstName + ' ' + LastName as fullname from Employees"


then use 'fullname' as the datatextfield for the dropdownlist
Method2


Dim newColumn As New DataColumn()
With newColumn
     .ColumnName = "fullname"
     .DataType = System.Type.GetType("System.String")
     .Expression = "LastName+' '+FirstName"
End With
ds.Tables(0).Columns.Add(newColumn)
ddlName.DataTextField = "FullName"
ddlName.DataValueField = "employeeid"
ddlName.DataSource = ds.Tables(0).DefaultView
ddlName.DataBind()


C#


DataColumn newColumn =new DataColumn();
newColumn.ColumnName = "fullname";
newColumn.DataType = System.Type.GetType("System.String");
newColumn.Expression = "LastName+' '+FirstName";
ds.Tables[0].Columns.Add(newColumn);
DropDownList1.DataTextField = "FullName";
DropDownList1.DataValueField = "employeeid";
DropDownList1.DataSource = ds.Tables[0].DefaultView;
DropDownList1.DataBind();



29.4 I have a page with a Dropdownlist and some simple code for the SelectedIndexChanged but it does not do a postback?


If you want the dropdownlist to cause a postback when selection changes set AutoPostBack property to "True" for the dropdownlist control.


29.5 How to add an extra item to the DropDownList filled with data from a database?


VB.NET


'DropDownList1.Items.Insert(0, "Please Select")
DropDownList1.Items.Insert(0, new ListItem("<text>", "<value>"))


C#


//DropDownList1.Items.Insert(0, "Please Select");
DropDownList1.Items.Insert(0, new ListItem("<text>", "<value>"));


This should be done after .DataBind of dropdownlist


29.6 Can I use a dropdownlist to allow user to select multiple items from a list of available items?


No. Dropdownlist Web Control allows user to select only one item from a list of items.


29.7 Why do I get System.Data.DataRowView/System.Data.Common.DbDataRecord as the item in a dropdownlist?


This is probably because you have not set the DataTextField/ DatavalueField property of the dropdownlist.
VB.NET


'..
DropDownList1.DataSource = ds.Tables(0)
DropDownList1.DataTextField = "CategoryName"
DropDownList1.DataValueField = "CategoryId"
DropDownList1.DataBind()


C#


//..
DropDownList1.DataSource =ds.Tables[0];
DropDownList1.DataTextField = "CategoryName";
DropDownList1.DataValueField = "CategoryId";
DropDownList1.DataBind();



29.8 How can I display a Tool tip for a dropdownlist?


The ToolTip property is inherited from the WebControl class and is not applicable to the DropDownList control. This implementation of the ToolTip property does not allow you to set a value and returns String. In a word, if we can make this (a dropdown with ToolTip) work with plain HTML and client side script, we should be able to emit the necessary code from ASP.NET.

More Details at DropDownList.ToolTip Property


29.9 How to select a specific Item in a DropDownList?


Method 1: VB.NET


DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(<value>))
'DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(<Text>))


C#


DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(<value>));
//DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(<Text>));


Method 2: VB.NET


DropDownList1.Items.FindByText("<Text>").Selected = true
'DropDownList1.Items.FindByValue("<value>").Selected = true


C#


DropDownList1.Items.FindByText("<Text>").Selected = true ;
//DropDownList1.Items.FindByValue("<value>").Selected = true ;



29.10 How to display multiple spaces in a dropdownlist?



<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>


VB.NET


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     'Put user code to initialize the page here
     Dim i As Integer
     For i = 0 To 10
          DropDownList1.Items.Add("Item Number" + SpaceDDL(10) + i.ToString)
     Next
End Sub

Private Function SpaceDDL(ByVal numberOfSpaces As Integer) As String
     Dim Spaces As String
     Dim i As Integer
     For i = 0 To numberOfSpaces
      Spaces &= " "
     Next
     Return Server.HtmlDecode(Spaces)
End Function


C#


private void Page_Load(object sender, System.EventArgs e)
{
     // Put user code to initialize the page here
     for(int i = 0 ;i<=10;i++)
     {
          DropDownList1.Items.Add("Item Number" + SpaceDDL(10) + i.ToString());
     }
}

string SpaceDDL(int numberOfSpaces )
{
     string Spaces="";
     for(int i = 0 ;i<=numberOfSpaces;i++)
     {
          Spaces += " ";
     }
     return Server.HtmlDecode(Spaces);
}
 
Copyright 2005-2015, Megasolutions Ltd  |   Privacy Policy