Prøv at se nedenstående principkode:
// aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="mccannon.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="
http://schemas.microsoft.com/intellisense/ie5"> </HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
</asp:DropDownList>
<asp:Table id="Table1" runat="server"></asp:Table>
</form>
</body>
</HTML>
// og codebehind
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace mccannon
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Table Table1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
FillTable(5, "load");
}
private void FillTable(int rows, string message)
{
// this.Table1.Rows.Clear();
TableRow tr = null;
TableCell tac = null;
for(int i=0;i<rows;i++)
{
tr = new TableRow();
tac = new TableCell();
tac.Text = String.Format("{0} nummer {1}", message, i.ToString());
tr.Cells.Add(tac);
this.Table1.Rows.Add(tr);
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
private void Button1_Click(object sender, System.EventArgs e)
{
FillTable(10, "button");
}
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
FillTable(15, "ddlist");
}
}
}
Den mener jeg opfører sig helt efter hensigten... kan du reproducere din problem med den kode ?