0

我有在 Page_Load 时填充的 DropdownList

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable table = new DataTable();


                string connectionString = GetConnectionString();
                string sqlQuery = "select distinct sname from contacts where sname is not null";


                using (OracleConnection conn = new OracleConnection(connectionString))
                {

                    try
                    {
                        conn.Open();

                        using (OracleCommand cmd = new OracleCommand(sqlQuery, conn))
                        {

                            using (OracleDataAdapter ODA = new OracleDataAdapter(cmd))
                            {

                                ODA.Fill(table);

                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write("Not Connected" + ex.ToString());
                    }

                }

                //DropDownList1.DataSource = table;
                //DropDownList1.DataValueField = "";

                DropDownList2.DataSource = table;
                DropDownList2.DataValueField = "sname";
                DropDownList2.DataTextField = "sname";



                DropDownList2.DataBind();
            }
}

现在我正在尝试在 DropDownList 的 Item 更改时填充 GridView

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable table = new DataTable();


            string connectionString = GetConnectionString();
            string val = DropDownList2.SelectedValue;

            string sqlQuery = "SELECT distinct DUTY_DATE FROM DUTY_ROTA,DUTY_TYPES,CONTACTS WHERE DUTY_DATE between SYSDATE and SYSDATE+30  AND DUTY_ROTA.DUTY_TYPE = DUTY_TYPES.DUTY_TYPE AND SNAME IS NOT NULL and contacts.sname = '" + val + "' ORDER BY DUTY_DATE";
            using (OracleConnection conn = new OracleConnection(connectionString))
            {

                try
                {
                    conn.Open();

                    using (OracleCommand cmd = new OracleCommand(sqlQuery, conn))
                    {

                        using (OracleDataAdapter ODA = new OracleDataAdapter(cmd))
                        {

                            ODA.Fill(table);

                        }

                    }
                }
                catch (Exception ex)
                {
                    Response.Write("Not Connected" + ex.ToString());
                }

            }

            GridView1.DataSource = table;
            GridView1.DataBind();

        }

我已启用 AutoPostBack。现在,当我将 DropDownList 项目更改为不同的项目时,页面正在加载但始终保留第一个值。我尝试调试,我发现

string val = DropDownList2.SelectedValue;

val 变量始终是查询返回的第一个值。谁能告诉我我怎么能摆脱这个。每当我在下拉列表中选择任何项目时,我都想填充 GridView。

4

2 回答 2

0

嗨,我认为当您选择其他项目时,下拉列表会再次收费。在页面加载中放置一个断点并查看您的下拉列表是否再次收费,我没有看到其他原因。祝你好运

于 2013-10-28T14:07:23.543 回答
0

您的下拉列表应如下所示:

<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
        </asp:DropDownList>

确保 AutoPostBack 设置为 TRUE。

您可以尝试的另一件事是在索引大于 -1 时选择项目。

      if(DropDownList2.SelectedIndex != -1) 
    {
        string val = DropDownList2.SelectedItem.Value;
//    enter code here
     }
于 2017-10-09T07:55:04.003 回答