帮助:事件中的ASP.NET动态控件集属性 [英] HELP: ASP.NET Dynamic Control Set Property from Event

查看:65
本文介绍了帮助:事件中的ASP.NET动态控件集属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现,为了使动态控件在回发期间保留其值,必须在Page_Init中声明它们,如下所示:

I''ve recently found out that in order for Dynamic Controls to retain their values during postback, they must be declared in Page_Init as follows:

protected void Page_Init(object sender, EventArgs e)
    {
        DropDownList dd = new DropDownList();
        dd.Items.Add(new ListItem("Item 1"));
        dd.Items.Add(new ListItem("Item 2"));

        dd.AutoPostBack = true;
        panel1.Controls.Add(dd);
    }



由于控件设置为自动回发,因此一旦更改SelectedIndex,它将保留其值.

我现在有另一个问题-我也想创建一个ImageButton,将其CommandArgument更改为下拉菜单中选择的值.但是,此ImageButton也是在Page_Init中动态创建的,因为它们实际上是自动生成的Table的一部分,并且在每一行上.

由于控制事件会在Page_Init之后的某个时间触发,因此以下代码不会按计划更改CommandArgument,而我对如何实现此功能一无所知:



Since the control is set for AutoPostback, it retains it''s value once the SelectedIndex is changed.

I now have another problem - I also want to create an ImageButton which has it''s CommandArgument changed to the value selected in the dropdown. However, this ImageButton is also dynamically created, in Page_Init, since these are actually a part of a Table automatically generated, and are on each row.

Because Control Events fire sometime AFTER Page_Init, the below code does not change the CommandArgument as planned, and I am really clueless on how to make this work:

        protected void Page_Init(object sender, EventArgs e)
{
        DropDownList dd = new DropDownList();
        dd.Items.Add(new ListItem("Item 1"));
        dd.Items.Add(new ListItem("Item 2"));
        dd.AutoPostBack = true;
        panel1.Controls.Add(dd);

        ImageButton imgButton = new ImageButton();
        imgButton.Command += new CommandEventHandler(imgButton_Command);
        imgButton.CommandName = "add";
        imgButton.CommandArgument = dd.SelectedValue;
        panel1.Controls.Add(imgButton);
}

推荐答案

使用DropDownList的Init或Load事件?还是Page_Load?那应该确保dd.SelectedValue是应该的.
Use the DropDownList''s Init or Load event? Or Page_Load? That should ensure that dd.SelectedValue is what it should be.


这篇关于帮助:事件中的ASP.NET动态控件集属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆