Hi Steve,
After some research against the table data and the requirement here, I
found that it is quite hard to do all the things dynamically, especially
for the different kind of controls. Here you've given me three
ControlTypes, they're TextBox, DropDownList and RadioButtonList, how
many
ones will you have in the real project? This will determine whether my
solution here is workable(it won't quite work if there're many different
kinds of controls).
Currently, in template databound control, such as Repeater or DataList...
,
we can dynamically modify and customize some existing controls in the
ItemTemplate during databinding period, however, we can not dynamically
create and add new controls at that time. This is because all the dynamic
controls added in databinding will lost in sequential postback. One way
to resolve this is predefined all the possible controls in the template
and
dynamically choose to show and hide the proper one.
Also, for updating, we can loop through all the controls in the template
databound control's item collection and get reference to those sub
controls
in each item and pickup the updated value from them. Here is a simple
test
page demonstrate the funcionality (use the test database data you provided
in the last message):
==============aspx ========================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CLRTestDBConnectionString %>"
SelectCommand="SELECT [StockPicker_ID], [Question],
[ControlType] FROM [StockPicker]">
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Repeater ID="Repeater1" runat="server"
DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<br /><hr /><br />
<table
style="width:100%;border-style:solid;border-width:2pt;border-color:Black"
cellpadding="0" cellspacing="0">
<tr>
<td>
Question: <asp:Label ID="lblQuestion" runat="server"
Text='<%# Eval("Question")%>'></asp:Label><br />
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp

ropDownList ID="lst" runat="server">
</asp

ropDownList>
<asp:RadioButtonList ID="rlst" runat="server">
</asp:RadioButtonList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<br />
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Submit Update"
OnClick="btnUpdate_Click" /></div>
</form>
</body>
</html>
==========code behind====================
public partial class dataaccess_RepeaterPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Repeater1_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
PlaceHolder holder = e.Item.FindControl("phItem") as
PlaceHolder;
string ctype = DataBinder.Eval(e.Item.DataItem, "ControlType")
as string;
int qid = (int)DataBinder.Eval(e.Item.DataItem,
"StockPicker_ID");
Label lbl = e.Item.FindControl("lblQuestion") as Label;
TextBox txt = e.Item.FindControl("txt") as TextBox;
DropDownList lst = e.Item.FindControl("lst") as DropDownList;
RadioButtonList rlst = e.Item.FindControl("rlst") as
RadioButtonList;
lbl.Attributes["qid"] = qid.ToString();
lbl.Attributes["ctype"] = ctype;
switch (ctype)
{
case "TextBox":
txt.Text = "Questions...........";
txt.Visible = true;
lst.Visible = false;
rlst.Visible = false;
break;
case "DropDownList":
lst.Items.Add("item1.......");
lst.Items.Add("item2.......");
lst.Items.Add("item3.......");
txt.Visible = false;
lst.Visible = true;
rlst.Visible = false;
break;
case "RadioButtonList":
rlst.Items.Add("item1.........");
rlst.Items.Add("item2.........");
rlst.Items.Add("item3.........");
txt.Visible = false;
lst.Visible = false;
rlst.Visible = true;
break;
}
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType ==
ListItemType.AlternatingItem)
{
Label lbl = item.FindControl("lblQuestion") as Label;
int qid = int.Parse(lbl.Attributes["qid"]);
string ctype = lbl.Attributes["ctype"];
string newvalue = null;
switch (ctype)
{
case "TextBox":
TextBox txt = item.FindControl("txt") as TextBox;
newvalue = txt.Text;
break;
case "DropDownList":
DropDownList lst = item.FindControl("lst") as
DropDownList;
newvalue = lst.SelectedValue;
break;
case "RadioButtonList":
RadioButtonList rlst = item.FindControl("rlst") as
RadioButtonList;
newvalue = rlst.SelectedValue;
break;
}
Response.Write("<br/>questionID: "+ qid + ", new value: " +
newvalue);
}
}
}
}
======================================
Please feel free to let me know if you have anything unclear or any other
ideas on this.
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.