如何从DetailsView检索文本框-ASP.NET [英] How to retrieve a TextBox from DetailsView - ASP.NET

查看:87
本文介绍了如何从DetailsView检索文本框-ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DetailsView控件,在其中作为一个文本框作为模板. 插入数据事件处理程序_ItemInserting时,我需要找出TextBox的值.

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";
            }          
        }

推荐答案

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

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");

请注意

您正在FindControl方法中使用Label控件的ID,并试图将其强制转换为TextBox.

这篇关于如何从DetailsView检索文本框-ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆