Control disappears

Discussion in 'ASP .Net Web Controls' started by Jakob Lithner, Feb 9, 2009.

  1. In my application I have a dynamic set of attributes stored in a separate
    table. To handle this I created a usercontrol that takes a collection of
    attributes. The idea is to show all relevant attibutes and make them editable
    for the user. Each attribute has: Name, datatype and value.

    I use the datatype to create a dynamic control (TextBox, CheckBox or
    DropDownList) and put it in a table cell. It works fine.

    The problem is when I want to save the values. I then iterate through my
    repeater items and pick up all relevant information. The problem is that the
    dynamic controls have disappeared! All controls are found except the ones
    with the values.

    I looked in the resulting webpage source and find all controls in the proper
    place. The ASP generated ID and names look correct.

    Any help very much appreciated!



    ASPX code:
    ======================================
    <%@ Control Language="C#" AutoEventWireup="true"
    CodeBehind="ucAttributeList.ascx.cs"
    Inherits="XXX.YYY.Web.AppControls.ucAttributeList" %>
    <table>
    <tr>
    <th>
    Name
    </th>
    <th>
    Value
    </th>
    <th>
    DataType
    </th>
    <th>
    Description
    </th>
    </tr>
    <asp:Repeater ID="repAttribute"
    OnItemDataBound="repAttribute_ItemDataBound" runat="server">
    <ItemTemplate>
    <tr>
    <td>
    <asp:Literal ID="litAttributeDisplayName" runat="server"
    />
    <asp:HiddenField ID="AttributeTypeID" runat="server" />
    <asp:HiddenField ID="ObjectAttributeID" runat="server" />
    </td>
    <td id="valueCell" runat="server">
    </td>
    <td>
    <asp:Literal ID="litDataType" runat="server" />
    </td>
    <td>
    <asp:Literal ID="litDescription" runat="server" />
    </td>
    </tr>
    </ItemTemplate>
    </asp:Repeater>
    <tr>
    <td>
    </td>
    <td>
    <asp:Button ID="btnSave" runat="server" Text="Save"
    onclick="btnSave_Click" />
    </td>
    <td>
    </td>
    <td>
    </td>
    </tr>
    </table>



    C# code:
    ======================================
    public partial class ucAttributeList : UserControl
    {
    public new List<Attribute> Attributes
    {
    get
    {
    var attributes = new List<Attribute>();
    foreach (var item in repAttribute.Items)
    {
    // Find controls
    var repeaterItem = (RepeaterItem)item;
    var AttributeTypeID =
    (HiddenField)(repeaterItem.FindControl("AttributeTypeID"));
    var ObjectAttributeID =
    (HiddenField)(repeaterItem.FindControl("ObjectAttributeID"));
    var valueCell =
    (HtmlTableCell)(repeaterItem.FindControl("valueCell"));
    var control = valueCell.FindControl("ctrlValue");

    // Create attribute and interpret values
    var attribute = new Attribute();
    attribute.AttributeTypeID =
    int.Parse(AttributeTypeID.Value);
    attribute.ObjectAttributeID =
    int.Parse(ObjectAttributeID.Value);
    if (control is TextBox)
    attribute.Value = ((TextBox)control).Text;
    else if (control is CheckBox)
    attribute.Value = ((CheckBox)control).Checked;
    else if (control is DropDownList)
    attribute.Value =
    int.Parse(((DropDownList)control).SelectedValue);
    else
    throw new SewsException("Control {0} is not handled",
    control.GetType());
    attributes.Add(attribute);
    }

    return attributes;
    }
    set
    {
    repAttribute.DataSource = value;
    repAttribute.DataBind();
    }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void repAttribute_ItemDataBound(object sender,
    RepeaterItemEventArgs e)
    {
    switch (e.Item.ItemType)
    {
    case ListItemType.Item:
    case ListItemType.AlternatingItem:

    // Interpret data item
    var attribute = (Attribute)e.Item.DataItem;

    // Find controls
    var litAttributeDisplayName =
    (Literal)(e.Item.FindControl("litAttributeDisplayName"));
    var AttributeTypeID =
    (HiddenField)(e.Item.FindControl("AttributeTypeID"));
    var ObjectAttributeID =
    (HiddenField)(e.Item.FindControl("ObjectAttributeID"));
    var valueCell =
    (HtmlTableCell)(e.Item.FindControl("valueCell"));
    var litDataType =
    (Literal)(e.Item.FindControl("litDataType"));
    var litDescription =
    (Literal)(e.Item.FindControl("litDescription"));

    // Display static data
    litAttributeDisplayName.Text =
    attribute.AttributeType.DisplayName;
    AttributeTypeID.Value = attribute.AttributeTypeID.ToString();
    ObjectAttributeID.Value =
    attribute.ObjectAttributeID.ToString();
    litDataType.Text =
    attribute.AttributeType.AttributeDataType.Name;
    litDescription.Text = attribute.AttributeType.Description;

    // Handle dynamic control
    AttributeDataTypeEnum dataType =
    (AttributeDataTypeEnum)attribute.AttributeType.AttributeDataTypeID;
    switch (dataType)
    {
    case AttributeDataTypeEnum.String:
    case AttributeDataTypeEnum.int32:
    case AttributeDataTypeEnum.float32:
    var textBox = new TextBox();
    textBox.ID = "ctrlValue";
    textBox.Text = attribute.Value.ToString();
    textBox.EnableViewState = true;
    valueCell.Controls.Add(textBox);
    break;

    case AttributeDataTypeEnum.Boolean:
    var checkBox = new CheckBox();
    checkBox.ID = "ctrlValue";
    checkBox.Checked = (bool)attribute.Value;
    valueCell.Controls.Add(checkBox);
    break;

    case AttributeDataTypeEnum.Enum:
    var dropDownList = new DropDownList();
    dropDownList.ID = "ctrlValue";
    dropDownList.DataSource =
    AttributeEnumAdapter.GetAttributeEnums(attribute.AttributeTypeID);
    dropDownList.DataValueField = "Value";
    dropDownList.DataTextField = "Name";
    dropDownList.DataBind();
    dropDownList.SelectedValue = attribute.ValueText;
    valueCell.Controls.Add(dropDownList);
    break;

    default:
    throw new SewsException("AttributeDataType {0} is not
    handled", dataType);
    }
    break;
    }
    }

    /// <summary>
    /// Save all values from GUI.
    /// </summary>
    protected void btnSave_Click(object sender, EventArgs e)
    {
    using (var dc = Sews2DataContext.CreateInstance())
    {
    foreach (var attribute in Attributes)
    {
    var dbAttribute = dc.Attributes.Single(a =>
    a.AttributeTypeID == attribute.AttributeTypeID && a.ObjectAttributeID ==
    attribute.ObjectAttributeID);
    dbAttribute.ValueText = attribute.ValueText;
    }
    dc.SubmitChanges();
    }
    }
    }



    Extract from generated web source:
    ==================================
    <tr>
    <td>
    Reset to Default
    <input type="hidden"
    name="ctl00$MajorContent$ucAllocateDOToContainer$myAttributeList$repAttribute$ctl02$AttributeTypeID"
    id="ctl00_MajorContent_ucAllocateDOToContainer_myAttributeList_repAttribute_ctl02_AttributeTypeID" value="19" />
    <input type="hidden"
    name="ctl00$MajorContent$ucAllocateDOToContainer$myAttributeList$repAttribute$ctl02$ObjectAttributeID"
    id="ctl00_MajorContent_ucAllocateDOToContainer_myAttributeList_repAttribute_ctl02_ObjectAttributeID" value="0" />
    </td>
    <td
    id="ctl00_MajorContent_ucAllocateDOToContainer_myAttributeList_repAttribute_ctl02_valueCell">
    <input
    id="ctl00_MajorContent_ucAllocateDOToContainer_myAttributeList_repAttribute_ctl02_ctrlValue"
    type="checkbox"
    name="ctl00$MajorContent$ucAllocateDOToContainer$myAttributeList$repAttribute$ctl02$ctrlValue" checked="checked" /></td>
    <td>
    Boolean
    </td>
    <td>
    </td>
    </tr>
     
    Jakob Lithner, Feb 9, 2009
    #1
    1. Advertising

  2. Hi Jakob,

    Thank you for posting here!

    I notice that you have posted the same question in case 43253535. Admin
    newsgroup, which I have already responded. So please check my answer there
    and if you need any further assistance on this particular issue, please
    reply to me in that thread so I can follow up with you in time.

    Sincerely,

    Vince Xu

    Microsoft Online Support
     
    Vince Xu [MSFT], Feb 10, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guadala Harry

    User Control Disappears After PostBack - Why?

    Guadala Harry, Jul 1, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    10,269
    Guadala Harry
    Jul 2, 2004
  2. Tina
    Replies:
    2
    Views:
    475
    =?Utf-8?B?YWU=?=
    Sep 29, 2005
  3. =?Utf-8?B?QVNQIERldmVsb3Blcg==?=

    Control disappears after ID is changed

    =?Utf-8?B?QVNQIERldmVsb3Blcg==?=, Jun 22, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    359
    =?Utf-8?B?QVNQIERldmVsb3Blcg==?=
    Jun 22, 2006
  4. Sami Rehman
    Replies:
    1
    Views:
    1,312
    Mark Rae [MVP]
    Sep 3, 2007
  5. Cato Antonsen

    Norwegian characters in string property of Custom Control disappears

    Cato Antonsen, Jan 23, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    164
    Cato Antonsen
    Jan 23, 2004
Loading...

Share This Page