作者:
aacced (aacced)
2014-06-04 23:04:43您好,以下我改成Web版,您參考參考!
protected void Button1_Click(object sender, EventArgs e)
{
string[] array2 = new string[] { "A", "B", "C", "D", "E" };
//^^ 這個矩陣是未知的,我先假設有ABCDE
for (int i = 0; i < array2.Length; i++)
{
Label myLabel = this.FindControl(string.Format("label{0}", (i+1).ToString())) as Label;
if (myLabel == null) continue;
myLabel.Text = array2[i];
}
}
※ 引述《sqrt1089 (李崩子)》之銘言:
: 您好,方向是這邊沒錯!先謝謝你~
: 但是我的for 是元素 (這邊是未知)
: 以下是用你的code改過
: private void button1_Click(object sender, EventArgs e)
: {
: string[] array2 = new string[] { "A", "B", "C", "D", "E" };
: //^^ 這個矩陣是未知的,我先假設有ABCDE
: int j = 1;
: foreach (string str in array2)//
: {
: foreach (Control childc in this.Controls)
: {
: if (childc is Label)
: {
: if (childc.Name.ToString() !=
: string.Format("label{0}",Convert.ToString(j).Trim())) continue;
: childc.Text = str;
: }
: }
: j++;
: }
: }
: 執行前後變成:http://ppt.cc/rLdA
: 所以假設array2有N個元素(小於12),相對應的label 也會被取代^^
: ※ 引述《aacced (aacced)》之銘言:
: : 不知道是不是你要的
: : private void button1_Click(object sender, EventArgs e)
: : {
: : foreach (Control childc in this.Controls)
: : {
: : if (childc is Label)
: : {
: : if (childc.Name.ToString() != string.Format("label{0}", textBox1.Text.Trim())) continue;
: : childc.Text = textBox2.Text;
: : }
: : }
: : }
: : 附上程式碼:http://cht.tw/h/dvn3l