为什么不能在asp.net中获取下拉列表的正确值

时间:2021-03-22 21:11:08

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.

逻辑很简单。获取dropdownlist的选定值,并将其传递给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.

但问题是无论我怎么尝试,文本都显示下拉列表中的list的拳头值,而不是选择的值。

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);

        }

2 个解决方案

#1


7  

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按钮——但是你可能已经把它设置在别的地方了……

#2


0  

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

不管怎样,希望它可以帮助

#1


7  

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按钮——但是你可能已经把它设置在别的地方了……

#2


0  

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

不管怎样,希望它可以帮助