LINQ查询WebControl.Controls [英] LINQ query to WebControl.Controls

查看:118
本文介绍了LINQ查询WebControl.Controls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上三个TextBox控件

I have three TextBox controls on the page

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
        OnTextChanged="TextBox_TextChanged" TabIndex="1">
<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True"
        OnTextChanged="TextBox_TextChanged" TabIndex="2">
<asp:TextBox ID="TextBox3" runat="server" AutoPostBack="True"
        OnTextChanged="TextBox_TextChanged" TabIndex="3">

和事件处理程序

protected void TextBox_TextChanged(object sender, EventArgs e)
{
    WebControl changed_control = (WebControl)sender;

    var next_controls = from WebControl control in changed_control.Parent.Controls
                        where control.TabIndex > changed_control.TabIndex
                        orderby control.TabIndex
                        select control;

    next_controls.DefaultIfEmpty(changed_control).First().Focus();
}

这code的意思是自动选择文本框旁边的TabIndex页面后回来后(见<一href=\"http://stackoverflow.com/questions/173810/autopostback-with-textbox-loses-focus-question-made-clearer\">Little JB的问题)。在现实中,我收到InvalidCastException的,因为它是不可能从System.Web.UI.LiteralControl投(WebControl.Controls实际上包含LiteralControls),以System.Web.UI.WebControls.WebControl。

The meaning of this code is to automatically select TextBox with next TabIndex after page post back (see Little JB's problem). In reality I receive InvalidCastException because it's impossible to cast from System.Web.UI.LiteralControl (WebControl.Controls contains actually LiteralControls) to System.Web.UI.WebControls.WebControl.

我很感兴趣的是它可能以某种方式修改这个形式给出接收工作的解决方案?谢谢!

I am interested is it possible to modify this aproach somehow to receive working solution? Thank you!

推荐答案

OfType

from control in changed_control
  .Parent
  .Controls
  .OfType<WebControl>()

这篇关于LINQ查询WebControl.Controls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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