我有20至30行和3列的表。我想添加按鈕或第四列的單元格,我可以點擊它。而onClick事件中,我需要獲取有關發生此行的信息。我可以在ASP列表中添加按鈕並獲取點擊哪個行按鈕?
表以編程方式即時生成。
可以這樣做,我乞求一些例子。
編輯2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace HelpdeskOsControl
{
public partial class Test : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//GenerateTable(getTestData());
}
private List<string> getTestData()
{
List<string> tData = new List<string>();
for (int i = 0; i < 10; i++)
{
tData.Add("proc" + i + "_" + new Random().Next(100) + "_" + new Random().Next(100));
}
return tData;
}
protected void btnClear_Click(object sender, EventArgs e)
{
for (int i = Processes.Rows.Count; i > 1; i--)
{
Processes.Rows.RemoveAt(i - 1);
}
}
protected void btnLoad_Click(object sender, EventArgs e)
{
GenerateTable(getTestData());
}
protected void btnKill_Click(object sender, EventArgs e)
{
lblView.Text = ((Button)sender).ID;
}
private void GenerateTable(List<string> list)
{
int st = 0;
foreach (string line in list)
{
TableRow tr = new TableRow();
Processes.Controls.Add(tr);
foreach(String str in line.Split('_'))
{
int index = tr.Cells.Add(new TableCell());
tr.Cells[index].Text = str;
}
Button b = new Button();
b.Text = "Kill " + st;
b.ID = "btnKill_" + st;
b.Click += new EventHandler(btnKill_Click);
TableCell tc = new TableCell();
tc.Controls.Add(b);
tr.Cells.Add(tc);
tr.TableSection = TableRowSection.TableBody;
Processes.Rows.Add(tr);
st++;
}
Processes.BorderStyle = BorderStyle.Solid;
Processes.GridLines = GridLines.Both;
Processes.BorderWidth = 2;
}
}
}
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Test.ascx.cs" Inherits="HelpdeskOsControl.Test" %>
<asp:Panel ID="Panel1" runat="server" Height="465px" Width="417px">
<asp:Table ID="Processes" runat="server" Height="20px" Width="400px" CssClass="tablesorter">
<asp:TableHeaderRow ID="ProcessesHeader" runat="server"
TableSection="TableHeader">
<asp:TableHeaderCell ID="TableHeaderCell1" runat="server">Name</asp:TableHeaderCell>
<asp:TableHeaderCell ID="TableHeaderCell2" runat="server">CPU</asp:TableHeaderCell>
<asp:TableHeaderCell ID="TableHeaderCell3" runat="server">Memory</asp:TableHeaderCell>
<asp:TableHeaderCell ID="TableHeaderCell4" runat="server"></asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
<asp:Panel ID="Panel2" runat="server">
<asp:Button ID="btnLoad" runat="server" onclick="btnLoad_Click" Text="Load" />
<asp:Button ID="btnClear" runat="server" onclick="btnClear_Click"
Text="Clear" />
<asp:Label ID="lblView" runat="server" Text="Label"></asp:Label>
</asp:Panel>
</asp:Panel>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HelpdeskOsControl._Default" %>
<%@ Register src="Test.ascx" tagname="WebUserControl" tagprefix="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Test Control</p>
<Test:WebUserControl ID="Test" runat="server" />
</div>
</form>
</body>
</html>
可以請人檢查爲什麼當我在Page_Load中的程序註釋掉GenerateTable(getTestData());
這個代碼是行不通的。
你是如何生成此表中的行?如果你使用的是GridView,你可以添加一個CommandField ... http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns.aspx – David 2010-11-01 21:40:10
最好顯示你正在創建這張桌子。該按鈕將作爲第4列的一部分。 – 2010-11-01 21:47:42
我添加了代碼。 – Primoz 2010-11-02 06:57:02