[英]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.