2012-11-08 113 views
0

使用VS 2010,C#.NET 3.5用户自定义控制

我有三个用户定义的Web控件:

控制1有一个列表框和一个按钮

控制2有三个Text-Boxes两个DropDownLists和三个Buttons

控件3只有一个代码中填充的表。

我有两个页面:

1具有控制2和控制3

第2页有对照1,对照2,控制3

控制2的功能完美的作品在第1页

但是,在点击提交按钮时,DropDownLists总是显示SelectedIndex = 0SelectedValue = "0"

当单击控件2上的提交按钮时,所有三个文本框和按钮在两个页面上都保持其值。只有DropDownLists无法保留其价值。

供参考,在这里是在提交按钮的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)); 

    } 

代码来填充下拉菜单

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(定义控制用户的一部分)中的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; 
    } 

任何人对此有何想法?

感谢, 约翰

+0

你是如何填充第2页上的DropDownList的?你能告诉我们你在哪里填充组合框的代码吗? – dash

回答

0

如果要动态地添加用户控件的,你需要确保他们不迟比Page_Init添加。这是当页面试图设置PostBack中的值时存在的控件。

+0

我不是。用户控件在设计时放置在页面上(即在.aspx页面而不是aspx.cs页面中)。 – John

+0

您声明:_不在页面加载时填充DDL,但在显示控件形式的事件为fired_时隐式填充DDL。 init值之前是否存在**值?或者在之后是否添加**值? –