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):
==============a spx =============== =========
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
ConnectionStrin g="<%$ ConnectionStrin gs:CLRTestDBCon nectionString %>"
SelectCommand=" SELECT [StockPicker_ID], [Question],
[ControlType] FROM [StockPicker]">
</asp:SqlDataSour ce>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Repeater ID="Repeater1" runat="server"
DataSourceID="S qlDataSource1" OnItemDataBound ="Repeater1_Ite mDataBound">
<ItemTemplate >
<br /><hr /><br />
<table
style="width:10 0%;border-style:solid;bor der-width:2pt;borde r-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:DropDownLi st ID="lst" runat="server">
</asp:DropDownLis t>
<asp:RadioButto nList ID="rlst" runat="server">
</asp:RadioButton List>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<br />
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Submit Update"
OnClick="btnUpd ate_Click" /></div>
</form>
</body>
</html>
==========code behind========= ===========
public partial class dataaccess_Repe aterPage : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
}
protected void Repeater1_ItemD ataBound(object sender,
RepeaterItemEve ntArgs e)
{
if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
PlaceHolder holder = e.Item.FindCont rol("phItem") as
PlaceHolder;
string ctype = DataBinder.Eval (e.Item.DataIte m, "ControlTyp e")
as string;
int qid = (int)DataBinder .Eval(e.Item.Da taItem,
"StockPicker_ID ");
Label lbl = e.Item.FindCont rol("lblQuestio n") as Label;
TextBox txt = e.Item.FindCont rol("txt") as TextBox;
DropDownList lst = e.Item.FindCont rol("lst") as DropDownList;
RadioButtonList rlst = e.Item.FindCont rol("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 "DropDownLi st":
lst.Items.Add(" item1.......");
lst.Items.Add(" item2.......");
lst.Items.Add(" item3.......");
txt.Visible = false;
lst.Visible = true;
rlst.Visible = false;
break;
case "RadioButtonLis t":
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.It em || item.ItemType ==
ListItemType.Al ternatingItem)
{
Label lbl = item.FindContro l("lblQuestion" ) as Label;
int qid = int.Parse(lbl.A ttributes["qid"]);
string ctype = lbl.Attributes["ctype"];
string newvalue = null;
switch (ctype)
{
case "TextBox":
TextBox txt = item.FindContro l("txt") as TextBox;
newvalue = txt.Text;
break;
case "DropDownLi st":
DropDownList lst = item.FindContro l("lst") as
DropDownList;
newvalue = lst.SelectedVal ue;
break;
case "RadioButtonLis t":
RadioButtonList rlst = item.FindContro l("rlst") as
RadioButtonList ;
newvalue = rlst.SelectedVa lue;
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/subscripti...ult.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/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.