English 中文(简体)
我怎么能从编辑中抽取网络控制?
原标题:How can I extract Web Controls from a Dictionary?

页: 1 DropDownList . 当你选择<代码>DropDownList的数值时,该数值改变了相应的<代码>TextBox的背景颜色。

To make this easier, I tried to create a Dictionary<TextBox, DropDownList>, but the Key property is System.Web.UI.WebControls.TextBox and the Value property is System.Web.UI.WebControls.DropDownList, instead of TextBox1 and DropDownList1.

如何通过<代码>Dictionary,适当设定TextBoxDropDownList数值?

namespace TextBoxColorPicker
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            get_box_colors();
        }

        protected void get_box_colors()
        {
            var textbox_dict = new Dictionary<TextBox, DropDownList>();

            textbox_dict.Add(TextBox1,DropDownList1);
            textbox_dict.Add(TextBox2,DropDownList2);
            textbox_dict.Add(TextBox3, DropDownList3);

            foreach (KeyValuePair<TextBox, DropDownList> entry in textbox_dict)
            {
                TextBox txtbox = entry.Key;
                DropDownList list = entry.Value;

                Label1.Text = txtbox.ToString();
                Label2.Text = list.ToString();

                if (list.SelectedValue == "R")
                {
                    txtbox.BackColor = System.Drawing.Color.Red;
                }
                else if (list.SelectedValue == "A")
                {
                    txtbox.BackColor = System.Drawing.Color.Gold;
                }
                else if (list.SelectedValue == "G")
                {
                    txtbox.BackColor = System.Drawing.Color.Lime;
                }
                else
                {
                    txtbox.BackColor = System.Drawing.Color.White;
                }
            }
        }
    }
}
问题回答

从根本上说,这只是关于字典的序号——它指在<条码>上打上<条码>(<>>/条码>,在<条码>上打上<条码>。 系统。 目的仅填上名称。

It s unclear what you re really trying to do, but it sounds like you want the ID property instead of ToString():

Label1.Text = txtbox.ID;
Label2.Text = list.ID;

在个人方面,我可能不想让这些身份证成为国际不动产业联合会的一部分,但这是一个不同的问题。

这些问题似乎是:

Label1.Text = txtbox.ToString();
Label2.Text = list.ToString();

尝试这样做(如果你重新需要价值观,否则就是指Joon Skeet的回答)。

Label1.Text = txtbox.Text;
Label2.Text = list.SelectedValue;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签