DropDownList没有触发OnSelectedIndexChanged

时间:2014-01-29 05:58:16

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

ASP.Net代码:

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

C#代码:

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
    {

    }
}

问题是我的ddlKid_SelectedIndexChanged没有执行。页面加载后,它会在else语句处停止。它的填充代码(添加下拉数据)。  填充孩子只调用了第一个!回发。

1 个答案:

答案 0 :(得分:0)

您可以更改绑定值: 填写孩子的价值如:

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