The DropDownExtender is an ASP.NET AJAX control that can connect to almost any ASP.NET control to provide a dropdown menu. For example: If you have a TextBox and a Panel control
(with a CheckBoxList), and wants to give the text box drop-down menu, then it applies only to the DropDownExtender the text box and set the DropDownControlID the Group. In this article we will see some tips and tricks that can be applied to a control DropDownExtender.
I suppose you have some basic experience developing ASP.NET Ajax applications and has installed the ASP.NET AJAX Library and ASP.NET Control Toolkit. As of this writing, the version of the toolkit is version 1.0.20229 (if you are targeting Framework 2.0, ASP.NET AJAX 1.0 and Visual Studio 2005) and Version 3.0.20229 (if targeting. NET Framework 3.5 and Visual Studio 2008).
Tip 1: How to attach a DropDownExtender to a TextBox and show a Panel in the dropdown
Drag and drop a ScriptManager to the page. Then drag and drop a Panel (panelItems) to the page. Now add a BulletedList to it and add some ListItems.
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- </asp:ScriptManager>
- <asp:Panel ID="panelItems" runat="server" BorderColor="Aqua" BorderWidth="1">
- <asp:BulletedList ID="BulletedList1" runat="server">
- <asp:ListItem Text="Item 1"/>
- <asp:ListItem Text="Item 2"/>
- <asp:ListItem Text="Item 3"/>
- </asp:BulletedList>
- </asp:Panel>
Now drag and drop a TextBox control outside the Panel. If you want to attach a DropDownExtender to the TextBox, then in the design mode, click on the smart tag of the TextBox > Add Extender > Choose the ‘DropDownExtender’ > OK.
If you go back to the ‘Source’ view, you will find the newly created DropDownExtender. Set the DropDownControlId to panelItems.
- <cc1:DropDownExtender ID="TextBox1_DropDownExtender" runat="server"
- DynamicServicePath="" Enabled="True" DropDownControlID="panelItems" TargetControlID="TextBox1">
- </cc1:DropDownExtender>
The entire code will look similar to the following.
- <body>
- <form id="form2" runat="server">
- <div>
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- </asp:ScriptManager>
- <asp:Panel ID="panelItems" runat="server" BorderColor="Aqua" BorderWidth="1">
- <asp:BulletedList ID="BulletedList1" runat="server">
- <asp:ListItem Text="Item 1"/>
- <asp:ListItem Text="Item 2"/>
- <asp:ListItem Text="Item 3"/>
- </asp:BulletedList>
- </asp:Panel>
- <br />
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <cc1:DropDownExtender ID="TextBox1_DropDownExtender" runat="server"
- DynamicServicePath="" Enabled="True" DropDownControlID="panelItems" TargetControlID="TextBox1">
- </cc1:DropDownExtender>
- </div>
- </form>
- </body>
- </html>
Run the code. When you hover over the TextBox, you will see a dropdown. Click on it to view the Panel with the BulletedList as shown below
Tip 2: How to keep the DropDownExtender always visible
The dropdownlist as demonstrated above is visible only when the user hovers over the textbox. If you want to override this behavior and keep it always visible, then here is a trick I got from this post Add the following script tag in the <body> element as shown below:
- <body>
- <form id="form2" runat="server">
- <script type="text/javascript">
- function pageLoad()
- {
- $find('TextBox1_DropDownExtender')._dropWrapperHoverBehavior_onhover();
- $find('TextBox1_DropDownExtender').unhover = VisibleMe;
- }
- function VisibleMe()
- {
- $find('TextBox1_DropDownExtender')._dropWrapperHoverBehavior_onhover();
- }
- </script>
- ... ScriptManager and other Controls come here
- </body>
- </html>
If you run the code, you will see that the dropdown is always visible.
Tip 3: How to keep the DropDown expanded and show the Panel when first loaded
The default behavior is that the panel is shown only once the user clicks on the textbox. If you want the Panel to be visible as a dropdown when the page is loaded, add the following script
Tip 4: How to align DropDownExtender panel to left instead of right
By default, the panel drops down to the right. However if you need to change this behavior, use the following script. The script uses the set_positioningMode() and sets it to 2 (left). The default value is 3 (right).
- <body>
- <form id="form3" runat="server">
- <script type="text/javascript">
- function pageLoad()
- {
- var chngPosition = $find('TextBox1_DropDownExtender')._dropPopupPopupBehavior;
- chngPosition.set_positioningMode(2);
- }
- </script>
- ... ScriptManager and other Controls come here
- </body>
- </html>
Tip 5: How can I make the width of the DropDownExtender (panel) the same as that of the TextBox
The width of the panel that is visible during the drop-down is not the same as that of the targetcontrol of the DropDownExtender. It is displayed to the right by default and adjusts its width according to the contents of the panel as shown below:
However, if you want to manually adjust the width of the drop down and make it the same as that of the textbox, use this code:
- <body>
- <form id="form3" runat="server">
- <script type="text/javascript">
- function pageLoad()
- {
- $get('panelItems').style.width = $get('TextBox1').clientWidth;
- }
- ... ScriptManager and other Controls come here
- </body>
- </html>
Create data table in ASP.NET 2.0(C#)
- private DataTable CreateDataTable()
- {
- DataTable myDataTable = new DataTable();
- DataColumn myDataColumn;
- myDataColumn = new DataColumn();
- myDataColumn.DataType = Type.GetType("System.String");
- myDataColumn.ColumnName = "id";
- myDataTable.Columns.Add(myDataColumn);
- myDataColumn = new DataColumn();
- myDataColumn.DataType = Type.GetType("System.String");
- myDataColumn.ColumnName = "username";
- myDataTable.Columns.Add(myDataColumn);
- myDataColumn = new DataColumn();
- myDataColumn.DataType = Type.GetType("System.String");
- myDataColumn.ColumnName = "firstname";
- myDataTable.Columns.Add(myDataColumn);
- myDataColumn = new DataColumn();
- myDataColumn.DataType = Type.GetType("System.String");
- myDataColumn.ColumnName = "lastname";
- myDataTable.Columns.Add(myDataColumn);
- return myDataTable;
- }
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
Insert data into datatable.
private void AddDataToTable(string username,string firstname,string lastname,DataTable
- myTable)
- {
- DataRow row;
- row = myTable.NewRow();
- row["id"] = Guid.NewGuid().ToString();
- row["username"] = username;
- row["firstname"] = firstname;
- row["lastname"] = lastname;
- myTable.Rows.Add(row);
- }
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
Add data to datatable which we have created
- protected void btnAdd_Click(object sender, EventArgs e)
- {
- if (txtUserName.Text.Trim() == "")
- {
- this.lblTips.Text = "You must fill a username.";
- return;
- }
- else
- {
- AddDataToTable(this.txtUserName.Text.Trim(), this.txtFirstName.Text.Trim(), this.txtLastName.Text.Trim(), (DataTable)Session["myDatatable"]);
- this.GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
- this.GridView1.DataBind();
- this.txtFirstName.Text = "";
- this.txtLastName.Text = "";
- this.txtUserName.Text = "";
- this.lblTips.Text = "";
- }
- }
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
Be noted "Session["myDatatable"] = myDt;" is important to ensure we can add new data continually until the page be closed.
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- myDt = new DataTable();
- myDt = CreateDataTable();
- Session["myDatatable"] = myDt;
- this.GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
- this.GridView1.DataBind();
- }
- }
0 comments:
Post a Comment