簡體   English   中英

用戶定義的Web表單控件

[英]User Defined controls on web form

使用VS 2010,C#、. NET 3.5

我有三個用戶定義的Web控件:

控件1有一個列表框和一個按鈕

控件2具有三個Text-Boxes兩個DropDownLists和三個Buttons

控件3僅具有一個用代碼填充的表。

我有兩個頁面:

頁面1具有控件2和控件3

頁面2具有控件1,控件2和控件3

控制2的功能在第1頁上完美工作。

但是,在第2頁上,單擊“提交”按鈕時,兩個DropDownLists都始終顯示SelectedIndex = 0SelectedValue = "0"

單擊控件2上的Submit Button時,所有三個文本框和按鈕在兩個頁面上均保留其值。 僅DropDownLists無法保留其值。

供參考,這是Submit Button OnClick事件中的代碼:

    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));

    }

填充DropDowns的代碼(用戶定義控件的一部分)

    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);
    }

僅供參考,在頁面加載時不會填充DDL,而是在觸發顯示控件形式的事件時隱式填充DDL:

    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;
    }

有人對為什么會這樣有任何想法嗎?

謝謝,約翰

如果要動態添加UserControl,則需要確保不遲於Page_Init添加它們。 這樣,當頁面嘗試從PostBack設置值時,控件就出現了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM