473,516 Members | 3,456 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 3761
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
15273
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
2506
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...
0
3081
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...
0
1754
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
941
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
2103
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...
2
5501
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
1399
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...
5
2577
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...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7136
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...
0
7547
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5106
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...
0
3265
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3252
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1620
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
1
823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.