This is just some quick-and-dirty code that demonstrates a way to take advantage of the Label control's "AssociatedControlID" attribute.
This ASP.NET sample builds XML from an online survey. In practice, you can have it build an email instead or save the XML for reporting and analysis.
Of course, this isn't an architected solution at all and is a little inefficient, but if someone wants a little online form and they need to be able to add questions with minimal effort (or create questions dynamically), this might be helpful.
Front-end code:
<asp:Panel ID="pnl1" runat="server">
<p>
<asp:Label ID="question1" runat="server" Text="What is your name?" AssociatedControlID="answer1" />:
<asp:TextBox ID="answer1" runat="server" />
</p>
<p>
<asp:Label ID="question2" runat="server" Text="What is your quest?" AssociatedControlID="answer2" />:
<asp:TextBox ID="answer2" runat="server" />
</p>
<p>
<asp:Label ID="question3" runat="server" Text="What is the air-speed velocity of an unladen swallow?" AssociatedControlID="answer3" />:
<asp:TextBox ID="answer3" runat="server" />
</p>
</asp:Panel>
<p>
<asp:Button ID="btnSubmit" runat="Server" OnClick="saveresponse" Text="submit" />
</p>
</div>
Server-side code:
protected void saveresponse(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (Control c in pnl1.Controls)
{
if (c.GetType().ToString() == ("System.Web.UI.WebControls.Label"))
{
Label l = (Label) c;
TextBox t = (TextBox) this.FindControl(l.AssociatedControlID.ToString());
sb.Append("<question questiontext=\"" + l.Text + "\"");
sb.Append(" questionid=\"" + c.ID.ToString() + "\"");
sb.Append(" answertext=\"" + t.Text + "\" />");
}
}
string stufftosave = sb.ToString();
//TODO: save the stufftosave
}