简体   繁体   English

如何访问usercontrol页面listview中动态创建的表的文本框?

[英]How to access the textbox of dynamically created table inside the listview of the usercontrol page?

Actually I'm developing web template using asp.net and c#. 实际上,我正在使用asp.net和c#开发Web模板。 I have maintemp.aspx page which includes usercontrol page. 我有maintemp.aspx页面,其中包括usercontrol页面。 In my usercontrol I have a listview which in this listview I have to create dynamic table inside the ItemTemplate based on a XML file. 在我的用户控件中,我有一个列表视图,在该列表视图中,我必须基于XML文件在ItemTemplate内创建动态表。

My listview code including the ItemTemplate is: 我的Listview代码(包括ItemTemplate)是:

<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>

As you can see at the listview ItemTemplate part I have put a with ID = "Table_Lv_ItemTemplate" which I create the dynamic table here. 如您所见,在listview的ItemTemplate部分中,我放置了一个ID = "Table_Lv_ItemTemplate" ,我在此处创建了动态表。

I have put the dynamic table code at the ItemDataBound function as below: 我将动态表代码放在ItemDataBound函数中,如下所示:

protected void lv_Uc_Module_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (tempDataBound == 0 && !"".Equals(hid_ChooseModule.Value))
    {
        XmlDocument xDocRead = new XmlDocument();
        xDocRead.Load(Server.MapPath("ModuleTemp.xml"));
        lv_Uc_Module_DataBind("");

        Table table_Lv_ItemTemplate = (Table)e.Item.FindControl("Table_Lv_ItemTemplate");
        table_Lv_ItemTemplate.Controls.Clear();
        //table_Lv_ItemTemplate.Dispose();

        tempDataBound++;
    }

    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        XmlDocument xDocRead = new XmlDocument();
        xDocRead.Load(Server.MapPath("ModuleTemp.xml"));
        string xModuleName = hid_ChooseModule.Value;
        XmlNode xColCounter;
        TableRow tr_DataBound = new TableRow();
        TableRow tr_Lv_Header = new TableRow();

        if (!"".Equals(hid_ChooseModule.Value))
        {
            xColCounter = xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/List");
        }
        else
        {
            xModuleName = xDocRead.SelectSingleNode("ModuleTemp").FirstChild.Name;
            xColCounter = xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/List");
        }
        int pkCounter = 0, nonPkCounter = 0, colCount = xColCounter.ChildNodes.Count;
        string[] primaryKey = new string[30];
        string[] nonPrimaryKey = new string[colCount + 1];

        for (int i = 1; i <= colCount; i++)
        {
            if (xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/Edit/TableColumn" + i).Attributes.GetNamedItem("IsPrimaryKey").Value == "Y")
            {
                primaryKey[pkCounter] = xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/Edit/TableColumn" + i).Attributes.GetNamedItem("Name").Value;
                pkCounter++;
            }
            else
            {
                nonPrimaryKey[nonPkCounter] = xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/Edit/TableColumn" + i).Attributes.GetNamedItem("Name").Value;
                nonPkCounter++;
            }
        }

        System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
        TableCell tc_Lv_Header = new TableCell();
        if (!"".Equals(hid_ChooseModule.Value))
        {
            if (tempHeader == 0)
            {
                tempHeader++;
                for (int i = 1; i <= pkCounter + nonPkCounter; i++)
                {
                    tc_Lv_Header = new TableCell();
                    Label lb = new Label();

                    if (i <= pkCounter)
                    {
                        lb.Text = primaryKey[i - 1];
                    }
                    else
                    {
                        lb.Text = nonPrimaryKey[i - pkCounter - 1];
                    }

                    lb.ID = "lb" + i + "_Table_IphContainer";
                    tc_Lv_Header.Controls.Add(lb);
                    tc_Lv_Header.CssClass = "th_ItemTemplate";
                    tr_Lv_Header.Cells.Add(tc_Lv_Header);
                }

                tr_Lv_Header.Cells.Add(tc_Lv_Header);

                table_Header.Rows.Add(tr_Lv_Header);
                PlaceHolder th_Ph_Lv_Hedear = (PlaceHolder)lv_Uc_Module.FindControl("th_Ph_Lv_header");
                th_Ph_Lv_Hedear.Controls.Add(table_Header);
            }
        }
        TableCell tc_DataBound = new TableCell();

        if (editTempSelection == e.Item.DataItemIndex)
        {
            for (int i = 1; i <= pkCounter + nonPkCounter; i++)
            {
                tc_DataBound = new TableCell();
                TextBox tb = new TextBox();
                tb.Width = 110;
                tb.Text = rowView[i - 1].ToString();
                tb.ID = "td_EditTemp" + i + "_" + e.Item.DataItemIndex;
                tc_DataBound.Controls.Add(tb);
                tc_DataBound.CssClass = "td_Tb_ItemTemplate";
                tr_DataBound.Cells.Add(tc_DataBound);
            }
        }
        else
        {
            for (int i = 1; i <= pkCounter + nonPkCounter; i++)
            {
                tc_DataBound = new TableCell();
                Label lb = new Label();
                lb.Text = rowView[i - 1].ToString();
                lb.ID = "lb_ItemTemp" + i + "_" + e.Item.DataItemIndex;
                tc_DataBound.Controls.Add(lb);
                tc_DataBound.CssClass = "td_Lb_ItemTemplate";
                tr_DataBound.Cells.Add(tc_DataBound);
            }
        }
        // add button to the columns
        if (editTempSelection != e.Item.DataItemIndex)
        {
            tc_DataBound = new TableCell();
            Button btn_Edit = new Button();
            btn_Edit.ID = "btn_Edit";
            btn_Edit.CommandName = "Edit";
            btn_Edit.CssClass = "btn_Edit";
            btn_Edit.CausesValidation = true;
            tc_DataBound.Controls.Add(btn_Edit);
            tr_DataBound.Cells.Add(tc_DataBound);

            tc_DataBound = new TableCell();
            Button btn_Delete = new Button();
            btn_Delete.ID = "btn_Delete";
            btn_Delete.CommandName = "Delete";
            btn_Delete.CssClass = "btn_Delete";
            btn_Delete.CausesValidation = false;
            btn_Delete.OnClientClick = "return confirm('Are you sure you want to delete this item?');";
            tc_DataBound.Controls.Add(btn_Delete);
            tr_DataBound.Cells.Add(tc_DataBound);
        }
        else
        {
            tc_DataBound = new TableCell();
            Button btn_Update = new Button();
            btn_Update.ID = "btn_Update";
            btn_Update.CommandName = "Update";
            btn_Update.CssClass = "btn_Update";
            btn_Update.ValidationGroup = "VGEditTmp";
            tc_DataBound.Controls.Add(btn_Update);
            tr_DataBound.Cells.Add(tc_DataBound);

            tc_DataBound = new TableCell();
            Button btn_Cancel = new Button();
            btn_Cancel.ID = "btn_Cancel";
            btn_Cancel.CommandName = "Cancel";
            btn_Cancel.CssClass = "btn_Cancel";
            btn_Cancel.CausesValidation = false;
            tc_DataBound.Controls.Add(btn_Cancel);
            tr_DataBound.Cells.Add(tc_DataBound);
            tr_DataBound.CssClass = "tr_Edit_ItemTemplate";
        }

        if (editTempSelection == e.Item.DataItemIndex)
        {
            lv_Uc_Module_ItemUpdating(sender, e.Item.DataItemIndex);
        }
        // add columns to the row
        tr_DataBound.Cells.Add(tc_DataBound);
        Table table_Lv_ItemTemplate = (Table)e.Item.FindControl("Table_Lv_ItemTemplate");
        table_Lv_ItemTemplate.Rows.Add(tr_DataBound);
    }
}

The result of this data bound is below image. 该数据绑定的结果在图像下方。 And it dynamically read the data from XML file and create the table. 并且它动态地从XML文件读取数据并创建表。 在此处输入图片说明

Once the users click on the edit button as below image, they can edit the row data. 用户单击下图所示的“编辑”按钮后,就可以编辑行数据。 在此处输入图片说明

The problem is here, that I can not access to these textboxes to get their data and update the data base. 问题出在这里,我无法访问这些文本框来获取其数据并更新数据库。 And I can not delete the rows. 而且我无法删除行。

I have tried TextBox tempLabelLv = (TextBox)add_Table.FindControl("td_EditTemp2_5"); 我已经尝试了TextBox tempLabelLv = (TextBox)add_Table.FindControl("td_EditTemp2_5"); the add_Table is my dynamic table id and I have tried TextBox tempLabelLv = (TextBox)lv_Uc_Module.FindControl("td_EditTemp2_5"); add_Table是我的动态表ID,我已经尝试了TextBox tempLabelLv = (TextBox)lv_Uc_Module.FindControl("td_EditTemp2_5"); that lv_Uc_Module is my listview id. lv_Uc_Module是我的列表视图ID。 But still I can not access to these textboxes. 但是我仍然无法访问这些文本框。

Could you please guide me how to overcome this problem, and get the data of these textboxes to update the database? 您能否指导我如何解决此问题,并获取这些文本框的数据来更新数据库? Appreciate your consideration. 感谢您的考虑。

You use dynamic table, so, each time, when your page is overloaded (for example, if you press any button), your table will be cleared from Rows, Cells and Controls, that were added dynamically. 您使用动态表,因此,每次页面超载时(例如,如果您按任意按钮),都会从动态添加的行,单元格和控件中清除表。 It's not a perfect solution, but try to rewrite table on the Page_Load event. 这不是一个完美的解决方案,但是尝试在Page_Load事件上重写表。

This is just a guess so don't go knocking down points if it's not right :D Okay, say you have a dymanically created page. 这只是一个猜测,因此如果不正确,请不要降低分数:D好的,说您有一个动态创建的页面。 Naturally elements in that dynamic creation will not be part of the design file, right? 当然,动态创建中的元素将不属于设计文件,对吗? So say I dynamically create a Label via some script, and now I want to get inside it. 假设我通过一些脚本动态创建了一个Label,现在我想进入它。 I could also, I think, add recognition to it by manually introducing it. 我认为,我也可以通过手动介绍它来增加对它的认可。 So to access that label I would do this: 因此,要访问该标签,我可以这样做:

public class yourPageOrClassNameOrSomethingOrAnotherOrIDontKnow: System.Web.UI.Page
{
    protected System.Web.UI.WebControls.Label myLabel;
...
}

Now I can access it even if I create it dynamically. 现在,即使我动态创建它也可以访问它。 If you however have a repeater or listview or what have you, that you created dynamically, be sure to register that, then use your earlier described method ( TextBox tempLabelLv = (TextBox)add_Table.FindControl("td_EditTemp2_5"); ) to locate it. 但是,如果您具有转发器或列表视图或您动态创建的内容,请确保进行注册,然后使用前面所述的方法(TextBox tempLabelLv =(TextBox)add_Table.FindControl(“ td_EditTemp2_5”);)找到它。 (In the above example you would register "add_Table" using protected System.Web.UI.HtmlControls.HtmlTable add_Table; " Hope this helps a bit. If not, I tried. =) (在上面的示例中,您将使用受保护的System.Web.UI.HtmlControls.HtmlTable add_Table注册“ add_Table” “希望能有所帮助。如果没有,我尝试了。=)

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

相关问题 在用户控制页面的列表视图中访问动态创建的表的文本框? - Access the textbox of dynamically created table in listview of user control page? 如何访问用户控制页面中动态创建表的文本框? - how to access the textbox of dynamically created table in user control page? 在DataList中的用户控件中访问动态创建的TextBox - Access dynamically created TextBox within a usercontrol which is in a DataList 在动态创建的UserControl中从文本框检索文本 - Retrieving Text from Textbox in dynamically Created UserControl 如何从另一个访问 UserControl 中的文本框 - How to access textbox inside UserControl from another one 我如何访问列表视图列内的文本框 - how i access textbox inside a listview Column asp.net:如何访问从后面的代码创建的表的单元格内的文本框的内容 - asp.net: how to access contents of a textbox inside the cell of a table that was created from the code behind 动态创建的文本框在用户控件中不可见-Winforms,C# - Dynamically created textbox not visible in Usercontrol - winforms, C# 如何在页面上动态管理UserControl…? - how to Manage UserControl dynamically on a page…? 如何从aspx页面访问userControl内部的Web控件 - how to access a web control inside a userControl from aspx page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM