如何从 DetailsView 检索文本框 - ASP.NET

发布于 2024-10-01 16:21:44 字数 1409 浏览 0 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染柒℉ 2024-10-08 16:21:44

尝试使用 TemplateField 容器控件(DetailsView)的 FindControl 方法。例如,如果您的 DetailsView 名为“MyControl”,请尝试

//// Find control on page
TextBox myAuthorProfile = (TextBox)MyControl.FindControl("uxContentAuthorInput");

请注意

您在 FindControl 方法中使用 Label 控件的 ID,并且尝试将其转换为 TextBox。

Try using the FindControl method of your TemplateField container control (the DetailsView). For example, if your DetailsView is named "MyControl" try

//// Find control on page
TextBox myAuthorProfile = (TextBox)MyControl.FindControl("uxContentAuthorInput");

Please Note

You are using the ID of the Label control in your FindControl method and you are trying to cast it to a TextBox.

无可置疑 2024-10-08 16:21:44

另一种选择是从 DetailsViewInsertEventArgs。它有一个名为 Values 的属性,它是一个字典,其中包含将从 DetailsView 进入数据库的所有绑定值。所以你可以执行以下操作:

if (string.IsNullOrEmpty(e.Values("ContentAuthor")))
{
  string myAlert = "Field is NULL";
}
else
{
  string myAlert = "Field is NOT NULL";
}  

Another option is to get the value from the DetailsViewInsertEventArgs. It has a property called Values 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:

if (string.IsNullOrEmpty(e.Values("ContentAuthor")))
{
  string myAlert = "Field is NULL";
}
else
{
  string myAlert = "Field is NOT NULL";
}  
日久见人心 2024-10-08 16:21:44

如果我理解正确的话,当数据放入 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 the DetailsView. The ItemInserting 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文