DDL SelectedIndexChanged()不会在第一个ListItem(ASP.Net C#)上触发 [英] DDL SelectedIndexChanged() does not fire on first ListItem (ASP.Net C#)

查看:61
本文介绍了DDL SelectedIndexChanged()不会在第一个ListItem(ASP.Net C#)上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的模板化用户控件,目的是在我们的Web应用程序中使用一个标准的容器",但是允许您在其中使用任何控件–非常类似于GridView/DataGrid的模板"列.我遇到的问题是,当我在自己的自定义控件中放置一个DropDownList控件时,该DDL的SelectedIndexChanged方法并不总是会触发,我对为什么感到困惑.

我使用自定义控件和一个DDL创建了一个小测试页,其中包含一些硬编码值,以重现该问题:

I created a custom Templated User Control, for the purpose of having a standard "container" to use around our web app, but allow whatever controls you want inside of it -- pretty much like a Template Column of a GridView/DataGrid. Problem I''m having is that when I place a DropDownList control inside my custom control, the SelectedIndexChanged method of that DDL doesn''t always fire, and I am stumped as to why.

I created a small test page with my custom control and a single DDL, which has some hard coded values, to replicate the problem:

<uc1:ExpandCollapseRegion ID="xpcColor"  runat="server" Title="Pick a Color" AlwaysOpen="true">
    <layouttemplate>
        
        <table>
            <tr>
                <td>Color: </td>
                <td>
                    <asp:DropDownList ID="ddlColor" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlColor_SelectedIndexChanged">
                        <asp:ListItem Text="" Value="" />
                        <asp:ListItem Text="Red" Value="1" />
                        <asp:ListItem Text="Orange" Value="2" />
                        <asp:ListItem Text="Yellow" Value="3" />
                        <asp:ListItem Text="Green" Value="4" />
                        <asp:ListItem Text="Blue" Value="5" />
                        <asp:ListItem Text="Indigo" Value="6" />
                        <asp:ListItem Text="Violet" Value="7" />
                    
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblColorChoice" runat="server" />
                </td>
            </tr>
        </table>
        
    </layouttemplate>



后面的代码如下所示:



The code-behind looks like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        ((Label)xpcColor.FindControl("lblColorChoice")).Text = "";
    }
}
protected void ddlColor_SelectedIndexChanged(object sender, EventArgs e)
{
    using (DropDownList ddlColor = ((DropDownList)xpcColor.FindControl("ddlColor")))
    {
        if (!String.IsNullOrEmpty(ddlColor.SelectedValue))
        {
            ((Label)xpcColor.FindControl("lblColorChoice")).Text = "You chose the color " + ddlColor.SelectedItem.Text;
        }
        else
        {
            ((Label)xpcColor.FindControl("lblColorChoice")).Text = "";
        }
    }
}



应该在这里发生的所有事情是在标签中显示选择了哪种颜色,但是如果没有选择任何颜色,则只需清除标签即可.很简单,不过没什么大不了的....

100%的时间,当我选择一种颜色时,SelectedIndexChanged方法将触发,并且Label控件将使用文本进行更新.我可以选择一种颜色,然后再选择另一种颜色,依此类推,这种方法效果很好.但是,如果在选择颜色之后,我选择了DDL的空白项,那么"SelectedIndexChanged"方法永远不会触发.

我想看看这是否与所选值有关,因此我在空白选项之前添加了一个新的ListItem(将空白ListItem用作第二个选项):



All that is supposed to happen here is to show in the Label what color was picked, but if no color was picked to then just clear the label. Very simple, no biggie, however....

100% of the time, when I pick a color the SelectedIndexChanged method fires, and the Label control is updated with the text. I can pick one color, the another, then another, and so forth over and over and the thing works great. However, if after choosing a color, I select the blank item of the DDL, the SelectedIndexChanged method *does not* fire, ever.

I wanted to see if this had something to do with the value being selected, so I added a new ListItem before the blank one (making the blank ListItem the second option):

<asp:ListItem Text="White" Value="0" />



现在,当我运行页面时,我可以选择一种颜色,更新标签,选择空白的颜色,然后清除标签,但是如果我选择白色",则该页面会执行PostBack,但再次使用SelectedIndexChanged方法*不会*火.

我以前从未遇到过这个问题,并承认我对原因有些困惑.
问题可能出在我的自定义控件中,但我犹豫不决,因为DDL对于所有选择(第一个选择除外)都可以正确运行.此外,DDL选项以及Label文本都可以保留在PostBack中,所以我也不知道这也是ViewState问题.


我仍然在Googl上,但是我在这里很困惑.如果其他任何人看到了这个问题,碰到了这个问题,或者可能输入了一些可能的解决方法,那么我将不知所措.非常感谢.

-Andrew



Now when I run the page, I can choose a color, the label is updated, choose the blank one and the label is cleared, but if I select "White", the page does a PostBack yet the SelectedIndexChanged method once again *does not* fire.

I have never run into this before and admit I am a bit stumped as to the cause.
The problem may well be in my custom control, but I am hesitant to think so as the DDL functions correctly for all selections, except for the first one. Also the DDL choice as well as the Label text survives a PostBack, so I am not sure this is a ViewState issue either.


I''m still Googl''ing this, but I am pretty much stumped here what''s going on. If anyone else has seen this, run across this, or may have some input of possible fixes, I am all ears. Much appreciated.

-- Andrew

推荐答案

@mahabubur rahman:

抱歉,您的建议已被接受.事实证明,using()语句是完全"问题.我可以通过将代码更改为以下内容来解决该问题:

@mahabubur rahman:

My apologies, your suggestion was spot on. It turned out that the using() statement was **exactly** the problem. I was able to solve the problem by changing the code to the following:

protected void ddlColor_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddlColor = (DropDownList)sender;
    if (!String.IsNullOrEmpty(ddlColor.SelectedValue))
    {
        ((Label)xpcColor.FindControl("lblColorChoice")).Text = "You chose the color " + ddlColor.SelectedItem.Text;
    }
    else
    {
        ((Label)xpcColor.FindControl("lblColorChoice")).Text = "";
    }
}




非常感谢!

-Andrew




Thank you very much!

-- Andrew


protected void ddlColor_SelectedIndexChanged(object sender, EventArgs e)
        {
           if (!String.IsNullOrEmpty(ddlColor.SelectedValue))
           {
             ((Label)xpcColor.FindControl("lblColorChoice")).Text = "You chose the color " + ddlColor.SelectedItem.Text;
           }
           else
           {
             ((Label)xpcColor.FindControl("lblColorChoice")).Text = "";
           }
            
        }




试试这个代码,让我知道结果.




Try this code and let me know the result.


@mahabubur rahman:

只需删除using (DropDownList ddlColor = ((DropDownList)xpcColor.FindControl("ddlColor")))语句就不会对我所述的问题产生任何影响.实际上,这样做实际上会带来失败的后果:SelectedIndexChanged()事件是在DropDownList控件的HTML Source中设置的,但是因为DDL在LayoutTemplate内,所以FindControl() 必须用于访问DDL属性和值的方法.

请再次查看我发布的内容是这里的问题.在先前选择之后选择DropDownList控件的 first 列表项时,SelectedIndexChanged()处理程序方法不会触发.我可以在该方法上放置一个BreakPoint,并且每次选择第一个ListItem时选择 except 时都会命中.

那就是问题所在,这就是让我感到困惑的原因.
-安德鲁
@mahabubur rahman:

Simply removing the the using (DropDownList ddlColor = ((DropDownList)xpcColor.FindControl("ddlColor"))) statement will have absolutely no effect on the problem I stated. Actually, doing this would in fact have the effect of introducing a failure: the SelectedIndexChanged() event is set in the HTML Source of the DropDownList control, yet because that DDL is inside the LayoutTemplate the FindControl() must be used method to access the DDL properties and values.

Please check out again what I was posted is the problem here. The SelectedIndexChanged() handler method does not fire when selecting the first ListItem of the DropDownList control after a previous choice. I can place a BreakPoint on the method and it hits every time I make a choice except when choosing the first ListItem.

That''s the problem, and that is what''s got me stumped.

-- Andrew


这篇关于DDL SelectedIndexChanged()不会在第一个ListItem(ASP.Net C#)上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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