下拉列表代码有问题请帮忙吗? [英] Something wrong with dropdown list code please help?

查看:57
本文介绍了下拉列表代码有问题请帮忙吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/*C# code*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dropdown
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        String[] country = new String[] {"--Select Country--","India" };
        String[] State = new String[] { "--Select State--", "Ap","Telangana" };

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.DataSource = country;
                DropDownList1.DataBind();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            int i = DropDownList1.SelectedIndex;
            if (i == 1)
            {
                foreach (String stat in State)
                {
                    DropDownList2.Items.Add(stat);
                }

            }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }



    }
}



/*ASP.net Code*/





我的尝试:



两个控件 - Dropdownlist1(国家)

- Dropdownlist1(国家)



i能够在page_load上加载dropwdownlist1和国家名称()



但是在dropdownlist1_selectedindex改变了......我无法加载第二个下拉列表

i已经尝试了

protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)

{



int i = DropDownList1.SelectedIndex;

if(i == 1)

{

DropDownList2.DataSource =州;



}

Dro pDownList2.DataBind();

}



What I have tried:

Two Controls - Dropdownlist1 (Country)
- Dropdownlist1 (States)

i am able to load dropwdownlist1 with countries names on page_load()

but upon dropdownlist1_selectedindex changed ... i cant load second dropdownlist
i have tried
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

int i = DropDownList1.SelectedIndex;
if (i == 1)
{
DropDownList2.DataSource=State;

}
DropDownList2.DataBind();
}

推荐答案

您好,

您可以使用列出< string>< / string> 而不是 string []

尝试这种方式.. < br $>


Hello ,
You could use List<string></string> instead of string[].
Try this way ..

List<string> State = new List<string>(new String[] { "--Select State--", "Ap","Telangana"});

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        { 
            int i = DropDownList1.SelectedIndex;
            if (i == 1)
            {
              DropDownList2.DataSource = State;
              DropDownList2.DataBind();
            }
        }
</string></string>



谢谢


Thanks


对不起代码工作正常..



我唯一的错误是忘了保持



下拉列表控件属性



Autopostback=True
Sorry the code works fine..

the only mistake i did is forgot to keep

Dropdownlist control property

"Autopostback" = "True"


这篇关于下拉列表代码有问题请帮忙吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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