는 C#을, .NET 3.5사용자 정의 컨트롤 VS 2010을 사용
나는 세 개의 사용자 정의 웹 컨트롤 한 :
제어 2 세 1은 목록 상자와 단추가
컨트롤을 가지고 Text-Boxes
두 개의 및 세 Buttons
컨트롤 3에는 코드로 채워진 테이블 만 있습니다.
나는 두 페이지가 :
1 페이지를 가지고 페이지 2 제어 1, 컨트롤 2, 및 제어를 가지고 3
제어 2의 기능은 페이지 1에 완벽하게 작동 3
제어 2 및 제어
그러나 제출 단추를 클릭하면 두 DropDownLists 모두 항상 SelectedIndex = 0
및 SelectedValue = "0"
으로 표시됩니다.
컨트롤 3의 제출 단추를 클릭하면 세 개의 텍스트 상자와 단추가 모두 두 페이지에 값을 유지합니다. 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);
}
(제어를 정의 사용자의 일부)하려면 DDLs이되지 않음 페이지로드시 채워 지지만 컨트롤 형식을 나타내는 이벤트가 발생하면 암시 적으로 채워집니다.
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;
}
왜 이런 일이 발생했는지에 대한 의견이 있으십니까? 동적으로 추가 된 UserControls를 추가하는 경우
덕분에, 존
Page 2에서 DropDownList를 어떻게 채우고 있습니까? 콤보 상자를 채우는 코드를 알려주시겠습니까? – dash