我有一个gridview绑定到这样的数据源:
var beerlist = (from b in ctx.beers
select new
{
id = b.beerid,
b.brand,
b.kind,
etc... }).ToList();
BeerListGridView.DataSource = beerlist;
DataBind();
我在page_load事件中执行此操作:if(!IsPostBack)。
现在我在onrowdatabound事件中添加按钮,如下所示:
if (e.Row.RowType == DataControlRowType.DataRow)
{
String beerid = e.Row.Cells[0].Text;
int columncount = e.Row.Cells.Count;
TableCell MyCell = new TableCell();
Button EditButton = new Button();
EditButton.ID = beerid;
EditButton.Text = "Edit";
EditButton.CommandName = "EditButton_Click";
EditButton.CommandArgument = beerid;
MyCell.Controls.Add(EditButton);
MyCell2.Controls.Add(InfoButton);
e.Row.Cells.AddAt(columncount, MyCell);
e.Row.Cells.AddAt(columncount + 1, MyCell2);
rowcommand事件如下所示:
if (e.CommandName != "Sort")
{
Session["beerid"] = e.CommandArgument;
if (e.CommandName == "EditButton_Click")
{
Response.Redirect("EditBeer.aspx");
}
else if (e.CommandName == "InfoButton_Click")
{
Response.Redirect("ViewRatingsRemarks.aspx");
}
}
在排序事件中,我执行以下操作:
if (e.SortExpression == "brand")
{
if (Session["brandsortorder"] == null)
{
var beerlist = (from b in ctx.beers
orderby b.brand
select new
{
id = b.beerid,
b.brand,
b.kind,
etc... }).ToList();
BeerListGridView.DataSource = beerlist;
DataBind();
Session["brandsortorder"] = "DESC";
}
else if etc..
当我点击编辑按钮时,按钮消失,我在没有按钮的情况下使用datagridview。 我做错了什么?
答案 0 :(得分:0)
答案 1 :(得分:0)
Thanx的建议,我做了以下事情: 将boundfield添加到gridview并将gridview的autogeneratecolumns设置为false:
<asp:GridView ID="BeerListGridView" AutoGenerateColumns="false" OnRowCommand="BeerListGridView_RowCommand" AllowSorting="true" OnSorting="BeerListGridView_Sorting" runat="server">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
<asp:BoundField DataField="brand" HeaderText="brand" SortExpression="brand" />
<asp:BoundField DataField="kind" HeaderText="kind" SortExpression="kind" />
<asp:BoundField DataField="alcperc" HeaderText="alcperc" SortExpression="alcperc" />
<asp:BoundField DataField="taste" HeaderText="taste" SortExpression="taste" />
<asp:BoundField DataField="color" HeaderText="color" SortExpression="color" />
<asp:BoundField DataField="land" HeaderText="land" SortExpression="land" />
<asp:BoundField DataField="brewery" HeaderText="brewery" SortExpression="brewery" />
<asp:BoundField DataField="addedby" HeaderText="addedby" SortExpression="addedby" />
<asp:TemplateField><ItemTemplate><asp:Button Text="edit" CommandName="Edit" CommandArgument='<%# Eval("id") %>' runat="server" /></ItemTemplate></asp:TemplateField>
</Columns>
正如您在最后看到的那样,我添加了带有编辑按钮的列,并填写了commandname和commandargument。有用!!! (onrowdatabound不再是必要的......)