如何在按钮单击事件上访问动态创建的控件值

编程入门 行业动态 更新时间:2024-10-19 08:50:16
本文介绍了如何在按钮单击事件上访问动态创建的控件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在网页上创建了使用asp.After选择了特定的学生(姓名),从该学生的id,我从数据库中获取提供给该学生的所有主题。用户可以追加/写入已经/实现的标记学生(从用户那里获取输入)。点击按钮后,想要保存数据。在这里,我创建了动态控制,但我无法在按钮点击事件中访问它。如何做到这一点? 我尝试过:

I have created on web page using asp.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++; } } }

更多推荐

如何在按钮单击事件上访问动态创建的控件值

本文发布于:2023-11-02 14:25:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   单击   按钮   事件   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!