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
ropDownList 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
ropDownList>
<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.