Programmatic Dropdown和SelectedIndexChanged事件 [英] Programmatic Dropdown and SelectedIndexChanged event

查看:80
本文介绍了Programmatic Dropdown和SelectedIndexChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个下拉控件,它是在另一个下拉控件中构建的

SelectedIndexChanged事件


protected void ddlParamType_SelectedIndexChanged(object sender,EventArgs e)

{

//其他一些代码

DropDownList ddlGroups = new DropDownList();

ddlGroups.SelectedIndexChanged + = new

EventHandler(this.ddlGroups_SelectedIndexChanged);

//绑定等

}


当新建构的下拉列表被回发时,SelectedIndexChanged

事件不会调用ddlGroups_SelectedIndexChanged方法。一旦调用

方法,我就不再需要控制了。什么是最佳实践

可以跟随?


谢谢

Andrew

Hi

I have a dropdown control which is constructed in another dropdown control
SelectedIndexChanged event

protected void ddlParamType_SelectedIndexChanged(object sender, EventArgs e)
{
// some other code
DropDownList ddlGroups = new DropDownList();
ddlGroups.SelectedIndexChanged += new
EventHandler(this.ddlGroups_SelectedIndexChanged);
// binding etc.
}

When the new constructed dropdown is posted back the SelectedIndexChanged
event does not call the ddlGroups_SelectedIndexChanged method. Once the
method is called I don''t need the control anymore. What is the best practice
to follow?

Thanks
Andrew

推荐答案

Andrew,


要使ASP.NET激活ddlGroups_SelectedIndexChanged事件,你必须确保动态创建
当页面回发时,DropDownList存在于控件

层次结构中。由于DropDownList仅在第一个DropDownList的SelectedIndexChanged事件中创建

,因此在其他用户操作引起的第二次回发时不会重新创建




要修复它,你可以使用几个选项。


1)使用viewstate变量来标记我们是否需要第二个DropDownList

与否,如果需要,然后在Page_Load中重新创建它。这将确保

事件被正确解雇。


protected void Page_Load(object sender,EventArgs e)

{

if(NeedDropDownList)

{

CreateDropDownList(true);

}

}


protected void CreateDropDownList(bool flag)

{

const string THE_ID =" ddl2" ;;

if(flag)

{

DropDownList ddl2 = new DropDownList();

ddl2.SelectedIndexChanged + = new

EventHandler(ddl2_SelectedIndexChanged);

ddl2.ID = THE_ID;

ddl2.AutoPostBack = true;

ddl2 .Items.Add(new ListItem(" 1"," value1"));

ddl2.Items.Add(new ListItem(" 2"," value2")); < br $>
form1.Controls.Add(ddl2);

}

else

{

form1.Controls.Remove(form1.FindControl(THE_ID));

}

NeedDropDownList = flag;

}


void ddl2_SelectedIndexChanged(object sender,EventArgs e)

{

DropDownList ddl2 =发送者为DropDownList;

Response.Write (ddl2.SelectedValue);

CreateDropDownList(false);

}


protected bool NeedDropDownList

{

get

{

object o = ViewState [" A"];

if(o == null)返回false;

return(bool)o;

}

set {ViewState [" A"] = value; }

}


protected void DropDownList1_SelectedIndexChanged(对象发送者,

EventArgs e)

{

CreateDropDownList(true);

}

2)因为在这种情况下,DropDownList的SelectedIndexChanged事件是

简单,所有你想要的是所选项目的文字或价值,因此我

认为你根本不需要这个活动,只需检查Request.Form

收集,看看它是否存在。


protected void Page_Load(object sender,EventArgs e)

{

if(IsPostBack)

{

object o = Request.Form [" ddl2"];

if(o!= null )

{

Response.Write(o);

}

}

}


protected void DropDownList1_SelectedIndexChanged(对象发件人,

EventArgs e)

{

DropDownList ddl2 = new DropDownList();

ddl2.ID =" ddl2";

ddl2.AutoPostBack = true;

ddl2.Items.Add(new ListItem(" 1"," value1"));

ddl2.Items.Add(new ListItem(" 2",value2"));

form1.Controls.Add(ddl2);

}

请注意第二种方法与第一种方法有一个区别:如果回发是由其他控件引起的(例如:

按钮'的点击) dropdownlist是动态创建的,在
Page_Load中,你仍会在Request.Form中找到ddl2'的值,无论

是否所选索引都被更改。在第一种方法中,第二个下拉列表的

SelectedIndexChanged事件仅在

索引真正更改时触发。

希望这会有所帮助。< br $>
此致,

Walter Wang(wa****@online.microsoft.com,删除''在线''')

Microsoft Online社区支持


==================================== ==============

通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。如果您使用的是Outlook Express,请确保清除

复选框工具/选项/读取:一次获取300个标题及时看到你的回复




注意:MSDN托管新闻组支持服务是针对非紧急问题

其中来自社区或Microsoft支持的初步响应

工程师可在1个工作日内完成。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx

======================================== ==========


此帖子按原样提供。没有保证,也没有授予任何权利。

Hi Andrew,

To make ASP.NET fire the ddlGroups_SelectedIndexChanged event, you have to
make sure the dynamically created DropDownList exist in the control
hierarchy when the page is postback. Since the DropDownList is only created
in the first DropDownList''s SelectedIndexChanged event, it''s not re-created
upon the second postback caused by other user action.

To fix it, you can use several options.

1) Use a viewstate variable to flag whether we need the second DropDownList
or not, then re-create it in Page_Load if needed. This will make sure the
event get''s fired correctly.

protected void Page_Load(object sender, EventArgs e)
{
if (NeedDropDownList)
{
CreateDropDownList(true);
}
}

protected void CreateDropDownList(bool flag)
{
const string THE_ID = "ddl2";
if (flag)
{
DropDownList ddl2 = new DropDownList();
ddl2.SelectedIndexChanged += new
EventHandler(ddl2_SelectedIndexChanged);
ddl2.ID = THE_ID;
ddl2.AutoPostBack = true;
ddl2.Items.Add(new ListItem("1", "value1"));
ddl2.Items.Add(new ListItem("2", "value2"));
form1.Controls.Add(ddl2);
}
else
{
form1.Controls.Remove(form1.FindControl(THE_ID));
}
NeedDropDownList = flag;
}

void ddl2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl2 = sender as DropDownList;
Response.Write(ddl2.SelectedValue);
CreateDropDownList(false);
}

protected bool NeedDropDownList
{
get
{
object o = ViewState["A"];
if (o == null) return false;
return (bool)o;
}
set { ViewState["A"] = value; }
}

protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
CreateDropDownList(true);
}
2) Since in this case the DropDownList''s SelectedIndexChanged event is
simple, all you wanted is the selected item''s text or value, therefore I
think you don''t need the event at all, just check the Request.Form
collection to see if it''s there.

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
object o = Request.Form["ddl2"];
if (o != null)
{
Response.Write(o);
}
}
}

protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
DropDownList ddl2 = new DropDownList();
ddl2.ID = "ddl2";
ddl2.AutoPostBack = true;
ddl2.Items.Add(new ListItem("1", "value1"));
ddl2.Items.Add(new ListItem("2", "value2"));
form1.Controls.Add(ddl2);
}
Please note this second approach do have one difference with the first
approach: if the postback is caused by other controls (for example: a
button''s click) after the second dropdownlist is created dynamically, in
Page_Load you will still find the ddl2''s value in Request.Form regardless
if the selected index is changed or not. In the first approach, the
SelectedIndexChanged event of the second dropdownlist only fires when the
index is really changed.
Hope this helps.
Sincerely,
Walter Wang (wa****@online.microsoft.com, remove ''online.'')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


嗨Walter,谢谢你这个


我认为第一个例子看起来更好。我的DropDownList被添加到

RepeaterItem。每次回发一次只能有一个。如果我在Page_Load事件中创建了控件
那么我不知道哪个RepeaterItem

需要我认为的控件。我可以在Repeater ItemCreated事件中添加它吗?

如果是这样我应该怎么做?我在解决如何在事件之间共享信息以及如何在处理时找到控件时遇到很多问题

很多控件和嵌套转发器以及页面有哪些主页,

页眉和页脚等。你能否指点我一些有用的文章和

文档如果你认为它会对我有帮助吗?

谢谢

Andrew


" Walter Wang [MSFT]" < wa **** @ online.microsoft.com写信息

新闻:Z7 ************** @ TK2MSFTNGHUB02.phx.gbl ...
Hi Walter, thanks for this

I think the first example looks better. My DropDownList is added to a
RepeaterItem. There can only be one at a time for each postback. If I
created the control in the Page_Load event then I don''t which RepeaterItem
needs the control I think. Can I add it in the Repeater ItemCreated event?
If so How should I do this? I have a lot of problems working out how to
share information between events and how to find controls when dealing with
Lots of controls and nested repeaters and where the page has a master page,
header and footer etc. Can you also point me to some useful articles and
documentation if you think it will help me?

Thanks
Andrew

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:Z7**************@TK2MSFTNGHUB02.phx.gbl...

嗨Andrew,


要使ASP.NET激活ddlGroups_SelectedIndexChanged事件,你必须

当页面回发时,确保动态创建的DropDownList存在于控件

层次结构中。由于DropDownList仅在第一个DropDownList的SelectedIndexChanged事件中创建了

,因此它不会重新创建 $ b />
由于其他用户操作引起的第二次回发。


要修复它,您可以使用多个选项。


1)使用viewstate变量标记我们是否需要第二个

DropDownList

,然后在需要时在Page_Load中重新创建它。这将确保

事件被正确解雇。


protected void Page_Load(object sender,EventArgs e)

{

if(NeedDropDownList)

{

CreateDropDownList(true);

}

}


protected void CreateDropDownList(bool flag)

{

const string THE_ID =" ddl2" ;;

if(flag)

{

DropDownList ddl2 = new DropDownList();

ddl2.SelectedIndexChanged + = new

EventHandler(ddl2_SelectedIndexChanged);

ddl2.ID = THE_ID;

ddl2.AutoPostBack = true;

ddl2 .Items.Add(new ListItem(" 1"," value1"));

ddl2.Items.Add(new ListItem(" 2"," value2")); < br $>
form1.Controls.Add(ddl2);

}

否则

{

form1.Controls.Remove(form1.FindControl(THE_ID));

}

NeedDropDownList = flag;

}


void ddl2_SelectedIndexChanged(object sender,EventArgs e)

{

DropDownList ddl2 =发送者为DropDownList;

Response.Write(ddl2.SelectedValue);

CreateDropDownList(false);

}


protected bool NeedDropDownList

{

get

{

object o = ViewState ["];

如果(o == null)返回false;

return(bool)o;

}

set {ViewState [" A"] = value; }

}


protected void DropDownList1_SelectedIndexChanged(对象发送者,

EventArgs e)

{

CreateDropDownList(true);

}


2)因为在这种情况下,DropDownList的SelectedIndexChanged事件是

简单,你想要的只是所选项目的文字或值,因此我想b / b $ b认为你根本不需要这个活动,只需检查Request.Form

集合,看看它是否存在。


protected void Page_Load(object sender,EventArgs e)

{

if(IsPostBack)

{

object o = Request.Form [" ddl2"];

if( o!= null)

{

Response.Write(o);

}

}

}


protected void DropDownList1_SelectedIndexChanged(object sender,

EventArgs e)

{

DropDownList ddl2 = new DropDownList();

ddl2.ID =" ddl2";

ddl2 .AutoPostBack = true;

ddl2.Items.Add(new ListItem(" 1"," value1"));

ddl2.Items.Add(new ListItem (" 2"" value2"));

form1.Controls.Add(ddl2);

}


请注意第二种方法与第一种

方法有一点不同:如果回发是由其他控件引起的(例如:

按钮'的点击)在动态创建第二个下拉列表后,在
Page_Load中,如果所选索引发生变化,你仍然可以在Request.Form中找到ddl2'的值,无论

。在第一种方法中,第二个下拉列表的

SelectedIndexChanged事件仅在

索引真正更改时触发。


希望这有帮助。


此致,
Walter Wang(wa****@online.microsoft.com,删除''在线。'')

Microsoft在线社区支持


============================ ======================

通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。如果您使用的是Outlook Express,请确保清除

复选框工具/选项/读取:一次获取300个标题看到你的

回复

及时。


注意:MSDN管理新闻组支持服务是针对非紧急问题的/>
,可以在1个工作日内收到社区或Microsoft支持工程师的初步回复。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx

======================================== ==========


此帖子按原样提供。没有保证,并且不授予

权利。
Hi Andrew,

To make ASP.NET fire the ddlGroups_SelectedIndexChanged event, you have to
make sure the dynamically created DropDownList exist in the control
hierarchy when the page is postback. Since the DropDownList is only
created
in the first DropDownList''s SelectedIndexChanged event, it''s not
re-created
upon the second postback caused by other user action.

To fix it, you can use several options.

1) Use a viewstate variable to flag whether we need the second
DropDownList
or not, then re-create it in Page_Load if needed. This will make sure the
event get''s fired correctly.

protected void Page_Load(object sender, EventArgs e)
{
if (NeedDropDownList)
{
CreateDropDownList(true);
}
}

protected void CreateDropDownList(bool flag)
{
const string THE_ID = "ddl2";
if (flag)
{
DropDownList ddl2 = new DropDownList();
ddl2.SelectedIndexChanged += new
EventHandler(ddl2_SelectedIndexChanged);
ddl2.ID = THE_ID;
ddl2.AutoPostBack = true;
ddl2.Items.Add(new ListItem("1", "value1"));
ddl2.Items.Add(new ListItem("2", "value2"));
form1.Controls.Add(ddl2);
}
else
{
form1.Controls.Remove(form1.FindControl(THE_ID));
}
NeedDropDownList = flag;
}

void ddl2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl2 = sender as DropDownList;
Response.Write(ddl2.SelectedValue);
CreateDropDownList(false);
}

protected bool NeedDropDownList
{
get
{
object o = ViewState["A"];
if (o == null) return false;
return (bool)o;
}
set { ViewState["A"] = value; }
}

protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
CreateDropDownList(true);
}
2) Since in this case the DropDownList''s SelectedIndexChanged event is
simple, all you wanted is the selected item''s text or value, therefore I
think you don''t need the event at all, just check the Request.Form
collection to see if it''s there.

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
object o = Request.Form["ddl2"];
if (o != null)
{
Response.Write(o);
}
}
}

protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
DropDownList ddl2 = new DropDownList();
ddl2.ID = "ddl2";
ddl2.AutoPostBack = true;
ddl2.Items.Add(new ListItem("1", "value1"));
ddl2.Items.Add(new ListItem("2", "value2"));
form1.Controls.Add(ddl2);
}
Please note this second approach do have one difference with the first
approach: if the postback is caused by other controls (for example: a
button''s click) after the second dropdownlist is created dynamically, in
Page_Load you will still find the ddl2''s value in Request.Form regardless
if the selected index is changed or not. In the first approach, the
SelectedIndexChanged event of the second dropdownlist only fires when the
index is really changed.
Hope this helps.
Sincerely,
Walter Wang (wa****@online.microsoft.com, remove ''online.'')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your
reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.



安德鲁,

您能告诉我更多关于您的具体要求或设计的信息吗?

这样我可以提供更具体的建议。谢谢。


问候,

Walter Wang(wa****@online.microsoft.com,删除''在线。'')

Microsoft在线社区支持


============================= =====================

在回复帖子时,请回复群组通过你的新闻阅读器

其他人可以从你的问题中学习并从中受益。

==================== ==============================

此帖子提供按现状 ;没有保证,也没有授予任何权利。

Hi Andrew,

Would you please tell me more about your specific requirement or design?
This way I can provide more specific suggestion. Thanks.

Regards,
Walter Wang (wa****@online.microsoft.com, remove ''online.'')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


这篇关于Programmatic Dropdown和SelectedIndexChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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