gpt4 book ai didi

c# - 如何在代码隐藏中访问 DetailsView 的 ItemTemplate 的 TextBox? ASP.Net C#

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:13 24 4
gpt4 key购买 nike

我有一个 DetailsView,其中 1 个字段转换为 TemplateField,以便能够操作包含 TextBox 的 InsertItemTemplate(参见:下面的代码)。问题是我无法在我的代码后面访问该 TextBox 属性......我真的不明白:(这是我的 aspx 代码(部分):

<asp:DetailsView ID="_DetailsView" ClientIDMode="Static" runat="server" Height="50px"
Width="125px" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="IDUniv"
DataSourceID="EntityDS" OnModeChanging="_OnModeChanging">
<Fields>
<asp:TemplateField HeaderText="DateUpdateUniv" SortExpression="DateUpdateUniv" ConvertEmptyStringToNull="False">
<InsertItemTemplate>
<asp:TextBox ID="TextBoxInsertItem" runat="server" Text='<%# Bind("DateUpdateUniv") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:EntityDataSource ID="EntityDS">

在 Page_LoadComplete 事件处理程序中我有这样的东西:

private void Page_LoadComplete(object sender, EventArgs e)
{
if (_DetailsView.HasControls())
{
Control _InsertDate = _DetailsView.FindControl("TextBoxInsertItem") as TextBox;
if (_InsertDate != null)
{
_InsertDate.Text = "something";
}
}
}

但以下代码是错误的:_DetailsView.FindControl("TextBoxInsertItem") 并且这也不起作用:_InsertDate.Text = "something";

我发现了一篇有趣的文章,但仍然......: http://www.devproconnections.com/article/aspnet2/getting-under-the-detailsview-control

有人可以帮我找到我的路吗?如何找到这个 TextBoxInsertItem 控件并与之交互?谢谢

最佳答案

 TextBox txtB = _DetailsView.FindControl("TextBoxInsertItem") as TextBox;
string text = txtB.Text;

这样试试?除了声明一个 TextBox 而不是一个 Control 之外,我看不出有任何区别。我已经使用了很多从子控件获取数据的特殊方法,而且它一直对我有用。

关于c# - 如何在代码隐藏中访问 DetailsView 的 ItemTemplate 的 TextBox? ASP.Net C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10984766/

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