DropDownList返回NULL [英] DropDownList returning NULL

查看:78
本文介绍了DropDownList返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DDL,该DDL根据另一个DDL的选择绑定到SQL Server表,并根据选择将文本插入到Label中.问题在于标签要么没有获取数据,要么DDL向标签提供了NULL.请在下面检查我的代码并帮助解决.谢谢.

I have a DDL that is bound to a SQL Server table depending on the selection of another DDL and inserting text into a Label depending on the selection. The problem is that the label is either not getting the data or the DDL are providing NULL to the label. Please check my code below and help resolve. Thank you.

protected void Page_Load(object sender, EventArgs e)
    {
        if(DropDownList1.SelectedIndex > 0)
        BindDropDownList2(DropDownList1.SelectedItem.Text);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
           if (DropDownList1.SelectedIndex == 5)
                if (DropDownList2.SelectedIndex == 3)
                       Label1.Text = "You selected index 5 from ddl1 and index 3 from ddl2";

    }

private void BindDropDownList2(string field)
    {
        if (DropDownList1.SelectedIndex != 5)
        {
            con.ConnectionString = strcon;
            com.Connection = con;
            con.Open();

            string strQuery = @"SELECT " + DropDownList1.SelectedItem.Text + " FROM NUMS";
            da = new SqlDataAdapter(strQuery, con);
            da.Fill(ds);

            DropDownList2.DataSource = ds;
            DropDownList2.DataTextField = field;
            DropDownList2.DataValueField = field;
            DropDownList2.DataBind();

            con.Close();
        }
        else
        {
            DropDownList2.Items.Clear();
            DropDownList2.Items.Add("");
            DropDownList2.Items.Add("A");
            DropDownList2.Items.Add("B");
        }
    }

推荐答案

嗨 在页面 _Load
中使用 isPostBack
Hi use isPostBack in Page_Load
protected void Page_Load(object sender, EventArgs e)
    {
        if(!isPostBack)
        {
            if(DropDownList1.SelectedIndex > 0)
            BindDropDownList2(DropDownList1.SelectedItem.Text);
        }   
    }



谢谢



Thanks,




在页面加载时添加此代码



add this code at page load

if(isPostBack)
    return;




并将ddl autopostback属性设为true




and make the ddl autopostback property true

this will help.


try dis


这篇关于DropDownList返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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