DropDownList not firing OnSelectedIndexChanged

2019-09-08 02:02发布

问题:

ASP.Net code:

<asp:DropDownList ID="ddlKid" runat="server" AutoPostBack="true" CssClass="ddlReports"
    Width="160px" OnSelectedIndexChanged="ddlKid_SelectedIndexChanged">
</asp:DropDownList>

C# code:

protected void ddlKid_SelectedIndexChanged(object sender, EventArgs e)
{
    int i = ddlKid.SelectedIndex;
    count = i;
    int KidId = int.Parse(KidArray[i].ToString());

    ArrayList ADA = new ArrayList();
    ADA.Add(FirstAssignment.SelectedItem);
    ADA.Add(SecondAssignment.SelectedItem);
    DrawTableNew(ADA, KidId);
}

protected void Page_Load(object sender, EventArgs e)
{
    GameLib.Reports.ReportID = 6;
    if (!IsPostBack)
    {
        FillGrades();
        FillKids();
        count = 0;
        Label1.Visible = false;
        FirstAssignment.Visible = false;
        SecondAssignment.Visible = false;
        L1st.Visible = false;
        L2nd.Visible = false;
        Header.Visible = false;
        Label2.Visible = false;
        Div2.Visible = false;
        Panel1.Visible = false;
        DwdTable.Visible = false;
        DwdButton2.Visible = false;
        pnlIssues.Visible = false;
        Panel2.Visible = false;
    }
    else
    {
        FillGrades();
        //  FillKids();
    }
}

protected void FillKids()
{
    ddlKid.Visible = true;
    try
    {
        if (GameUser.UserType == 1)
        {
            ddlKid.Items.Insert(0, new System.Web.UI.WebControls.ListItem(GameUser.UserName, GameUser.UserID.ToString()));
        }
        else
        {
            DataTable dt = new DataTable();
            //dt = GameLib.GameUser.GetKidsForParent(GameUser.UserID);
            dt = GameLib.GameUser.GetKidsForParentAsPerGrade(GameUser.UserID, ddlAssessment.SelectedItem.ToString());
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int kidId = (int)dt.Rows[i]["intUserID"];
                KidArray.Add(kidId);
            }
            if (dt.Rows.Count <= 0)
            {
                //MPEGrade.Show();
            }
            else
            {
                ddlKid.DataSource = dt;
                ddlKid.DataTextField = "vchLoginName";
                ddlKid.DataValueField = "vchGradeName";
                ddlKid.DataBind();
            }
        }
    }
    catch
    {

    }
}

The problem is my ddlKid_SelectedIndexChanged is not executing. After page load it stops at else statement. Its code for fill kids(Adding drop down data). The fill kid only called on the first that is !postback.

回答1:

you can change binding value: fill kids value like:

string vchLoginName=//login name from db;
string vchGradeName=//Grade name from db;
ddlKid.Items.Add(new ListItem(vchLoginName,vchGradeName));