我正在实现一个每个单元格内部带有按钮的gridview。但是我不明白为什么gridview单元格内的按钮没有触发。我使用gridview RowDataBound和RowCommand来检查按钮是否触发。我还在gridview外部创建了一个按钮,但是它按预期工作。我真的不明白为什么单击一次后gridview单元上的按钮不会触发。
在后台代码上生成gridview。
DataTable dataTable = new DataTable();
dataTable.Columns.AddRange(new DataColumn[32]
{
new DataColumn("Month"),
new DataColumn("Day1"),
new DataColumn("Day2"),
new DataColumn("Day3"),
new DataColumn("Day4"),
new DataColumn("Day5"),
new DataColumn("Day6"),
new DataColumn("Day7"),
new DataColumn("Day8"),
new DataColumn("Day9"),
new DataColumn("Day10"),
new DataColumn("Day11"),
new DataColumn("Day12"),
new DataColumn("Day13"),
new DataColumn("Day14"),
new DataColumn("Day15"),
new DataColumn("Day16"),
new DataColumn("Day17"),
new DataColumn("Day18"),
new DataColumn("Day19"),
new DataColumn("Day20"),
new DataColumn("Day21"),
new DataColumn("Day22"),
new DataColumn("Day23"),
new DataColumn("Day24"),
new DataColumn("Day25"),
new DataColumn("Day26"),
new DataColumn("Day27"),
new DataColumn("Day28"),
new DataColumn("Day29"),
new DataColumn("Day30"),
new DataColumn("Day31")
});
DataRow dataRow;
var months = new string[] { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
for (int i = 0; i < 12; i++)
{
dataRow = dataTable.NewRow();
dataRow["Month"] = months[i];
for (int j = 0; j < 30; j++)
{
}
dataTable.Rows.Add(dataRow);
}
GridView.DataSource = dataTable;
GridView.DataBind();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDataSources();
}
}
在单元格上呈现按钮。
protected void GridViewPricing_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 1; i <= 31; i++)
{
var button = new Button();
button.ID = string.Format("{0}", i);
button.Attributes.Add("style", "display:block; padding:4px; width:100%; height:100%;");
button.CommandName = "ButtonCell";
button.CommandArgument = "ButtonCell";
e.Row.Cells[i].Controls.Add(button);
}
}
}
测试一次单击是否触发按钮。
protected void ButtonCell_Click(object sender, EventArgs e)
{
Response.Write("Button Click");
}
protected void GridViewPricing_RowCommand(object sender, GridViewCommandEventArgs e)
{
Response.Write("Button Click");
}
这是供查看的。
<asp:GridView ID="GridView"
runat="server"
AutoGenerateColumns="false"
CellPadding="0"
CellSpacing="0"
OnRowDataBound="GridViewPricing_RowDataBound"
OnRowCommand="GridViewPricing_RowCommand">
<Columns>
<asp:BoundField DataField="Month" HeaderText="" />
<asp:BoundField DataField="Day1" HeaderText="1" />
<asp:BoundField DataField="Day2" HeaderText="2" />
<asp:BoundField DataField="Day3" HeaderText="3" />
<asp:BoundField DataField="Day4" HeaderText="4" />
<asp:BoundField DataField="Day5" HeaderText="5" />
<asp:BoundField DataField="Day6" HeaderText="6" />
<asp:BoundField DataField="Day7" HeaderText="7" />
<asp:BoundField DataField="Day8" HeaderText="8" />
<asp:BoundField DataField="Day9" HeaderText="9" />
<asp:BoundField DataField="Day10" HeaderText="10" />
<asp:BoundField DataField="Day11" HeaderText="11" />
<asp:BoundField DataField="Day12" HeaderText="12" />
<asp:BoundField DataField="Day13" HeaderText="13" />
<asp:BoundField DataField="Day14" HeaderText="14" />
<asp:BoundField DataField="Day15" HeaderText="15" />
<asp:BoundField DataField="Day16" HeaderText="16" />
<asp:BoundField DataField="Day17" HeaderText="17" />
<asp:BoundField DataField="Day18" HeaderText="18" />
<asp:BoundField DataField="Day19" HeaderText="19" />
<asp:BoundField DataField="Day20" HeaderText="20" />
<asp:BoundField DataField="Day21" HeaderText="21" />
<asp:BoundField DataField="Day22" HeaderText="22" />
<asp:BoundField DataField="Day23" HeaderText="23" />
<asp:BoundField DataField="Day24" HeaderText="24" />
<asp:BoundField DataField="Day25" HeaderText="25" />
<asp:BoundField DataField="Day26" HeaderText="26" />
<asp:BoundField DataField="Day27" HeaderText="27" />
<asp:BoundField DataField="Day28" HeaderText="28" />
<asp:BoundField DataField="Day29" HeaderText="29" />
<asp:BoundField DataField="Day30" HeaderText="30" />
<asp:BoundField DataField="Day31" HeaderText="31" />
</Columns>
</asp:GridView>
答案 0 :(得分:0)
尝试一下:
在关闭columns
元素的标记之前添加按钮
<asp:BoundField DataField="Day31" HeaderText="31" />
<asp:ButtonField ButtonType="Button" CommandName="ButtonCommandName" HeaderText="Buttons"
ShowHeader="True" Text="Click Me" ItemStyle-Width="30px" />
</Columns>
在您的GridView_RowCommand中
protected void GridViewPricing_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = GridViewPricing.Rows[index];
if (e.CommandName == "ButtonCommandName")
{
//Your Code here something like
gvRow.Cells[0].Text
}
}