简体   繁体   中英

User Defined controls on web form

Using VS 2010, C#, .NET 3.5

I have three User Defined Web Controls:

Control 1 has a ListBox and A Button

Control 2 has three Text-Boxes two DropDownLists and three Buttons

Control 3 has only a Table which is populated in code.

I have two pages:

Page 1 has Control 2 and Control 3

Page 2 has Control 1, Control 2, and Control 3

Functionality of Control 2 works perfectly on Page 1.

However, on Page 2 when the submit button is clicked, both DropDownLists ALWAYS show SelectedIndex = 0 and SelectedValue = "0" .

All three Text Boxes and Buttons retain their value on both pages when the Submit Button on Control 2 is clicked. Only the DropDownLists fail to retain their value.

For reference, here is the code in the Submit Button OnClick event:

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        clsLog.WriteLog("TrainingForm.ascx - Submit.");
        tcCategoryError.Text = " ";
        tcDateError.Text = " ";
        tcDescriptionError.Text = " ";
        tcHoursError.Text = " ";
        tcMethodError.Text = " ";
        foreach (Control c in this.Controls)
        {
            LogControls(c);
        }
        c_iTID = Convert.ToInt32(hTID.Value);
        c_szUserName = hUserName.Value;
        bool bValid = true;
        DateTime dtTrainingDate = DateTime.MinValue;
        string szTrainingDescription = "";
        decimal dHours = 0M;
        int iCategoryID = 0;
        int iMethodID = 0;
        if (!DateTime.TryParse(txtTrainingDate.Text, out dtTrainingDate))
        {
            bValid = false;
            tcDateError.Text = "Please Enter Valid Training Date";
        }
        if (!decimal.TryParse(txtTrainingHours.Text, out dHours))
        {
            bValid = false;
            tcHoursError.Text = "Please Enter Valid Training Hours";
        }
        if (this.ddlCategory.SelectedValue == "0")
        {
            bValid = false;
            tcCategoryError.Text = "Please Select Training Category";

        }
        else
            iCategoryID = Convert.ToInt32(this.ddlCategory.SelectedValue);
        if (this.ddlTrainingMethod.SelectedValue == "0")
        {
            bValid = false;
            tcMethodError.Text = "Please Select Training Method";
        }
        else 
            iMethodID = Convert.ToInt32(this.ddlTrainingMethod.SelectedValue);
        if (txtTrainingDescription.Text.Trim() == "")
        {
            bValid = false;
            tcDescriptionError.Text = "Please Enter Training description.";
        }
        else
            szTrainingDescription = txtTrainingDescription.Text.Trim();
        if (bValid)
        {
            clsData.UpdateTraining(c_iTID, "", c_szUserName, dtTrainingDate, szTrainingDescription, iCategoryID, dHours, iMethodID);
            TrainingID = 0;
            ClearForm();
        }
        OnEvent(new MyEventArgs(c_szUserName));

    }

Code to populate DropDowns (part of the User Defined Control)

    protected void BindddlCategory(int iCategoryID)
    {
        DataTable dt = clsData.GetTrainingCategories();
        ddlCategory.Items.Clear();
        ddlCategory.AppendDataBoundItems = true;
        ddlCategory.Items.Add(new ListItem("Select Training Category", "0"));
        ddlCategory.DataSource = dt;
        ddlCategory.DataTextField = "TrainingCategory";
        ddlCategory.DataValueField = "CID";
        ddlCategory.DataBind();
        if (iCategoryID != 0)
            ddlCategory.SelectedValue = iCategoryID.ToString();
    }
    protected void BindddlCategory()
    {
        BindddlCategory(0);
    }
    protected void BindddlTrainingMethod(int iMethodID)
    {
        DataTable dt = clsData.GetTrainingMethods();
        ddlTrainingMethod.Items.Clear();
        ddlTrainingMethod.AppendDataBoundItems = true;
        ddlTrainingMethod.Items.Add(new ListItem("Select Training Method", "0"));
        ddlTrainingMethod.DataSource = dt;
        ddlTrainingMethod.DataTextField = "TrainingCategory";
        ddlTrainingMethod.DataValueField = "CID";
        ddlTrainingMethod.DataBind();
        if (iMethodID != 0)
            ddlTrainingMethod.SelectedValue = iMethodID.ToString();
    }
    protected void BindddlTrainingMethod()
    {
        BindddlTrainingMethod(0);
    }

FYI, the DDLs are NOT populated at Page load but implicitly populated when the event to show the form of the control is fired:

    public void ShowTrainingEntry(int iTrainingID)
    {
        clsLog.WriteLog("TrainingForm.ascx - ShowTrainingEntry(" + iTrainingID.ToString() + ")");
        hTID.Value = iTrainingID.ToString();
        hUserName.Value = UserName;
        int iCategoryID = 0;
        int iMethodID = 0;
        if (iTrainingID != 0)
        {
            DataTable dt = clsData.GetTrainingRecord(iTrainingID);
            if (dt.Rows.Count == 1)
            {
                txtTrainingDate.Text = Convert.ToDateTime(dt.Rows[0]["TrainingDate"]).ToString("MM/dd/yyyy");
                txtTrainingHours.Text = Convert.ToDecimal(dt.Rows[0]["Hours"]).ToString("N1");
                txtTrainingDescription.Text = dt.Rows[0]["TrainingDescription"].ToString();
                int.TryParse(dt.Rows[0]["CategoryCID"].ToString(), out iCategoryID);
                int.TryParse(dt.Rows[0]["MethodCID"].ToString(), out iMethodID);
            }
            ShowChangeMessage(iCategoryID == 0 | iMethodID == 0);
            ShowDeleteButton(true);
            ShowCancelButton(true);
        }
        BindddlCategory(iCategoryID);
        BindddlTrainingMethod(iMethodID);
        tblMain.Visible = true;
    }

Anyone have any ideas on why this is happening?

Thanks, John

If you are adding the UserControls dynamically, you need to make sure they are added no later than Page_Init. This is so the controls are present when the page attempts to set the values from the PostBack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM