English 中文(简体)
环形控制
原标题:Looping through asp:CheckBox controls
  • 时间:2012-04-18 15:48:17
  •  标签:
  • asp.net

我的网页上有11项控制,全部是检查箱。 报告载于总页。

我可以完成我所希望的工作:

generalInformation.InputAttributes.Add( "class", "SetupChecklist" );
generalInformation2.InputAttributes.Add( "class", "SetupChecklist" );
generalInformation3.InputAttributes.Add( "class", "SetupChecklist" );

Ec.

我现在试图通过这些草案,并做同样的事情来挽救自己某些法典,但我遇到了很多困难,难以妥善工作,我根本无法工作。

谁能给我一个良好的途径,通过这11个检查箱进行控制,并添加“板球清单”的盒子?

我尝试了这一点,而且出于某种原因,没有增加这一类别。

protected void InitializeCheckboxes ()
    {
        //generalInformation.InputAttributes.Add( "class", "SetupChecklist" );
        var allCheckBoxes = Page.Controls.OfType<CheckBox>();
        foreach ( var c in allCheckBoxes )
        {
            c.InputAttributes.Add( "class", "SetupChecklist" );
        } 
    }

我用<代码>InitializeCheck Boxes( in the page_Load methods. 当我刚才使用一般信息时,它确实发挥了作用。 InputAttribues. 页: 1 但并非在我通过它们的时候。 任何建议?

最佳回答

最好将其放在<条码>中(作为四分之一提交)或其他集装箱控制。 之后,您可查阅有关准则的参考资料OfType:

// assuming all checkboxes are in a panel named "SetupContainer"
var allCheckBoxes = SetupContainer.Controls.OfType<CheckBox>();
foreach(var chb in allCheckBoxes)
    chb.InputAttributes.Add( "class", "SetupChecklist" );

当然,你也可以利用它来找到整个页上的所有核对结果,但可能会出现错误。

问题回答

无需测试,但可以帮助。

foreach(Control oControl in Page.Controls)
{
  if(oControl is CheckBox && ((CheckBox)oControl).ID.StartsWith("generalInformation") )
   ((CheckBox)oControl).InputAttributes.Add( "class", "SetupChecklist" );
}

由于<代码>runat = “server”,您的检查箱将如下所示。

<span class="SetupChecklist" class="SetupChecklist" name="generalInformation">
     <input id="generalInformation" type="checkbox" name="generalInformation" />
</span>

JQuery

<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.js"></script>
<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $(this).find("input[type= checkbox ]").addClass( GuestClass );
    });
</script>

这将节省你的时间,以便采取以下步骤。

  1. From Client goes to
  2. IIS Web Server
  3. ISAPI Extension
  4. ISAPI Extension loads/execute/converts aspx to HTML
  5. sends back to IIS Web Server.
  6. IIS responds back to client
public void GetUserControls(ControlCollection controls)
{
    foreach (Control ctl in controls)
    {
        if (ctl is CheckBoxOrWhateverControlTypeYouWant)
        {
             /// Add attribute
        }

        if (ctl.Controls.Count > 0)
            GetUserControls(ctl.Controls);
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签