首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

急怎么把字符串赋予为控件的ID值

2013-07-16 
急急急!!!如何把字符串赋予为控件的ID值在aspx页面上,for(int i1i6i++){string mbtn+i}现在m的值

急急急!!!如何把字符串赋予为控件的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
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 1; i <= 5; i++)
                {
                    Button button = this.Page.FindControl("btn" + i) as Button;
                    button.Text = "hello: btn" + i;
                }

            }
        }
    }
}


[解决办法]
没看懂你的意思,FindControl();
[解决办法]
LZ你为什么不在生成button的时候就给id赋值呢?

热点排行