简体   繁体   English

在用户控制页面的列表视图中访问动态创建的表的文本框?

[英]Access the textbox of dynamically created table in listview of user control page?

I'm creating web template using asp.net and C#. 我正在使用asp.net和C#创建Web模板。 in my user control page i have to create the table dynamically inside the list view. 在我的用户控制页面中,我必须在列表视图中动态创建表。 I just read the data from XML file then retrieve the name and number of columns and rows of each table. 我只是从XML文件中读取数据,然后检索每个表的名称,列数和行数。 While I'm creating the table I assign the name and id to each cell. 在创建表时,我将名称和ID分配给每个单元格。 In this dynamic table once the user click on the edit button of each row all the cells of this row will change to textbox . 在此动态表中,一旦用户单击每行的编辑按钮,该行的所有单元格都将变为textbox then the user can change the data of this textboxes , but in my update function I cannot access to these textboxes by id and store the data in database. 那么用户可以更改此文本textboxes的数据,但是在我的更新功能中,我无法通过id访问这些textboxes并将数据存储在数据库中。 Below is my dynamic table code which is created at the ItemDataBound function: 以下是我在ItemDataBound函数中创建的动态表代码:

System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
TableRow tr_DataBound = new TableRow();
TableCell tc_DataBound = new TableCell();
for (int i = 1; i <= pkCounter + nonPkCounter; i++)
            {
                //tc_DataBound = new TableCell();
                tc_DataBound = new TableCell();
                TextBox tb = new TextBox();
                //(TextBox)e.Item.FindControl("td_EditTemp" + i);
                tb.Width = 110;
                tb.Text = rowView[i - 1].ToString();
                tb.ID = "td_EditTemp" + i;
                tc_DataBound.Controls.Add(tb);
                tc_DataBound.CssClass = "th_ItemTemplate";
                tr_DataBound.Cells.Add(tc_DataBound);
            }
        tr_DataBound.Cells.Add(tc_DataBound);
        Table table_Lv_ItemTemplate = (Table)e.Item.FindControl("Table_Lv_ItemTemplate");
        table_Lv_ItemTemplate.Rows.Add(tr_DataBound);

My listview code is: 我的listview代码是:

<asp:ListView ID="lv_Uc_Module" runat="server"
                    onitemediting="lv_Uc_Module_ItemEditing" 
                    onitemcanceling="lv_Uc_Module_ItemCanceling" 
                    onitemdeleting="lv_Uc_Module_ItemDeleting"  
                    OnItemDataBound="lv_Uc_Module_ItemDataBound"
                    OnSorting="lv_Uc_Module_Sorting">

                        <LayoutTemplate>
                            <asp:Table runat="server" ID="table_Lv_Layout">
                                <asp:TableRow runat="server" ID="tr_Table_Layout">
                                    <asp:TableCell runat="server" ID="td_Table_Layout">
                                        <asp:Table runat="server" ID="itemPlaceholderContainer">
                                            <asp:TableRow runat="server" ID="tr_Table_IphContainer">

                                                <asp:TableHeaderCell runat="server">
                                                    <asp:PlaceHolder ID="th_Ph_Lv_header" runat="server"></asp:PlaceHolder>
                                                </asp:TableHeaderCell>
                                            </asp:TableRow>
                                            <asp:TableRow runat="server">
                                                <asp:TableCell runat="server">

                                                    <asp:PlaceHolder runat="server" ID="itemPlaceholder" />

                                                </asp:TableCell>

                                            </asp:TableRow>
                                        </asp:Table>
                                    </asp:TableCell>
                                </asp:TableRow>
                                <asp:TableRow runat="server" ID="tr_Validate_Table_Layout">
                                    <asp:TableCell runat="server" ID="td_Validate_Table_Layout" HorizontalAlign="Center" BackColor="#CCCCCC">
                                        <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="You received the following errors:" ShowMessageBox="true" ShowSummary="false" ValidationGroup="VGEditTmp" />

                                    </asp:TableCell>
                                </asp:TableRow>
                            </asp:Table>
                            <br />
                            <asp:DataPager ID="lv_DataPager" runat="server" PagedControlID="lv_Uc_Module" PageSize="25" OnPreRender="lv_DataPager_PreRender">
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonType="Image" ShowFirstPageButton="true" ShowLastPageButton="true" FirstPageImageUrl="~/Images/First.png" LastPageImageUrl="~/Images/Last.png" NextPageImageUrl="~/Images/Next.png" PreviousPageImageUrl="~/Images/Previous.png" />
                                    <asp:TemplatePagerField>
                                        <PagerTemplate>
                                            <span style="color:Blue;">
                                            </span>
                                        </PagerTemplate>
                                    </asp:TemplatePagerField>
                                </Fields>
                            </asp:DataPager>
                        </LayoutTemplate>

                        <ItemTemplate>
                            <asp:TableRow runat="server">
                                <asp:TableCell runat="server">

                                    <asp:Table runat="server" ID="Table_Lv_ItemTemplate"></asp:Table>
                                </asp:TableCell>
                                <asp:TableCell runat="server">
                                    <asp:Button ID="btn_Edit" runat="server" CommandName="Edit" Text="" CssClass="btn_Edit" CausesValidation="True" Visible="false" />
                                    <asp:Button ID="btn_Delete" runat="server" CommandName="Delete" Text="" CssClass="btn_Delete" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this item?');" Visible="false" />
                                </asp:TableCell>
                            </asp:TableRow>
                        </ItemTemplate>
                    </asp:ListView>

I'm using below method in my update function at the code behind to access the data changed at the textbox : 我在更新功能的后面代码中使用以下方法来访问在textbox更改的数据:

TextBox tb = (lv_Uc_Module.Items[e].FindControl("td_EditTemp" + i + "_" + e)) as TextBox;

Could you please guide me how to access these textboxes data. 您能否指导我如何访问这些textboxes数据。
Appreciate your consideration. 感谢您的考虑。

Have you looked at using the asp:repeater? 您是否看过使用asp:repeater?

It makes creating dynamic controls much simpler especially if you use the 'Data_Bound' Event. 它使创建动态控件变得更加简单,尤其是在使用“ Data_Bound”事件的情况下。

Have a look here - repeater example . 看这里- 中继器示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问用户控制页面中动态创建表的文本框? - how to access the textbox of dynamically created table in user control page? 如何访问usercontrol页面listview中动态创建的表的文本框? - How to access the textbox of dynamically created table inside the listview of the usercontrol page? 动态创建的ListView控件 - Dynamically Created ListView Control 在ListView控件中访问TextBox控件 - Access TextBox Control In ListView Control 从父页面动态添加的用户控件的访问控制 - Access control of dynamically added user control from parent page 从ASP.net中的用户控件访问页面上的文本框 - Access Textbox on Page from User Control in ASP.net 动态创建面板的访问控制 - Access control of panel created dynamically 在动态创建的事件处理程序中访问动态创建的控件(文本框) - Accessing Dynamically created control (textbox) in a dynamically created eventhandler 使用JQuery将文本框与动态创建的表中的单元格相乘 - Multiplying a textbox with a cell in a dynamically created table with JQuery 无法访问通过用户控件动态加载的控件(如文本框、复选框、复选框列表)中的数据 - Unable to access data from controls (like textbox, checkbox, checkboxlist) which are loaded dynamically through an user control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM