如何从 DetailsView 检索文本框 - ASP.NET
我有一个DetailsView 控件,里面有一个文本框模板。 我需要在插入数据事件处理程序_ItemInserting 时找出文本框的值。
该脚本不起作用。蚂蚁的想法??谢谢
-------------------- 网页表格
<asp:TemplateField HeaderText="Profile" SortExpression="ContentAuthor">
<ItemTemplate>
<asp:Label ID="uxContentAuthorDisplayer" runat="server" Text='<%# Bind("ContentAuthor") %>'></asp:Label>
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="uxContentAuthorInput" runat="server" Text='<%# Bind("ContentAuthor") %>'></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
-------------------- 背后的代码
protected void uxInsertAuthor_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
//// Find control on page
TextBox myAuthorProfile = (TextBox)uxInsertAuthorInput.FindControl("uxContentAuthorDisplayer");
// Set a default value in Data Base if field has been left empty (DB field NOT NULL)
if (string.IsNullOrEmpty(myAuthorProfile.Text))
{
string myAllert = "Field is NULL";
}
else
{
string myAllet = "Field is NOT NULL";
}
}
I have a DetailsView Control, inside as a template a TextBox.
I need to find out the value for a TextBox when Inserting data Event handler-, _ItemInserting.
The script does not work. Ant ideas?? Thanks
-------------------- WEB FORM
<asp:TemplateField HeaderText="Profile" SortExpression="ContentAuthor">
<ItemTemplate>
<asp:Label ID="uxContentAuthorDisplayer" runat="server" Text='<%# Bind("ContentAuthor") %>'></asp:Label>
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="uxContentAuthorInput" runat="server" Text='<%# Bind("ContentAuthor") %>'></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
-------------------- CODE BEHIND
protected void uxInsertAuthor_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
//// Find control on page
TextBox myAuthorProfile = (TextBox)uxInsertAuthorInput.FindControl("uxContentAuthorDisplayer");
// Set a default value in Data Base if field has been left empty (DB field NOT NULL)
if (string.IsNullOrEmpty(myAuthorProfile.Text))
{
string myAllert = "Field is NULL";
}
else
{
string myAllet = "Field is NOT NULL";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
TemplateField
容器控件(DetailsView)的FindControl
方法。例如,如果您的 DetailsView 名为“MyControl”,请尝试请注意
您在 FindControl 方法中使用 Label 控件的 ID,并且尝试将其转换为 TextBox。
Try using the
FindControl
method of yourTemplateField
container control (the DetailsView). For example, if your DetailsView is named "MyControl" tryPlease Note
You are using the ID of the Label control in your FindControl method and you are trying to cast it to a TextBox.
另一种选择是从
DetailsViewInsertEventArgs
。它有一个名为Values
的属性,它是一个字典,其中包含将从 DetailsView 进入数据库的所有绑定值。所以你可以执行以下操作:Another option is to get the value from the
DetailsViewInsertEventArgs
. It has a property calledValues
which is a dictionary with all the bound values that will go into the database from the DetailsView. So you could do the following instead:如果我理解正确的话,当数据放入
DetailsView
时,您需要检查TextBox
的值。ItemInserting
事件不是您要查找的事件:http://msdn.microsoft.com/en-us/library/keezbt7k.aspx
我想说
ItemCreated
就是您正在寻找的那个。If I understood it correctly, you want to check the value of the
TextBox
when the data is put into theDetailsView
. TheItemInserting
event is not the one you're looking for:http://msdn.microsoft.com/en-us/library/keezbt7k.aspx
I'd say
ItemCreated
is the one you're looking for.