下拉菜单C#Asp.Net中的索引没有变化

时间:2017-07-17 12:37:21

标签: c# asp.net indexing drop-down-menu

我有一个DropDownList从数据库填充。当我在索引1上选择一个记录时,它会得到正确的结果,但是当我选择第二个索引时,它会得到相同的索引。看起来DropDownList默认显示First Index。如何在DropDownList中选择更改索引? 因此,如果选择索引3,结果必须来自索引3。 我尝试了DropDownList.SelectedItemDropDownList.SelectedValueDropDownList.SelectedIndexDropDownList.Text,但似乎没有效果。

在Page_Load上加载DropDownList

 public void load_Individual()
    {
        if (IsPostBack == false)
        {
            SqlDataAdapter sdaIndividual = new SqlDataAdapter("select * from tbl_trip", _connectionString);
            DataTable dtIndividual = new DataTable();
            sdaIndividual.Fill(dtIndividual);
            if (dtIndividual.Rows.Count > 0)
            {
                ddl_Individual.DataTextField = "TruckNo";
                ddl_Individual.DataValueField = "TruckNo";
                ddl_Individual.DataSource = dtIndividual;
                ddl_Individual.DataBind();
            } 
        }
    }

用于选择值的DropDownList

if (ddl_Individual.Text != string.Empty)
        {
            ddl_Individual.ClearSelection();
            adp = new SqlDataAdapter(@"Select * , '' as c1, '' as c2, 0 as c3 , 0 as c4 , 0 as c5 , 0 as c6, 0 as c7 , 0 as c8 from tbl_Trip 
            where TruckNo='" + ddl_Individual.SelectedValue + "'", _connectionString);
            adp.Fill(Dt);
            Session["mydata"] = Dt;
            Response.Redirect("LoadReport.aspx");
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(),"alertMessage","aler('select valid value')", true);
        }

1 个答案:

答案 0 :(得分:1)

ddl_Individual.ClearSelection();

此行在您有机会读取用户选择的值之前清除下拉列表中的选择。删除它,因为我认为没有必要。

确保通过检查回发来设置页面加载中的项目:

if(!IsPostBack)   
{
    // Perform binding
} 

如果下拉列表应触发回发,请务必设置AutoPostBack = true;

另外,请查看使用参数化查询以帮助防止SQL注入攻击。无论应用程序的大小如何,这都是一个很好的习惯。