无法在IE浏览器中获取列表框选定的值 [英] Unable to get Listbox selected value in IE browser

查看:181
本文介绍了无法在IE浏览器中获取列表框选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已经在代码中添加了列表框

Hello all,

I have added listbox in my code

<asp:ListBox ID="LststateList" runat="server" Width="180px" SelectionMode="Single"

 Height="350px"CssClass="textbox3"></asp:ListBox>



和Linkbutton的onclick事件,我正在验证列表框是否选择了任何值,以下是我的linkbutton



and onclick event of Linkbutton i am verifying that listbox have selected any value or not below is my linkbutton

<asp:LinkButton ID="lnkgo" runat="server" OnClick="Go_Click" Text="Go to this page"></asp:LinkButton>





<pre lang="cs">protected void Go_Click(object sender, EventArgs e)
        {
string sb = string.Empty;
           for (int i = 0; i < LststateList.Items.Count; i++)
            {
                if (LststateList.Items[i].Selected == true)
                {
                    sb = Convert.ToString(LststateList.Items[i].Value);
                }
            }
            if (string.IsNullOrEmpty(sb))
            {
               -----            }}




这里的问题是,当我从列表框中选择任何值并单击链接按钮时,列表框将始终显示所选值= false,
我不知道发生这种情况的原因,而且这种情况仅在IE中出现,

还有我在




here problem is the,when i select any value from list box and click on linkbutton then Listbox always display selected value=false,
I dont know reason why this happend,and its occured in IE Only,

and one thing that I have added all above controls in

<div></div> 

标签.

tag.

help me about that.

推荐答案

您的代码似乎还可以.

您有其他问题.这是页面回发.
发生什么情况,当您单击服务器按钮时,将发生页面回发,并且我确定您在page_load事件中具有一些加载列表框的代码.

试试这个
Your code seems perfectly okay.

You have a different problem. It is page postback.
What happens, when you click a server button, the page postback occurs and I am sure you have some code of loading list box in page_load event.

Try this
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
          //fill up your listbox here
    }
}



这会起作用.

欢呼



This will work.

cheers



请尝试

Hi,
Please try

if (LststateList.Items[i].Selected == true)
                {
                    sb = LststateList.Items[i].Value.ToString();
                }



对我有用.

谢谢.



It is working for me.

Thank you.


我同意Sandip,您的代码很好.

在ASP.NET页面生命周期中,将首先执行Page_Load,然后单击事件按钮.

因此,您需要验证是否添加条件if(!Page.IsPostBack).

I agree to Sandip, your code is fine.

In ASP.NET Page Life Cycle, the Page_Load will be executed first and then the event button click.

So you need to validate adding the condition if(!Page.IsPostBack).

if (!IsPostBack)
{
    var query = your query;

    ListBox1.DataSource = query;
    ListBox1.DataBind();
}


这篇关于无法在IE浏览器中获取列表框选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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