在page_Load中动态填充dropdownlist [英] dynamically populating dropdownlist in page_Load

查看:68
本文介绍了在page_Load中动态填充dropdownlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个下拉列表-

<asp:DropDownList ID="stateList" runat="server" Width ="170px"

                CssClass ="text" AutoPostBack="True"  OnSelectedIndexChanged ="stateList_SelectedIndexChanged">
            <asp:ListItem Value ="Andhra Pradesh">Andhra Pradesh</asp:ListItem>
            <asp:ListItem Value ="Arunachal Pradesh">Arunachal Pradesh</asp:ListItem>
<asp:ListItem Value ="Assam">Assam</asp:ListItem>
<asp:ListItem Value="Bihar">Bihar</asp:ListItem>
<asp:ListItem Value ="Chattisgarh">Chhattisgarh</asp:ListItem>
<asp:ListItem Value ="Goa">Goa</asp:ListItem>
<asp:ListItem Value="Gujarat">Gujarat</asp:ListItem>
<asp:ListItem Value ="Haryana">Haryana</asp:ListItem>


</asp:DropDownList>


</asp:DropDownList>

<asp:DropDownList ID="cityList" runat="server" Width ="135px" CssClass ="text" >
            </asp:DropDownList>



数据库中包含州和城市值.在Page_Load中,需要填充cityList,并且需要选择指定的城市.现在,正在填充cityList,但是当我给出



State and City values are there in the database. In the Page_Load the cityList needs to be populated and the specified city needs to be selected. Now, the cityList is being populated ,but when i give

cityList .Items .FindByText( cityname).Selected =true ;


它返回未设置为对象实例的对象引用.此代码有什么问题?
我已经在里面给出了这段代码


it returns object reference not set to an instance of an object. What is wrong with this code?
I have given this code inside

if(!IsPostBack )
   {}


在此先感谢

默认情况下未选择任何内容.我已通过以下方式填充cityList


Thanks in advance

Nothing is selected by default. I have populated the cityList in the following way

cityList.Items.Add("Agra");
            cityList.Items.Add("Aligarh");

推荐答案

您是否尝试过使用DEBBUGER?在该行上放置一个断点,并检查将变为NULL的情况.当您尝试访问空对象的某些属性时,将发生对象引用错误.

可能是:cityList.Items.FindByText( cityname)为NULL,并且继续尝试使用它的Selected属性.
此外,如果没有的话,这行代码是postback =>.首次加载页面.页面加载时默认情况下是否选择任何内容?我对此表示怀疑,这很可能就是没有得到选定值而导致错误的原因.
Did you tried to use DEBBUGER? Put a break point on this line and check what comes to be NULL. Object reference error happens when you try to access some property of a null object.

Might be: cityList.Items.FindByText( cityname) is NULL and you keep on trying a Selected property of it.
Further, this line of code is if not postback => page load for first time. Is anything selected by default when page loads? I doubt, so most probably thats the reason of not getting any value as selected and thus an error.


cityList.ClearSelection();
cityList .Items .FindByText(cityname).Selected = true;

尝试这个希望能奏效的

谢谢,

阿米特(Amit)
cityList.ClearSelection();
cityList .Items .FindByText( cityname).Selected =true ;

try this hope this will work

thanks,

amit


这篇关于在page_Load中动态填充dropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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