我必须动态创建控件并将它们添加到表中。在按钮单击中,我需要找到为该控件输入的值。控件ID也是动态的。以下是我试过的代码。
private void createUdfControls()
{
colUdfNames.Clear();//this is a collection object.
//the from id for gl is 3.
colUdfNames = BL_GeneralLedger.GetAllUdfNames(3);
if (colUdfNames.Count>0)
{
TableRow tr1 = null; ;
for(int i=0;i<colUdfNames.Count;i++)
{
TableRow tr;
if(i%2==0)
{
tr = new TableRow();
tr1 = tr;
}
string udfTypeValue = colUdfNames[i].UdfTypeValue;
int EnumUdfTypeId = colUdfNames[i].Enum_UdfTypeId;
TableCell cellUdf1 = new TableCell();
TableCell cellUdfValue1 = new TableCell();
TableCell cellUdf2 = new TableCell();
TableCell cellUdfValue2 = new TableCell();
switch (EnumUdfTypeId)
{
case 1:
{
//Text
if (i % 2 == 0)
{
cellUdf1.Text = colUdfNames[i].UdfName;
TextBox txtText = new TextBox();
txtText.ID = "txtText" + colUdfNames[i].UdfId;
txtText.MaxLength = colUdfNames[i].Width;
txtText.Text = "ww";
cellUdfValue1.Controls.Add(txtText);
}
else
{
cellUdf2.Text = colUdfNames[i].UdfName;
TextBox txtText = new TextBox();
txtText.ID = "txtText" + colUdfNames[i].UdfId;
txtText.MaxLength = colUdfNames[i].Width;
txtText.Text = "ww";
cellUdfValue2.Controls.Add(txtText);
}
break;
}
case 2:
{
//Number
if(i%2==0)
{
cellUdf1.Text = colUdfNames[i].UdfName;
TextBox txtNumber = new TextBox();
txtNumber.ID = "txtNumber" + colUdfNames[i].UdfId;
txtNumber.MaxLength = colUdfNames[i].Width;
txtNumber.Text = "12";
cellUdfValue1.Controls.Add(txtNumber);
DataRow dr = dtUdfControlInfo.NewRow();
dr[0] = txtNumber.ID;
dtUdfControlInfo.Rows.Add(dr);
}
else
{
cellUdf2.Text = colUdfNames[i].UdfName;
TextBox txtNumber = new TextBox();
txtNumber.ID = "txtNumber" + colUdfNames[i].UdfId;
txtNumber.MaxLength = colUdfNames[i].Width;
txtNumber.Text = "12";
cellUdfValue2.Controls.Add(txtNumber);
DataRow dr = dtUdfControlInfo.NewRow();
dr[0] = txtNumber.ID;
dtUdfControlInfo.Rows.Add(dr);
}
break;
}
case 3:
{
//Decimal
if(i%2==0)
{
cellUdf1.Text = colUdfNames[i].UdfName;
TextBox txtDecimal = new TextBox();
txtDecimal.ID = "txtDecimal" + colUdfNames[i].UdfId;
txtDecimal.MaxLength = colUdfNames[i].Width;
txtDecimal.Text = "2.2";
cellUdfValue1.Controls.Add(txtDecimal);
}
else
{
cellUdf2.Text = colUdfNames[i].UdfName;
TextBox txtDecimal = new TextBox();
txtDecimal.ID = "txtDecimal" + colUdfNames[i].UdfId;
txtDecimal.MaxLength = colUdfNames[i].Width;
txtDecimal.Text = "2.2";
cellUdfValue2.Controls.Add(txtDecimal);
}
break;
}
case 4:
{
//Memo
if(i%2==0)
{
cellUdf1.Text = colUdfNames[i].UdfName;
TextBox txtMemo = new TextBox();
txtMemo.TextMode = TextBoxMode.MultiLine;
txtMemo.ID = "txtMemo" + colUdfNames[i].UdfId;
txtMemo.MaxLength = colUdfNames[i].Width;
txtMemo.Text = "memo";
cellUdfValue1.Controls.Add(txtMemo);
DataRow dr = dtUdfControlInfo.NewRow();
dr[0] = txtMemo.ID;
dtUdfControlInfo.Rows.Add(dr);
}
else
{
cellUdf2.Text = colUdfNames[i].UdfName;
TextBox txtMemo = new TextBox();
txtMemo.TextMode = TextBoxMode.MultiLine;
txtMemo.ID = "txtMemo" + colUdfNames[i].UdfId;
txtMemo.MaxLength = colUdfNames[i].Width;
txtMemo.Text = "memo";
cellUdfValue2.Controls.Add(txtMemo);
DataRow dr = dtUdfControlInfo.NewRow();
dr[0] = txtMemo.ID;
dtUdfControlInfo.Rows.Add(dr);
}
break;
}
case 5:
{
//date
if(i%2==0)
{
cellUdf1.Text = colUdfNames[i].UdfName;
TextBox txtDate = new TextBox();
txtDate.ID = "txtDate" + colUdfNames[i].UdfId;
txtDate.MaxLength = colUdfNames[i].Width;
txtDate.Text = "12.07.2010";
cellUdfValue1.Controls.Add(txtDate);
}
else
{
cellUdf2.Text = colUdfNames[i].UdfName;
TextBox txtDate = new TextBox();
txtDate.ID = "txtDate" + colUdfNames[i].UdfId;
txtDate.MaxLength = colUdfNames[i].Width;
txtDate.Text = "12.07.2010";
cellUdfValue2.Controls.Add(txtDate);
}
break;
}
case 6:
{
//Datetime
if(i%2==0)
{
cellUdf1.Text = colUdfNames[i].UdfName;
TextBox txtDateTime = new TextBox();
txtDateTime.ID = "txtDateTime" + colUdfNames[i].UdfId;
txtDateTime.MaxLength = colUdfNames[i].Width;
txtDateTime.Text = "12.07.2010";
cellUdfValue1.Controls.Add(txtDateTime);
}
else
{
cellUdf2.Text = colUdfNames[i].UdfName;
TextBox txtDateTime = new TextBox();
txtDateTime.ID = "txtDateTime" + colUdfNames[i].UdfId;
txtDateTime.MaxLength = colUdfNames[i].Width;
txtDateTime.Text = "12.07.2010";
cellUdfValue2.Controls.Add(txtDateTime);
}
break;
}
case 7:
{
//"Dropdown"
if(i%2==0)
{
cellUdf1.Text = colUdfNames[i].UdfName;
DropDownList ddlDropDown = new DropDownList();
ddlDropDown.ID = "ddlDropDown" + colUdfNames[i].UdfId;
cellUdfValue1.Controls.Add(ddlDropDown);
LoadUdfList(ddlDropDown, colUdfNames[i].UdfId);
if (colUdfNames[i].IsMandatory)
{
ddlDropDown.Items.Insert(0, new ListItem("Select " + colUdfNames[i].UdfName, "0", true));
}
}
else
{
cellUdf2.Text = colUdfNames[i].UdfName;
DropDownList ddlDropDown = new DropDownList();
ddlDropDown.ID = "ddlDropDown" + colUdfNames[i].UdfId;
cellUdfValue2.Controls.Add(ddlDropDown);
LoadUdfList(ddlDropDown, colUdfNames[i].UdfId);
if (colUdfNames[i].IsMandatory)
{
ddlDropDown.Items.Insert(0, new ListItem("Select " + colUdfNames[i].UdfName, "0", true));
}
}
break;
}
case 8:
{
//"Checkbox"
if(i%2==0)
{
cellUdf1.Text = colUdfNames[i].UdfName;
CheckBox ChkBox = new CheckBox();
ChkBox.ID = "ChkBox" + colUdfNames[i].UdfId;
ChkBox.Checked = true;
cellUdfValue1.Controls.Add(ChkBox);
}
else
{
cellUdf2.Text = colUdfNames[i].UdfName;
CheckBox ChkBox = new CheckBox();
ChkBox.ID = "ChkBox" + colUdfNames[i].UdfId;
ChkBox.Checked = true;
cellUdfValue2.Controls.Add(ChkBox);
}
break;
}
}
if (i % 2 == 0)
{
tr1.Cells.AddAt(0, cellUdf1);
tr1.Cells.AddAt(1, cellUdfValue1);
}
else
{
tr1.Cells.AddAt(2, cellUdf2);
tr1.Cells.AddAt(3, cellUdfValue2);
}
tblUdf.Rows.Add(tr1);
// tblUdf.DataBind();
}
}
}
和读取功能如下
private void readValuesFromUdfControls() { colUdfDataMaster.Clear(); int c = tblUdf.Controls.Count; int tablrow = tblUdf.Rows.Count;
//TextBox tb1 = (TextBox)tblUdf.Rows[0].FindControl(dtUdfControlInfo.Rows[0][0].ToString());
//string tb1 = (string)Request.Form["ctl00_ContentPlaceHolder1_" + dtUdfControlInfo.Rows[0][0].ToString()];
for (int j = 0; j < tblUdf.Rows.Count;j++ )
{
for (int k = 0; k < tblUdf.Rows[j].Cells.Count;k++ )
{
Control ctrl = tblUdf.Rows[j].Cells[k].Controls[1];
for (int i = 0; i < colUdfNames.Count; i++)
{
PL_UdfDataMaster objUdfDataMaster = new PL_UdfDataMaster();
if (ctrl.ID.Contains("txtText"))
{
TextBox tb = (TextBox)ctrl;
objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
objUdfDataMaster.Enum_FormId = 3;
objUdfDataMaster.UdfData = tb.Text;
}
else if (ctrl.ID.Contains("txtNumber"))
{
TextBox tb = (TextBox)ctrl;
objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
objUdfDataMaster.Enum_FormId = 3;
objUdfDataMaster.UdfData = tb.Text;
}
else if (ctrl.ID.Contains("txtDecimal"))
{
TextBox tb = (TextBox)ctrl;
objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
objUdfDataMaster.Enum_FormId = 3;
objUdfDataMaster.UdfData = tb.Text;
}
else if (ctrl.ID.Contains("txtMemo"))
{
TextBox tb = (TextBox)ctrl;
objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
objUdfDataMaster.Enum_FormId = 3;
objUdfDataMaster.UdfData = tb.Text;
}
else if (ctrl.ID.Contains("txtDate"))
{
TextBox tb = (TextBox)ctrl;
objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
objUdfDataMaster.Enum_FormId = 3;
objUdfDataMaster.UdfData = tb.Text;
}
else if (ctrl.ID.Contains("txtDateTime"))
{
TextBox tb = (TextBox)ctrl;
objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
objUdfDataMaster.Enum_FormId = 3;
objUdfDataMaster.UdfData = tb.Text;
}
else if (ctrl.ID.Contains("ddlDropDown"))
{
DropDownList ddl = (DropDownList)ctrl;
objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
objUdfDataMaster.Enum_FormId = 3;
objUdfDataMaster.UdfData = ddl.SelectedItem.Text;
}
else if (ctrl.ID.Contains("ChkBox"))
{
CheckBox chk = (CheckBox)ctrl;
objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
objUdfDataMaster.Enum_FormId = 3;
objUdfDataMaster.UdfData = chk.Checked == true ? "true" : "false";
}
colUdfDataMaster.Add(objUdfDataMaster);
}
}
}
}
问题是我得到了表tblUdf的rowcount = 0,其中我在表格单元格中添加了表行和控件。
如何获取动态添加的控件的控件值。
Thankx。 Mohak
答案 0 :(得分:1)
在回读值之前,您需要确保在回发时再次添加控件。
答案 1 :(得分:0)
要详细说明Ben的答案,您需要覆盖CreateChildControls方法并在那里创建控件。这种方法的文档提供了一个方便的例子来做你正在做的事情:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.createchildcontrols.aspx