日期:2014-05-17  浏览次数:20774 次

急急急!!!如何把字符串赋予为控件的ID值
在aspx页面上,
for(int i=1;i<6;i++)
{
string m="btn"+i;

}
现在m的值为btn1,btn2,btn3,btn4,btn5我想把一个button控件的id值也为btn1,...btn5.如何关联呢?
我用(button)this.controls.Find(m,true)[0] 系统提示有错,错误信息为system.web.controlcollection不包含Find的定义。

------解决方案--------------------
没太明白你的意思。上面是要给button赋值,下面怎么又是通过id名来find控件?是还没赋ID给他想动态指定ID给控件还是?????
------解决方案--------------------
refer:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="btn1" runat="server" Text="btn1" />
            <asp:Button ID="btn2" runat="server" Text="btn2" />
            <asp:Button ID="btn3" runat="server" Text="btn3" />
            <asp:Button ID="btn4" runat="server" Text="btn4" />
            <asp:Button ID="btn5" runat="server" Text="btn5" />
        </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {