如何在按钮单击事件上访问动态创建的控件值 [英] How to access dynamically created control's value on button click event
本文介绍了如何在按钮单击事件上访问动态创建的控件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在网页上创建了使用asp.net.After选择了特定的学生(姓名),从该学生的id,我从数据库中获取提供给该学生的所有主题。用户可以追加/写入已经/实现的标记学生(从用户那里获取输入)。点击按钮后,想要保存数据。在这里,我创建了动态控制,但我无法在按钮点击事件中访问它。如何做到这一点?
我尝试过:
I have created on web page using asp.net.After selecting particular student(name),from that student's id ,I am fetching all subject offered to that student from database.User can append/write marks got/achieved to that student(Taking input from user).After clicking on button,want to save data. Here I have created dynamical control but I unable to access it on button click event.How to do that?
What I have tried:
Code ASPX:
<asp:Panel ID="PnlStudent" runat="server" Visible="false">
<asp:Table ID="tblStudent" runat="server" Width="600px">
<asp:TableRow>
<asp:TableCell Height=" 32px" ForeColor="#32417a" BackColor="#e5e5e5" Width="156px">
Subject
</asp:TableCell>
<asp:TableCell Height=" 32px" ForeColor="#32417a" BackColor="#e5e5e5" Width="156px">
Marks Optined
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
Code in Cs file :
protected void ddlStudent_SelectedIndexChanged(object sender, EventArgs e)
{
// fetch from Student table
if (dsStudent != null)
{
DataTable dt = dsStudent.Tables["Student"];
if (dt.Rows.Count > 0)
{
PnlStudent.Visible = true;
foreach (DataRow dr in dt.Rows)
{
TableRow NewRow1 = new TableRow();
TableCell NewCell1 = new TableCell();
NewCell1.Width = Unit.Pixel(156);
NewCell1.Height = Unit.Pixel(32);
NewCell1.ForeColor = System.Drawing.Color.LightSlateGray;
NewCell1.BackColor = System.Drawing.Color.LightGray;
Label newLable1 = new Label();
newLable1.Text = dr["Subject"].ToString();
NewCell1.Controls.Add(newLable1);
NewRow1.Cells.Add(NewCell1);
TableCell NewCell2 = new TableCell();
NewCell2.Height = Unit.Pixel(32);
NewCell2.BackColor = System.Drawing.Color.LightGray;
TextBox txtBox1 = new TextBox();
NewCell2.Controls.Add(txtBox1);
NewRow1.Cells.Add(NewCell2);
tblStudent.Rows.Add(NewRow1);
}
}
}
}
protected void BtnSave_Click(object sender, EventArgs e)
{
}
推荐答案
为了做你想做的事,首先为动态添加的每个组件键入一个唯一的标识,如下所示:
Hi,
To do what you wish, first type a unique identification for each component that you add dynamically, like this:
PnlStudent.Visible = true;
int index_row = 0;
foreach (DataRow dr in dt.Rows)
{
TableRow NewRow1 = new TableRow();
TableCell NewCell1 = new TableCell();
NewCell1.Width = Unit.Pixel(156);
NewCell1.Height = Unit.Pixel(32);
NewCell1.ForeColor = System.Drawing.Color.LightSlateGray;
NewCell1.BackColor = System.Drawing.Color.LightGray;
Label newLable1 = new Label();
//type id in the label component
newLable1.ID = "mylabel_" + index_row.ToString();
newLable1.Text = dr["Subject"].ToString();
NewCell1.Controls.Add(newLable1);
NewRow1.Cells.Add(NewCell1);
TableCell NewCell2 = new TableCell();
NewCell2.Height = Unit.Pixel(32);
NewCell2.BackColor = System.Drawing.Color.LightGray;
TextBox txtBox1 = new TextBox();
//type id in the textbox component
txtBox1.ID = "mytext_" + index_row.ToString();
NewCell2.Controls.Add(txtBox1);
NewRow1.Cells.Add(NewCell2);
tblStudent.Rows.Add(NewRow1);
index_row++;
}
要读取组件中的数据,请执行以下操作:
To read the data in the component, do this:
protected void BtnSave_Click(object sender, EventArgs e)
{
string valuemytextbox, valuemylabel;
int index_row = 0;
bool header = true; // the first line is header
//read each line
foreach (TableRow row in tblStudent.Rows)
{
// the first line is header
if (header)
{
header = false;
}
else
{
//find components in the row
valuemytextbox = ((TextBox)row.FindControl("mytext_" + index_row.ToString())).Text;
valuemylabel = ((Label)row.FindControl("mylabel_" + index_row.ToString())).Text;
//salve data in database
....
index_row++;
}
}
}
这篇关于如何在按钮单击事件上访问动态创建的控件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文