需要帮助在ASP.NET中创建数组 [英] Need help creating an Array in ASP.NET
本文介绍了需要帮助在ASP.NET中创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从计算器结果"文本框中获取数字并将其添加到数组中.然后,我需要使用按钮将结果打印到消息框或标签上.有任何想法吗?
I am trying to take numbers from a calculators results textbox and add it to an array. I then need to print the results to a message box or label using a button. any Ideas?
推荐答案
您可以使用列表,例如:-
假设这些是您的控件:
you can use List, Example:-
suppose these are your controls:
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
<asp:textbox id="TextBox2" runat="server"></asp:textbox>
<asp:textbox id="TextBox3" runat="server"></asp:textbox>
<asp:button id="AddButton" runat="server" text="Add" onclick="AddButton_Click" />
<asp:button id="ShowDataButton" runat="server" onclick="ShowDataButton_Click" text="Show Data"/>
代码是:
The code is:
public partial class WebForm1 : System.Web.UI.Page
{
public List<FormModel> FromData
{
get
{
if (ViewState["__FromData"] == null)
ViewState["__FromData"] = new List<FormModel>();
return (List<FormModel>)ViewState["__FromData"];
}
}
protected void AddButton_Click(object sender, EventArgs e)
{
FromData.Add(new FormModel()
{
Text1 = TextBox1.Text,
Text2 = TextBox2.Text,
Text3 = TextBox3.Text,
});
}
protected void ShowDataButton_Click(object sender, EventArgs e)
{
foreach (var item in FromData)
{
Response.Write(item.Text1 + "," + item.Text2 + "," + item.Text3);
Response.Write("<br />");
}
}
}
[Serializable]
public class FormModel
{
public string Text1 { get; set; }
public string Text2 { get; set; }
public string Text3 { get; set; }
}
希望对您有帮助.
祝你好运.
Hope it will help you.
Good luck.
这篇关于需要帮助在ASP.NET中创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文