简体   繁体   English

为什么无法在asp.net中获得正确的dropdownlist值

[英]Why can't get the right value of dropdownlist in asp.net

I have following codes in asp.net: 我在asp.net中有以下代码:

<asp:dropdownlist id="ddlApp" runat="server" />
<asp:button id="btnSmt" runat="server" Text="Submit" />

and code behind: 和代码背后:

    private void btnSmt_Click(object sender, System.EventArgs e)
    {
            lbl.Text = ddlApp.SelectedItem.Value;           
    }

The logic is very simple. 逻辑非常简单。 Get the selected value of dropdownlist and pass it to lbl.text. 获取下拉列表的选定值并将其传递给lbl.text。

But the problem is no matter how I try, the text show the fist value of list in the dropdownlist rather than the selected value. 但问题是无论我如何尝试,文本显示下拉列表中列表的第一个值而不是选定的值。

And I notice that everytime I click the button the page refresh. 而且我注意到每次单击按钮页面都会刷新。

Please help. 请帮忙。

BTW, I have the following event binding: 顺便说一句,我有以下事件绑定:

private void InitializeComponent()
        {    
            this.btnSmt.Click += new System.EventHandler(this.btnSmt_Click);
            this.Load += new System.EventHandler(this.Page_Load);
            this.ddlApp.SelectedIndexChanged +=new System.EventHandler(this.ddlApp_Change);

        }

You have to do the binding for the dropdownlist in 您必须为下拉列表执行绑定

if (!Page.IsPostBack)

Else it will re-build the items for the dropdown list on every postback and therefore only return the currently selected item in the new collection - which is the first. 否则,它将在每个回发时重新构建下拉列表的项目,因此仅返回新集合中当前选定的项目 - 这是第一个。

It also looks like you're missing the btnSmt_Click on the button - but you've probably set it somewhere else... 它看起来你错过了btnSmt_Click上的btnSmt_Click - 但你可能已经将它设置在其他地方......

First of did you debug this??? 你先调试了这个??? Cause the C# code seems corrent. 因为C#代码似乎是正确的。

Try changing this: 尝试改变这个:

<asp:button id="btnSmt" runat="server" Text="Submit" /> 

To

<asp:button id="btnSmt" runat="server" Text="Submit" OnClick="btnSmt_Click" /> 

if this truely is your code your click event would never have been caught, therefor if you would put a breakpoint in your C# code you would have seen that the action is not triggered. 如果这确实是你的代码,你的点击事件永远不会被捕获,因此,如果你在C#代码中放置一个断点,你会看到该动作没有被触发。

Anyway, hope it helps 无论如何,希望它有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM