Home All Groups Group Topic Archive Search About

Dropdownlist set to last value in gridview on postback

Author
9 Nov 2006 7:07 PM
BSchwartz
Background:
I have an application that reads configuration information from an XML file.
This XML file has the definition of what a Gridview's columns will look
like.  These columns could be BoundFields or TemplateFields.  A standard grid
many have look something like this.

4 rows that have the follow columns

TemplateField (CheckBox), BoundField, BoundField, TemplateField (TextBox),
TemplateField (Dropdownlist)

Problem:
The problem occurs when I try to reload the correct values into the dynamic
controls.  I have placed the column generation in the Page_Init() (Pre_Init
would work too).  The CheckBox and TextBox fields reload properly, but the
Dropdownlist sets all Dropdownlists to the last dropdownlist selected value
in the dropdownlist column.  This happens on postback and I am not doing a
rebind of Data.

Let me know if more information is needed.

Any help is greatly appreciated.

Thanks!

Author
17 Nov 2006 11:54 AM
Steven Cheng[MSFT]
Hello,

From your description, you're using GridView to display some data, and to
make it flexible, you dynamically create the Columns(boundfield and
templatefield) for the GridView in page. However, you found that some
control(like dropdownlist) doesn't work well on page when postback, correct?

As for the TemplateField, do you mean that you dynamically create it and
load the Template(such as ItemTemplate) through Page.LoadTemplate method?
Also,  for the GridView, are you using an associated DataSource control or
manually assign DataSource property to it and call DataBind method?

I've just perfomed some test on my local environment, I add an
templateField in the GridView dynamically and load the ItemTemplate from an
ascx template file. The ascx template contains a Dropdownlist and a button.
It seems at runtime, when the page postback, each dropdownlist(in each
GridView row) can maintain its own selected value. Here are the complete
page stuffs, you can test it for reference.

========PAGE ASPX template=========
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CategoryID], [CategoryName] FROM
[Categories]"></asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="CategoryID"
            >
            <Columns>

            </Columns>
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" Text="Button" /></div>
    </form>
</body>

=============PAGE CODE BEHIND==========

public partial class Controls_DynamicGridView : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        BoundField bf1 = new BoundField();
        bf1.DataField = "CategoryID";
        bf1.HeaderText = "CategoryID..";
        bf1.ReadOnly = true;

        GridView1.Columns.Add(bf1);

        TemplateField tf1 = new TemplateField();
        tf1.ItemTemplate = Page.LoadTemplate("./tmp.ascx");
        tf1.HeaderText = "TemplateField";

        GridView1.Columns.Add(tf1);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource =
SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            GridView1.DataBind();
        }
    }
}

================================

=====tmp.ascx==========
<br/>Test List:<br/>
                    <asp:DropDownList ID="lst" runat="server">
                    <asp:ListItem Text="aaa" Value="aaa"></asp:ListItem>
                    <asp:ListItem Text="bbb" Value="bbb"></asp:ListItem>
                    <asp:ListItem Text="ccc" Value="ccc"></asp:ListItem>
                    </asp:DropDownList>
                     <asp:Button ID="btnSubmit" runat="server" Text="Submit
Button" />
========================

Please feel free to let me know if there is anything different or I missed.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
Author
17 Nov 2006 2:23 PM
BSchwartz
I am actually creating the members of the drop down list dynamically as well
as the binding the data source manually.  I simply get a DataSet from the BLL
and bind that to the GridView.  Here is what I am doing with the template
fields.  And on post back the drop down lists are all set to the value of the
last DropDownlist.

====== HelperObject.cs
// This is in the function to create the column
TemplateField tf = new TemplateField();
tf.HeaderText = xmlnode.InnerText;
BaseTemplate template = new DropDownListGridViewTemplate();
template.SetID("optionfield");
template.SetListItemCollection(listOfItems);
tf.ItemTemplate = template;

//Add to CreatedColumns Collection


===== Default.aspx.cs
protected void Page_Init(object sender, EventArgs e)
{
   //Call helper function

   foreach (DataControlField dcf in CreatedColumns)
   {
      grid.Columns.Add(dcf);
   }
}

===== GridColumnTemplates.cs
public class DropDownListGridViewTemplate : BaseTemplate
{
   private ListItem[] _items = new ListItem[0];

   public void SetListItemCollection(ListItem[] items)
   {
      _items = items;
   }

   #region ITemplate Members
   public override void InstantiateIn(Control container)
   {
      DropDownList ddlField = new DropDownList();
      ddlField.ID = _ID;
      ddlField.Items.AddRange(_items);
      ddlField.EnableViewState = true;
      container.Controls.Add(ddlField);
   }
   #endregion
}
   }
}



Show quoteHide quote
"Steven Cheng[MSFT]" wrote:

> Hello,
>
> From your description, you're using GridView to display some data, and to
> make it flexible, you dynamically create the Columns(boundfield and
> templatefield) for the GridView in page. However, you found that some
> control(like dropdownlist) doesn't work well on page when postback, correct?
>
> As for the TemplateField, do you mean that you dynamically create it and
> load the Template(such as ItemTemplate) through Page.LoadTemplate method?
> Also,  for the GridView, are you using an associated DataSource control or
> manually assign DataSource property to it and call DataBind method?
>
> I've just perfomed some test on my local environment, I add an
> templateField in the GridView dynamically and load the ItemTemplate from an
> ascx template file. The ascx template contains a Dropdownlist and a button.
> It seems at runtime, when the page postback, each dropdownlist(in each
> GridView row) can maintain its own selected value. Here are the complete
> page stuffs, you can test it for reference.
>
> ========PAGE ASPX template=========
> <body>
>     <form id="form1" runat="server">
>     <div>
>         <asp:SqlDataSource ID="SqlDataSource1" runat="server"
> ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
>             SelectCommand="SELECT [CategoryID], [CategoryName] FROM
> [Categories]"></asp:SqlDataSource>
>         <asp:GridView ID="GridView1" runat="server"
> AutoGenerateColumns="False" DataKeyNames="CategoryID"
>             >
>             <Columns>
>               
>             </Columns>
>         </asp:GridView>
>         <asp:Button ID="Button1" runat="server" Text="Button" /></div>
>     </form>
> </body>
>
> =============PAGE CODE BEHIND==========
>
> public partial class Controls_DynamicGridView : System.Web.UI.Page
> {
>     protected void Page_Init(object sender, EventArgs e)
>     {
>         BoundField bf1 = new BoundField();
>         bf1.DataField = "CategoryID";
>         bf1.HeaderText = "CategoryID..";
>         bf1.ReadOnly = true;
>
>         GridView1.Columns.Add(bf1);
>
>         TemplateField tf1 = new TemplateField();
>         tf1.ItemTemplate = Page.LoadTemplate("./tmp.ascx");
>         tf1.HeaderText = "TemplateField";
>
>         GridView1.Columns.Add(tf1);
>     }
>
>     protected void Page_Load(object sender, EventArgs e)
>     {
>         if (!IsPostBack)
>         {
>             GridView1.DataSource =
> SqlDataSource1.Select(DataSourceSelectArguments.Empty);
>             GridView1.DataBind();
>         }
>     }
> }
>
> ================================
>
> =====tmp.ascx==========
> <br/>Test List:<br/>
>                     <asp:DropDownList ID="lst" runat="server">
>                     <asp:ListItem Text="aaa" Value="aaa"></asp:ListItem>
>                     <asp:ListItem Text="bbb" Value="bbb"></asp:ListItem>
>                     <asp:ListItem Text="ccc" Value="ccc"></asp:ListItem>
>                     </asp:DropDownList>
>                      <asp:Button ID="btnSubmit" runat="server" Text="Submit
> Button" />
> ========================
>
> Please feel free to let me know if there is anything different or I missed.
>
> Sincerely,
>
> Steven Cheng
>
> Microsoft MSDN Online Support Lead
>

>
> ==================================================
>
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
> ications.
>

>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscriptions/support/default.aspx.
>
> ==================================================
>

>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
Author
21 Nov 2006 9:10 AM
Steven Cheng[MSFT]
Hello,

Thanks for your further response and the code snippet.

Well, after some test agains the code, the problem should be caused by the
AddRange method which directly populate the dropdownlist's collection from
the _items property of the DropDownListGridViewTemplate  instance.   As for
the DropDownListGridViewTemplate instance, it will be created once in each
page request, however, its "InstantiateIn" method will be called in each
GridView's ItemCreation lifecycle. Therefore the "_items" collection is
actually be shared by all the GridView rows, and this cause the mixed state
issue. I've changed the code logic as below which explicitly create new
ListItem instance for each InstantiateIn method call:

========================
public class DropDownListGridViewTemplate : ITemplate
{
    private ListItem[] _items = new ListItem[0];

    public void SetListItemCollection(ListItem[] items)
    {
        _items = items;
    }



    #region ITemplate Members

    void ITemplate.InstantiateIn(Control container)
    {

HttpContext.Current.Response.Write("<br/>ITemplate.InstantiateIn...");
        DropDownList ddlField = new DropDownList();
        ddlField.ID = "lstTmp";

        foreach (ListItem item in _items)
        {
            ddlField.Items.Add(new ListItem(item.Text, item.Value));
        }

        container.Controls.Add(ddlField);
    }

    #endregion
}

====================================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.