DataGrid没法响应ItemCommand事件,求教
展开全部
4种方法都可以实现
如果你不行,就直接拷贝粘贴源代码存成test.aspx浏览
HTML code<%@ Page Language="C#" EnableViewState="true" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public System.Data.DataTable CreateDataTable()
{
System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser");
System.Data.DataRow dr;
dataTable1.Columns.Add(new System.Data.DataColumn("Id", typeof(System.Int32)));
dataTable1.Columns.Add(new System.Data.DataColumn("Title", typeof(System.String)));
for (int i = 0; i < 8; i++)
{
dr = dataTable1.NewRow();
dr[0] = i;
dr[1] = Guid.NewGuid().ToString();
dataTable1.Rows.Add(dr);
}
return dataTable1;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Data_g.DataSource = this.CreateDataTable();
Data_g.DataBind();
}
}
protected void Data_g_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandName == "See")
{
Response.Write("See 引发");
}
if (e.CommandName == "SeeSee")
{
Response.Write("SeeSee 引发");
}
Response.Write("<li>该行Id=" + Data_g.DataKeys[e.Item.ItemIndex].ToString());
}
protected void see3_Click(object sender, EventArgs e)
{
Button s = sender as Button;
Response.Write("<li>方法3得到的Id=" + s.CommandArgument);
}
protected void see4_Click(object sender, EventArgs e)
{
Button s = sender as Button;
DataGridItem item = s.NamingContainer as DataGridItem;
Response.Write("<li>方法4得到的Id" + Data_g.DataKeys[item.ItemIndex].ToString());
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid ID="Data_g" runat="server" OnItemCommand="Data_g_ItemCommand" DataKeyField="Id"
AutoGenerateColumns="false">
<Columns>
<asp:ButtonColumn ButtonType="PushButton" CommandName="SeeSee" Text="查看密保卡方法1"></asp:ButtonColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button ID="see" runat="server" Text="查看密保卡方法2" CommandName="See" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button ID="see3" runat="server" Text="查看密保卡方法3" OnClick="see3_Click" CommandArgument='<%#Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button ID="see4" runat="server" Text="查看密保卡方法4" OnClick="see4_Click" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
如果你不行,就直接拷贝粘贴源代码存成test.aspx浏览
HTML code<%@ Page Language="C#" EnableViewState="true" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public System.Data.DataTable CreateDataTable()
{
System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser");
System.Data.DataRow dr;
dataTable1.Columns.Add(new System.Data.DataColumn("Id", typeof(System.Int32)));
dataTable1.Columns.Add(new System.Data.DataColumn("Title", typeof(System.String)));
for (int i = 0; i < 8; i++)
{
dr = dataTable1.NewRow();
dr[0] = i;
dr[1] = Guid.NewGuid().ToString();
dataTable1.Rows.Add(dr);
}
return dataTable1;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Data_g.DataSource = this.CreateDataTable();
Data_g.DataBind();
}
}
protected void Data_g_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandName == "See")
{
Response.Write("See 引发");
}
if (e.CommandName == "SeeSee")
{
Response.Write("SeeSee 引发");
}
Response.Write("<li>该行Id=" + Data_g.DataKeys[e.Item.ItemIndex].ToString());
}
protected void see3_Click(object sender, EventArgs e)
{
Button s = sender as Button;
Response.Write("<li>方法3得到的Id=" + s.CommandArgument);
}
protected void see4_Click(object sender, EventArgs e)
{
Button s = sender as Button;
DataGridItem item = s.NamingContainer as DataGridItem;
Response.Write("<li>方法4得到的Id" + Data_g.DataKeys[item.ItemIndex].ToString());
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid ID="Data_g" runat="server" OnItemCommand="Data_g_ItemCommand" DataKeyField="Id"
AutoGenerateColumns="false">
<Columns>
<asp:ButtonColumn ButtonType="PushButton" CommandName="SeeSee" Text="查看密保卡方法1"></asp:ButtonColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button ID="see" runat="server" Text="查看密保卡方法2" CommandName="See" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button ID="see3" runat="server" Text="查看密保卡方法3" OnClick="see3_Click" CommandArgument='<%#Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button ID="see4" runat="server" Text="查看密保卡方法4" OnClick="see4_Click" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
威孚半导体技术
2024-08-19 广告
2024-08-19 广告
威孚(苏州)半导体技术有限公司是一家专注生产、研发、销售晶圆传输设备整机模块(EFEM/SORTER)及核心零部件的高科技半导体公司。公司核心团队均拥有多年半导体行业从业经验,其中技术团队成员博士、硕士学历占比80%以上,依托丰富的软件底层...
点击进入详情页
本回答由威孚半导体技术提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询