asp.net dropdownlist首选不会触发SelectedIndexChanged事件

时间:2017-01-23 15:00:17

标签: c# asp.net visual-studio visual-studio-2010

我创建了一个下拉列表,并尝试点击第一个选择,但没有触发SelectedIndexChanged事件。但是,对于下拉列表中的其他选项,它完全正常。

以下是我的代码:

 public partial class _Default : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
{
    string member = (String)Session["ssmem"];
    if (!IsPostBack)
    {
        if (Session["ssmem"] == null)
            Response.Redirect("LoginforAccess.aspx");

        else
        {
            Response.ClearHeaders();
            Response.AddHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
            Response.AddHeader("Pragma", "no-cache");

           //if go back then must log in --2
        }

        //Dropdownlist
        string strConnectionString =
                  ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

        //STEP1 : Define a connection to Database 
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strcommand = "Select Username from [User] WHERE (UsergroupID = (SELECT UsergroupID FROM [User] AS User_1 WHERE (Username = @user)))";

        SqlCommand cmd = new SqlCommand(strcommand, myConnect);
        // cmd.Parameters.AddWithValue("@usergrp", groupid);
        cmd.Parameters.AddWithValue("@user", member);

        myConnect.Open();
        SqlDataReader reader = cmd.ExecuteReader();


        DropDownList1.DataSource = reader;
        DropDownList1.DataTextField = "Username";
        DropDownList1.DataValueField = "Username";
        DropDownList1.DataBind();

        reader.Close();
        myConnect.Close();
    }



}

  protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
    if (Session["ssmem"] != null)
        MasterPageFile = "User.master";
    //change the master page --1
}
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    //chosen name to display stats
    string choseuser = "";
    choseuser = DropDownList1.SelectedItem.Text;
    Response.Redirect("Substats.aspx?choseuser=" + choseuser);
}
 }

下拉列表的来源视图:

  <asp:DropDownList ID="DropDownList1" runat="server" Width="200px" Height="35px" 
    AutoPostBack="True" 
    onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
    ViewStateMode="Enabled">
</asp:DropDownList>

1 个答案:

答案 0 :(得分:0)

We have to add first item as title to the dropdownlist,
and instead of using datasource used dr.Read() in while loop for adding the items on dropdownlist.

This problem occurs because in dropdownlist first item is selected bydefault and it is not reflected on selection  


<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" ViewStateMode="Enabled">
            <asp:ListItem>Select College</asp:ListItem> 
<!--Add one item as title into dropdownlist in edit item of dropdownlist-->
        </asp:DropDownList>

 
protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            Response.Write("hello");
            try
            {
                string sel = "select name from websites";
                SqlCommand cmd = new SqlCommand(sel,con);
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
               /* DropDownList1.DataSource = dr;
                DropDownList1.DataTextField = "name";
                DropDownList1.DataValueField = "name";
                DropDownList1.DataBind();*/
                while (dr.Read())//used dr.read() instead of datasource
                {
                    DropDownList1.Items.Add(dr["name"].ToString());
                }
            }
            catch(Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
    }


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            SqlCommand cmd = new SqlCommand("select url from websites where name = @itm", con);
            cmd.Parameters.AddWithValue("@itm", DropDownList1.SelectedItem.Text);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            if (dr.HasRows)
            {
                Response.Redirect(dr["url"].ToString());
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }