ListViewItem中的DropDownList [英] DropDownList inside ListViewItem
问题描述
问题
我遇到问题 DropDownList
控制 ListView
- 当 DropDownList的选定索引$ c $时c>被更改,
ListViewItem
的 OnItemCommand
事件未被触发。
我不确定我是否正确配置控件,所以任何建议都会受到赞赏。
< b>更多细节
我在 ItemTemplate $中有一个
DropDownList
c $ c>用于 ListView
,在我的aspx中定义。
当用户选择不同的索引时 DropDownList
,我想执行回发并捕获新选择的 DropDownList
索引,以及 ListViewItem
包含所说的 DropDownList
。
为实现这一目标,我我设置的 AutoPostBack
属性 DropDownList
改为true并尝试在 OnItemCommand中捕获
ListView的命令。 ListViewItem
调试显示回发正在发生,但 OnItemCommand
事件没有被触发。
也许值得注意的是我不添加 DropDownList
到服务器上的ItemCreated事件中的 ListView
控件,因为这会导致 DropDownList
到在 ListViewItem
之外渲染,并没有改善问题。我只能假设我这样做是错误的,或者有一些我忽略的东西。
请有人建议吗?
谢谢
Hi,
Problem
I have a problem using a DropDownList
control inside a ListView
- when the selected index of the DropDownList
is changed, the OnItemCommand
event of the ListViewItem
isn't being fired.
I'm not sure whether I'm configuring the controls correctly, so any advice would be appreciated.
More detail
I have a DropDownList
inside of an ItemTemplate
for a ListView
, defined in my aspx.
When the user selects a different index in the DropDownList
, I want to perform a postback and capture the newly chosen DropDownList
index, as well as the ListViewItem
that contains said DropDownList
.
To achieve this, I am setting the AutoPostBack
property of the DropDownList
to "true" and am trying to capture the ListViewItem
in the OnItemCommand
command of the ListView.
Debugging reveals that the postback is occuring, but the OnItemCommand
event isn't being triggered.
It might also be worth noting that I am NOT adding the DropDownList
to the ListView
controls in the ItemCreated event on the server, as this causes the DropDownList
to render outside of the ListViewItem
and doesn't improve the problem. I can only assume I am doing this the wrong way, or there is something I'm overlooking.
Please can anyone advise?
Thanks
推荐答案
抓住 ListView ItemDataBound
Plesae不要复制粘贴,试着理解。
example.aspx
Catch it on ListView ItemDataBound
Plesae dont copy and paste, try to understand.
example.aspx
<asp:listview id="ListView1" runat="server" onitemdatabound="ListView1_ItemDataBound" xmlns:asp="#unknown">
DataSourceID="SqlDataSource1" ItemPlaceholderID="SqlItemContainer" DataKeyNames="cid">
.......................
<itemtemplate>
<asp:dropdownlist id="ddl" runat="server" autopostback="true" onselectedindexchanged="ddl_SelectedIndexChanged">
<asp:listitem text="Select 1" value="One" selected="True">Review</asp:listitem>
<asp:listitem text="Select 2" value="Two">Send Back to Level1</asp:listitem>
</asp:dropdownlist>
</itemtemplate>
<asp:label id="ddlval" text="" runat="server"></asp:label>
....... ....................
example.cs
...........................
example.cs
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
ddlval.Text = ((DropDownList)sender).SelectedValue;
}
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (Page.IsPostBack)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
DropDownList ddl = e.Item.FindControl("ddl") as DropDownList;
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
}
}
你应该抓住SelectedIndexChanging [ ^ ]或SelectedIndexChanged [ ^ ]事件。
You should rather capture SelectedIndexChanging [^]or SelectedIndexChanged [^]event.
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlListFind = (DropDownList)sender;
ListViewItem item1 = (ListViewItem)ddlListFind.NamingContainer;
DropDownList getDDLList = (DropDownList)item1.FindControl("dropdownlist1");
Label lblMessage = (Label)item1.FindControl("lblMsg");
lblMessage.Visible = true; lblMessage.Text = "dropDown text is : " + getDDLList.SelectedItem.Text + " and value is : " + getDDLList.SelectedItem.Value;
}
这篇关于ListViewItem中的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!