ASP获取控件类型 [英] ASP getting control types

查看:65
本文介绍了ASP获取控件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我的问题.我有一个自定义ASP控件,已将其放置在Repeater控件中.我的控件由一个CheckBox,一个TextBox和一个HiddenField组成.在控件上,我有一个名为Save的公共方法.我要做的就是遍历转发器的控件,当发现我的控件被选中时,它必须调用Save方法.
我有一个可供访问的Checked公共属性,我的问题是当我循环遍历这样的控件时:

Ok, so this is my problem. I have a custom ASP control that I have put in a Repeater control. My control consists of a CheckBox, a TextBox, and a HiddenField. On the control I have a public method called Save. All I want to do is loop through the controls of the repeater and when it finds my control is Checked, it must call the Save method.
I have a public property for Checked that I can access, my problem is that when I loop through the controls like so:

<pre>foreach (Control c in myrepeater.Controls)
     if (c.GetType() == typeof(MyControl)
     {
        MyControl mc = c as MyControl;
        if (mc.Checked)
            mc.Save();
     }


我还检查了c.GetType().BaseTypec.GetType().BaseType.BaseType,但都没有帮助.


I have also checked c.GetType().BaseType and c.GetType().BaseType.BaseType, and neither help at all. Any help would be greatly appreciated!

推荐答案



如果您的标记看起来像这样,请在文本框的位置考虑您的自定义控件,然后

Hi,

If your markup looks something like this where consider your custom control in the place of textbox then

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" >
    <ItemTemplate>
        <asp:TextBox ID="Text1" runat="server" Text='<%# Eval("ProductName") %>' ></asp:TextBox>
        <asp:CheckBox ID="check1" runat="server" Checked='<%# Eval("Test") %>' />
    </ItemTemplate>
</asp:Repeater>



从后面的代码



From code behind

foreach(Control ctrl in Repeater1.Controls)
{
    TextBox txt;
    //Text1 here is the control id given in the markup
    if( ( txt= (TextBox)ctrl.FindControl("Text1"))!=null){
        Response.Write(txt.Text);
    }
}


项目中有一篇文章可能会有所帮助. 链接 [
There is an article in project which might help. Link[^]


这篇关于ASP获取控件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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