473,732 Members | 2,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Edit Button in Dynamically created DataList

76 New Member
Background: I have a generated datalist to display information and I need to add the capability to have a button (Edit button) so that users can change the information. I'm sure once I figure it out I'll smack myself on the head for being so stupid but I feel as though right now I'm chasing my tail and want to see if some of yall can point me in a better direction.

Initiation
Expand|Select|Wrap|Line Numbers
  1.  public class GroupDetail : WebPart
  2.   {
  3. //...
  4.     DataList dlViewGroup;
  5. protected override void CreateChildControls()
  6. {
  7. base.CreateChildControls();
  8.       dlViewGroup = new DataList();
  9.       oWeb = getWebFromURL(strOrgSite);
  10.       oOrgChart = getListByName(strOrgList, oWeb);
  11.       if (strGroupCode != string.Empty)
  12.       {
  13.         getGroupInformation(oOrgChart, strGroupCode);
  14.       }
  15.       else if (strDefaultGroupCode != string.Empty)
  16.       {
  17.         getGroupInformation(oOrgChart, strDefaultGroupCode);
  18.       }
  19.       dlViewGroup.HeaderTemplate = new GroupDetailTemplate(ListItemType.Header);
  20.       dlViewGroup.ItemTemplate = new GroupDetailTemplate(ListItemType.Item);
  21.       dlViewGroup.FooterTemplate = new GroupDetailTemplate(ListItemType.Footer);
  22.       dlViewGroup.EditItemTemplate = new GroupDetailTemplate(ListItemType.EditItem);
  23.       dlViewGroup.EditCommand += new DataListCommandEventHandler(dlViewGroup_Edit);
  24.       this.Controls.Add(dlViewGroup);
  25. }
  26.     //User Control Methods
  27.     protected void dlViewGroup_Edit(object sender, DataListCommandEventArgs e)
  28.     {
  29.       dlViewGroup.EditItemIndex = e.Item.ItemIndex;
  30.       BindGroupData();
  31.     }
  32. }
  33.  
Then here is the templat for the GroupDetail DataList
Expand|Select|Wrap|Line Numbers
  1.    public void InstantiateIn(System.Web.UI.Control container)
  2.     {
  3.       //Declare Template Items
  4.       PlaceHolder ph = new PlaceHolder();
  5.       Label lblDirective = new Label();
  6.       Label lblSymbol = new Label();
  7.       Label lblFax = new Label();
  8.       Label lblEmail = new Label();
  9.       Label lblDirectory = new Label();
  10.       HyperLink lnkArea = new HyperLink();
  11.       lblDirective.ID = "lblDirective";
  12.       lblSymbol.ID = "lblSymbol";
  13.       lblFax.ID = "lblFax";
  14.       lblEmail.ID = "lblEmail";
  15.       lblDirectory.ID = "lblDirectory";
  16.       lnkArea.ID = "lnkArea";
  17.       lnkArea.Target = "_blank";
  18.  
  19.       //Declare Template Controls
  20.       Button btnEditDept = new Button();
  21.       btnEditDept.ID = "btnEditDept";
  22.       btnEditDept.CommandName = "edit";
  23.       btnEditDept.Text = "Edit Department";     
  24.  
  25.       switch (templateType)
  26.       {
  27.         case ListItemType.Header:
  28.           ph.Controls.Add(new LiteralControl("<table border=\"0\"><tr><td colspan=\"2\" class=\"ms-toolbar\">"));
  29.           ph.Controls.Add(btnEditDept);
  30.           ph.Controls.Add(new LiteralControl("</td></tr>"));
  31.           break;
  32.         case ListItemType.Item:
  33.            //Department
  34.           ph.Controls.Add(new LiteralControl("<tr><td class=\"ms-formlabel\">"));
  35.           ph.Controls.Add(new LiteralControl("Department:"));
  36.           ph.Controls.Add(new LiteralControl("</td><td>"));
  37.           ph.Controls.Add(lblDirective);
  38.           ph.Controls.Add(new LiteralControl("</td></tr>"));
  39.  
  40.           //Office Symbol
  41.           ph.Controls.Add(new LiteralControl("<tr><td class=\"ms-formlabel\">"));
  42.           ph.Controls.Add(new LiteralControl("Office Symbol:"));
  43.           ph.Controls.Add(new LiteralControl("</td><td>"));
  44.           ph.Controls.Add(lblSymbol);
  45.           ph.Controls.Add(new LiteralControl("</td></tr>"));
  46.  
  47.           //Office Fax
  48.           ph.Controls.Add(new LiteralControl("<tr><td class=\"ms-formlabel\">"));
  49.           ph.Controls.Add(new LiteralControl("Office Fax:"));
  50.           ph.Controls.Add(new LiteralControl("</td><td>"));
  51.           ph.Controls.Add(lblFax);
  52.           ph.Controls.Add(new LiteralControl("</td></tr>"));
  53.  
  54.           //Office Email
  55.           ph.Controls.Add(new LiteralControl("<tr><td class=\"ms-formlabel\">"));
  56.           ph.Controls.Add(new LiteralControl("Office Email:"));
  57.           ph.Controls.Add(new LiteralControl("</td><td>"));
  58.           ph.Controls.Add(lblEmail);
  59.           ph.Controls.Add(new LiteralControl("</td></tr>"));
  60.  
  61.           //Staff Directory
  62.           ph.Controls.Add(new LiteralControl("<tr><td class=\"ms-formlabel\">"));
  63.           ph.Controls.Add(new LiteralControl("Staff Directory:"));
  64.           ph.Controls.Add(new LiteralControl("</td><td>"));
  65.           ph.Controls.Add(lblDirectory);
  66.           ph.Controls.Add(new LiteralControl("</td></tr>"));
  67.  
  68.           //Office Website
  69.           ph.Controls.Add(new LiteralControl("<tr><td class=\"ms-formlabel\">"));
  70.           ph.Controls.Add(new LiteralControl("Website:"));
  71.           ph.Controls.Add(new LiteralControl("</td><td>"));
  72.           ph.Controls.Add(lnkArea);
  73.           ph.Controls.Add(new LiteralControl("</td></tr>")); 
  74.  
  75.           ph.DataBinding += new EventHandler(Item_DataBinding);
  76.           break;
  77.         case ListItemType.Footer:
  78.           ph.Controls.Add(new LiteralControl("</table>"));
  79.           break;
  80.         case ListItemType.EditItem:
  81.           //Department
  82.           ph.Controls.Add(new LiteralControl("<tr><td class=\"ms-formlabel\">"));
  83.           ph.Controls.Add(new LiteralControl("Department:"));
  84.           ph.Controls.Add(new LiteralControl("</td><td>"));
  85.           ph.Controls.Add(lblDirective);
  86.           ph.Controls.Add(new LiteralControl("</td></tr>"));
  87.  
  88.           //Office Symbol
  89.           ph.Controls.Add(new LiteralControl("<tr><td class=\"ms-formlabel\">"));
  90.           ph.Controls.Add(new LiteralControl("Office Symbol:"));
  91.           ph.Controls.Add(new LiteralControl("</td><td>"));
  92.           ph.Controls.Add(lblSymbol);
  93.           ph.Controls.Add(new LiteralControl("</td></tr>"));
  94.  
  95.           //Office Fax
  96.           ph.Controls.Add(new LiteralControl("<tr><td class=\"ms-formlabel\">"));
  97.           ph.Controls.Add(new LiteralControl("Office Fax:"));
  98.           ph.Controls.Add(new LiteralControl("</td><td>"));
  99.           ph.Controls.Add(lblFax);
  100.           ph.Controls.Add(new LiteralControl("</td></tr>"));
  101.  
  102.           //Office Email
  103.           ph.Controls.Add(new LiteralControl("<tr><td class=\"ms-formlabel\">"));
  104.           ph.Controls.Add(new LiteralControl("Office Email:"));
  105.           ph.Controls.Add(new LiteralControl("</td><td>"));
  106.           ph.Controls.Add(lblEmail);
  107.           ph.Controls.Add(new LiteralControl("</td></tr>"));
  108.  
  109.           //Staff Directory
  110.           ph.Controls.Add(new LiteralControl("<tr><td class=\"ms-formlabel\">"));
  111.           ph.Controls.Add(new LiteralControl("Staff Directory:"));
  112.           ph.Controls.Add(new LiteralControl("</td><td>"));
  113.           ph.Controls.Add(lblDirectory);
  114.           ph.Controls.Add(new LiteralControl("</td></tr>"));
  115.  
  116.           //Office Website
  117.           ph.Controls.Add(new LiteralControl("<tr><td class=\"ms-formlabel\">"));
  118.           ph.Controls.Add(new LiteralControl("Website:"));
  119.           ph.Controls.Add(new LiteralControl("</td><td>"));
  120.           ph.Controls.Add(lnkArea);
  121.           ph.Controls.Add(new LiteralControl("</td></tr>"));
  122.  
  123.           ph.DataBinding += new EventHandler(Item_DataBinding);
  124.           break;
  125.       }
  126.       container.Controls.Add(ph);
  127.     }
  128.     static void Item_DataBinding(object sender, System.EventArgs e)
  129.     {
  130.       PlaceHolder ph = (PlaceHolder)sender;
  131.       DataListItem dli = (DataListItem)ph.NamingContainer;
  132.       ((Label)ph.FindControl("lblDirective")).Text = getStringFromValue(DataBinder.Eval(dli.DataItem, "Title"));
  133.       ((Label)ph.FindControl("lblSymbol")).Text = getStringFromValue(DataBinder.Eval(dli.DataItem, "Symbol"));
  134.       ((Label)ph.FindControl("lblFax")).Text = getStringFromValue(DataBinder.Eval(dli.DataItem, "Office_x0020_Fax"));
  135.       ((Label)ph.FindControl("lblEmail")).Text = getStringFromValue(DataBinder.Eval(dli.DataItem, "Office_x0020_Email"));
  136.       ((Label)ph.FindControl("lblDirectory")).Text = getTextFromNumber(Int32.Parse(getStringFromValue(DataBinder.Eval(dli.DataItem, "Staff_x0020_Directory"))));
  137.       ((HyperLink)ph.FindControl("lnkArea")).NavigateUrl = getFormatURL(getStringFromValue(DataBinder.Eval(dli.DataItem, "Area")), 0);
  138.       ((HyperLink)ph.FindControl("lnkArea")).Text = getFormatURL(getStringFromValue(DataBinder.Eval(dli.DataItem, "Area")), 1);
  139.     }
  140.  
Nov 19 '09 #1
1 3772
Arielle
76 New Member
I have not found the answer yet but I did get a slightly better explination. Apparently because the controls are created dynamically they are "receated" on postback and therefore it is not holding on to the viewstate.
Nov 30 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
15287
by: Ravikanth[MVP] | last post by:
Hi Check whether textbox control name is 'txtDischargeDate' or it contains automatically generated id? HTH Ravikanth >-----Original Message-----
3
2526
by: Hartmut Schroth | last post by:
Hi, I need a solution for the following problem: In the item template of a datalist control I have already a button control with the commandname set to "select" to perform some specific database action. I additionally want to update an integer value in the underlying data source by simply clicking an a check box or an imagebutton WITHOUT USING THE EDIT/UPDATE COMMANDNAME property of the
0
3112
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format. However, the DataGrid begins to get very cumbersome as the number of columns of data you present increases. Anything more than half a dozen columns or so and you probably induce horizontal scrolling - a real no-no for me. If you put such a...
0
1765
by: Alex | last post by:
Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Configuration Public Class Main : Inherits Page Private strConn As String =
0
950
by: Wee Bubba | last post by:
if you put a row of my dataList into edit mode there is a button. I am trying to attach to an existing event handler like this (C#): public void EditRowHandler (Object src, DataListCommandEventArgs e) { ... Button myBtnt = (Button) dl1.Items.FindControl("btnSMEdit"); myBtn.Click += new EventHandler(btnSearch_Click);
4
2109
by: tshad | last post by:
I have a DataList that I have set up to allow editing. The edit button does go to the subroutine defined by OnEditCommand. The problem is that it doesn't change the fields to editing fields. The textbox doesn't show from the edititemtemplate section. I don't get any errors. Do I need to do something special to tell it to do this? Here is the datalist: ...
2
5516
by: donnet | last post by:
Inside my .aspx file, I have a textbox populated with data from a dataset like this: <asp:TextBox text='<%# DataBinder.Eval(Container.DataItem, "Comment")%>' id="CommentText" runat="server" TextMode="MultiLine"></asp:TextBox> </ItemTemplate> </asp:datalist> <asp:button id="EditCommentButton" Text="Edit Comment" Runat="server"></asp:button
1
1406
by: jagdishl | last post by:
Hi: I have a datalist which is populated dynamically based on a query.I have to place a submit button after datalist (not in the footer template) so that after pressing the submit button a final update is made to the database.The datalist may at times be very long.i was wondering how I could place this submit button in the design page.I would really be grateful if you could point out to sample codes. Thank you. Jagdish.l
5
2593
by: \A_Michigan_User\ | last post by:
I'm using asp.net/vb.net/ado.net to create a very simple user interface. (Everything works if I use STATICALLY created textBoxes... but not when I make them DYNAMICALLY.) 1. Execute a SQL SELECT cmd to retrieve a record. 2. Loop through the dataReader values, creating textBox boxes dynamically, adding them to a Panel. (The database values are placed in these textboxes.)
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.