[OK] - 私は混乱しています。それは言語のことかもしれません。これはあなたが達成しようとしているものですが、ドロップダウンリストの代わりにテキストボックスを使用していますか?
フロントエンド:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SO_Web.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="div_Boxes" runat="server">
</div>
</form>
</body>
</html>
バックエンド:
protected void Page_Load(object sender, EventArgs e)
{
createBoxes();
}
string[] boxNames = { "One", "Two", "Three" };
private void createBoxes()
{
int x = 0;
int y = 10;
Panel p = new Panel();
foreach (string name in boxNames)
{
Label l = new Label();
l.ID = "lbl_" + name;
l.Text = "Select Value " + name;
l.Style.Add("float", "left");
DropDownList c = new DropDownList();
c.ID = "cbx_" + name;
c.Items.Add("Select One");
for (int i = 1; i < 101; i++)
{
c.Items.Add(i.ToString());
}
c.SelectedIndex = 0;
c.AutoPostBack = true;
c.Style.Add("display", "block");
c.SelectedIndexChanged += cbx_Changed;
p.Controls.Add(l);
p.Controls.Add(c);
}
Label lbl_Total = new Label();
lbl_Total.Text = "Your Total:";
TextBox txt_Total = new TextBox();
txt_Total.ID = "txt_Total";
txt_Total.Width = 75;
p.Controls.Add(lbl_Total);
p.Controls.Add(txt_Total);
p.Width = 300;
p.Height = 200;
div_Boxes.Controls.Add(p);
}
protected void cbx_Changed(object sender, EventArgs e)
{
bool proceed = true;
int total = 0;
foreach (string name in boxNames)
{
DropDownList c = (DropDownList)findControl(this.Page.Controls,"cbx_" + name);
if (c.SelectedIndex == 0)
{
proceed = false;
}
else
{
total += c.SelectedIndex;
}
}
if (proceed)
{
((TextBox)findControl(this.Page.Controls,"txt_Total")).Text = total.ToString("C2");
}
}
private Control findControl(ControlCollection page, string id)
{
foreach (Control c in page)
{
if (c.ID == id)
{
return c;
}
if (c.HasControls())
{
var res = findControl(c.Controls, id);
if (res != null)
{
return res;
}
}
}
return null;
}
しかし、利用者ならば、あなたはtexboxesを使用して、ユーザーが項目を入力できるようにしたい、とあなたは110のMAX値が欲しいですすべての3つのボックスで110未満の値を入力すると、合計が必要になります。ユーザーが任意のボックスに対して> 110を入力した場合、値を100にリセットしますか?あれは正しいですか?
*決して* Convert.ToInt32()、常にInt32.TryParse()を使用しないでください。ユーザーが常に正しいキーを押すことは期待できません。 txtOne.TextChangedイベントのイベントハンドラでtxtOneを更新することは、他の点では意味がなく、このWebサイトの名前が付けられたクラッシュを引き起こす可能性があります。 –
私に例を教えてください。 –
意味のないスニペットから役に立つ例を書くのは非常に難しいです。盲目的に推測する:TextBoxの代わりにtxtOneをラベルにすることで先に進めてください。他の3つのボックスのTextChangedイベントハンドラを使用すると、合計を計算しラベルを更新するメソッドを呼び出す必要があります。 –