0

我有一个非常直接的动态生成的 asp 页面。这是我第一次使用动态生成的 asp 控件。该表正确呈现,但是当我单击“查询”按钮时,我无法让事件连接到“OnClick”方法。我收到网页错误“'ButtonClick' 未定义”。为了简洁起见,我已经剥离并简化了代码。

设备状态.aspx

<%@ Page Title="Device Status" Language="C#" MasterPageFile="Secure.master"
'AutoEventWireup="true" CodeFile="DeviceStatus.aspx.cs" Inherits="Devices" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<div id="selectDiv" runat="server"></div>

</asp:Content>

设备状态.aspx.cs

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


public partial class Devices : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e) {

    int i, NumberOfDevices = 3;
    string Contents;
    string[] number = new string[] { "1", "2", "3"};
    string[] name = new string[] {"mike", "bob", "cindy"};
    string[] location = new string[] { "Austin", "Miami", "Atlanta" };     

    // Create event binding
    PlaceHolder Placeholder1 = new PlaceHolder();
    Button button = new Button();
    button.Text = "Click";
    button.Click += new System.EventHandler(ButtonClick);
    Placeholder1.Controls.Add(button);

    //Create Form and table header
    Contents = "<form runat=\"server\"><table border=\"1\" >";
    Contents += "<tr><th>Select</th><th>Number</th><th>Name</th><th>Status</tr>";

    // Repeat for each table row
    for (i = 0; i < NumberOfDevices; i++) {

        // Creat each row
        Contents += "<tr><td><asp:Button ID=\"row" + i + "\""    
             + "runat=\"server\"OnClick=\"ButtonClick\" >Query</asp:Button></td>"
             + "<td>" + number[i] + "</td><td>" + name[i] + "</td><td>"
             + location[i] + "</td></tr>";
    }

    // Cleanup
    Contents += "</table>";
    Contents += "<asp:PlaceHolder ID=\"Placeholder1\" runat=\"server\"></asp:PlaceHolder>";
    Contents += "</form>";
    Contents += "</asp:Content>";

    //Place dynamic asp controls in webpage
    selectDiv.InnerHtml = Contents; 
}


protected void ButtonClick(object sender, EventArgs e)
{
    Response.Redirect("DataRendering.aspx");
}



}
4

1 回答 1

0

我接受了加里森的建议。花了一些时间来弄清楚代码,但一旦完成,这是最直接的解决方案。感谢大家的贡献。

于 2013-09-20T20:38:29.577 回答