问题描述
大家好,
我有一个表table1
,其中有1行包含2个文本框和ajax tabcontainer中的单选按钮列表.
在现有的table1
中有一个添加和删除按钮,用于使用2文本框和单选按钮列表添加/删除新行.并且有一个提交按钮来显示此值.但是我无法获得这些动态创建的文本框的价值.在普通页面(不使用tabcontainer)中,效果很好.
谁能帮我...我尝试了2天来找到解决方案.拜托...
这是我的代码...
Hi all,
I have a table table1
with 1 row containing 2 textbox and a radiobuttonlist in a ajax tabcontainer.
There is an add and remove button to add/remove new rows with 2textbox and a radiobuttonlist in the existing table1
. And there is a submit button to show this values. But I could not get the value of these dynamically created textboxes. In a normal page(without using tabcontainer) it works fine.
Can anyone please help me...I tried 2days to find the solution. Please...
this is my code...
private int numOfRows = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GenerateTable(numOfRows);
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (ViewState["RowsCount"] != null)
{
numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
GenerateTable(numOfRows);
}
}
private void GenerateTable(int rowsCount)
{
int colsCount = 4;
for (int i = 1; i < rowsCount; i++)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
Label label = new Label();
label.ID = "medicalDepLblNo" + i;
label.Text = (i + 1).ToString();
cell1.Controls.Add(label);
row.Cells.Add(cell1);
TableCell cell2 = new TableCell();
TextBox textBox = new TextBox();
textBox.ID = "medicalDepTxtName" + i;
textBox.Width = 204;
cell2.Controls.Add(textBox);
row.Cells.Add(cell2);
TableCell cell3 = new TableCell();
textBox = new TextBox();
textBox.ID = "medicalDepTxtAge" + i;
textBox.Width = 60;
cell3.Controls.Add(textBox);
row.Cells.Add(cell3);
TableCell cell4 = new TableCell();
RadioButtonList rBtnList = new RadioButtonList();
rBtnList.ID = "medicalDepRbtnListSex" + i;
rBtnList.RepeatDirection = RepeatDirection.Horizontal;
rBtnList.Items.Add("Male");
rBtnList.Items.Add("Female");
rBtnList.SelectedIndex = 0;
cell4.Controls.Add(rBtnList);
row.Cells.Add(cell4);
// And finally, add the TableRow to the Table
TableDependant.Rows.Add(row);
}
//Set Previous Data on PostBacks
SetPreviousData(rowsCount, colsCount);
//Sore the current Rows Count in ViewState
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}
private void SetPreviousData(int rowsCount, int colsCount)
{
for (int i = 1; i < rowsCount; i++)
{
//Extracting the Dynamic Controls from the Table
Label lbl1 = (Label)TableDependant.Rows[i].Cells[0].FindControl("medicalDepLblNo" + i);
lbl1.Text = Request.Form["medicalDepLblNo" + i];
TextBox txtBox1 = (TextBox)TableDependant.Rows[i].Cells[1].FindControl("medicalDepTxtName" + i);
txtBox1.Text = Request.Form["medicalDepTxtName" + i];
TextBox txtBox2 = (TextBox)TableDependant.Rows[i].Cells[2].FindControl("medicalDepTxtAge" + i);
txtBox2.Text = Request.Form["medicalDepTxtAge" + i];
RadioButtonList rbtnList1 = (RadioButtonList)TableDependant.Rows[i].Cells[3].FindControl("medicalDepRbtnListSex" + i);
rbtnList1.SelectedValue = Request.Form["medicalDepRbtnListSex" + i];
}
}
protected void btnSubmitMedical_Click(object sender, EventArgs e)
{
int num = int.Parse(medicalDdlNoofDependants.SelectedValue);
string[] names = new string[10];
for (int i = 0; i < num; i++)
{
names[i] = Request.Form["medicalDepTxtName" + i];
}
}
从答案移至代码(并格式化)-Ankur.
Moved to code from answer (and formatted) - Ankur.
推荐答案
这篇关于Ajax tabcontainer中的动态文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!