gpt4 book ai didi

c# - 如何访问用户控件页面 ListView 中动态创建的表的文本框?

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:47 26 4
gpt4 key购买 nike

实际上我正在使用 asp.net 和 c# 开发 Web 模板。我有包含用户控制页面的 maintemp.aspx 页面。在我的用户控件中,我有一个 ListView ,在这个 ListView 中,我必须基于 XML 文件在 ItemTemplate 中创建动态表。

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

正如您在 listview ItemTemplate 部分看到的那样,我放了一个 with ID = "Table_Lv_ItemTemplate",我在这里创建了动态表。

我已将动态表代码放在 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);
}
}

此数据绑定(bind)的结果如下图所示。它动态地从 XML 文件中读取数据并创建表。 enter image description here

一旦用户点击下图的编辑按钮,他们就可以编辑行数据。 enter image description here

问题在这里,我无法访问这些文本框来获取它们的数据并更新数据库。而且我无法删除行。

我试过 TextBox tempLabelLv = (TextBox)add_Table.FindControl("td_EditTemp2_5"); add_Table 是我的动态表 ID,我试过 TextBox tempLabelLv = (TextBox)lv_Uc_Module.FindControl("td_EditTemp2_5"); lv_Uc_Module 是我的 ListView ID。但我仍然无法访问这些文本框。

能否请您指导我如何克服这个问题,并获取这些文本框的数据来更新数据库?感谢您的考虑。

最佳答案

这只是一个猜测,所以如果不正确,请不要打分:D 好吧,假设您有一个动态创建的页面。动态创建中的元素自然不会成为设计文件的一部分,对吗?所以说我通过一些脚本动态创建一个标签,现在我想进入它。我认为,我还可以通过手动引入它来增加识别度。所以要访问该标签,我会这样做:

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

现在即使我动态创建它也可以访问它。但是,如果您有动态创建的中继器或 ListView 或您拥有的东西,请务必注册它,然后使用您之前描述的方法( TextBox tempLabelLv = (TextBox)add_Table.FindControl("td_EditTemp2_5"); )找到它. (在上面的示例中,您将使用 protected System.Web.UI.HtmlControls.HtmlTable add_Table; 注册“add_Table”“希望这能有所帮助。如果没有,我试过了。=)

关于c# - 如何访问用户控件页面 ListView 中动态创建的表的文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9477635/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com