C# ASP.net 使用 ImageButton 填充 GridView
C# ASP.net Populate GridView with ImageButton
我是 c# 的新手,asp.net 和 Visual Studio,我通常的阵容是 Java 与 Eclipse(和 Captain Casa)。
所以我的实际问题如下。
DataTable lowerTable = new DataTable();
// create columns
for (int i = 0; i < logic.gameSize; i++)
{
lowerTable.Columns.Add();
}
for (int j = 0; j < logic.gameSize; j++)
{
// create a DataRow using .NewRow()
DataRow row = lowerTable.NewRow();
// iterate over all columns to fill the row
for (int i = 0; i < logic.gameSize; i++)
{
ImageButton button = new ImageButton();
button.ImageUrl = "";
button.AlternateText = logic.getValue(logic.bord.getLowerBord()[j, i].getFigur()).ToString();
row[i] = button;
}
// add the current row to the DataTable
lowerTable.Rows.Add(row);
LowerField.Controls.Add(new ImageButton());
LowerField.DataSource = lowerTable;
try
{
LowerField.DataBind();
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.ToString());
}
}
}
我正在创建一个 DataTable 来填充我的 GridView ...但结果不是预期的充满按钮的网格,而是充满文本的网格:System.Web.UI.WebControls.ImageButton。
我已经尝试使用全球化的 Button,但这也无济于事。
我是否忽略了一个简单的机械师?我实际上找不到任何符合 Google 的东西,或者至少找不到任何符合我实际需要的东西。
希望你能帮上忙
谢谢
比恩沃尔夫
您可以使用如下模板字段在网格视图中创建按钮:
//Put the a key value in DataKeyNames so you can get it in case of editing
<asp:GridView ID="gdvValores" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="btnEditar" CssClass="btnEditar" runat="server" OnClick="btnEditar_Click" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="35px" />
</asp:TemplateField>
并且在 .cs 中
protected void btnEditar_Click(object sender, EventArgs e)
{
//Get rowidex of gridview
int linha = ((System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.Control)(sender)).BindingContainer)).RowIndex;
//Get the ID in case of editing a item
ViewState["codigo"] = gdvValores.DataKeys[linha]["id"].ToString();
}
我是 c# 的新手,asp.net 和 Visual Studio,我通常的阵容是 Java 与 Eclipse(和 Captain Casa)。
所以我的实际问题如下。
DataTable lowerTable = new DataTable();
// create columns
for (int i = 0; i < logic.gameSize; i++)
{
lowerTable.Columns.Add();
}
for (int j = 0; j < logic.gameSize; j++)
{
// create a DataRow using .NewRow()
DataRow row = lowerTable.NewRow();
// iterate over all columns to fill the row
for (int i = 0; i < logic.gameSize; i++)
{
ImageButton button = new ImageButton();
button.ImageUrl = "";
button.AlternateText = logic.getValue(logic.bord.getLowerBord()[j, i].getFigur()).ToString();
row[i] = button;
}
// add the current row to the DataTable
lowerTable.Rows.Add(row);
LowerField.Controls.Add(new ImageButton());
LowerField.DataSource = lowerTable;
try
{
LowerField.DataBind();
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.ToString());
}
}
}
我正在创建一个 DataTable 来填充我的 GridView ...但结果不是预期的充满按钮的网格,而是充满文本的网格:System.Web.UI.WebControls.ImageButton。 我已经尝试使用全球化的 Button,但这也无济于事。 我是否忽略了一个简单的机械师?我实际上找不到任何符合 Google 的东西,或者至少找不到任何符合我实际需要的东西。
希望你能帮上忙 谢谢 比恩沃尔夫
您可以使用如下模板字段在网格视图中创建按钮:
//Put the a key value in DataKeyNames so you can get it in case of editing
<asp:GridView ID="gdvValores" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="btnEditar" CssClass="btnEditar" runat="server" OnClick="btnEditar_Click" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="35px" />
</asp:TemplateField>
并且在 .cs 中
protected void btnEditar_Click(object sender, EventArgs e)
{
//Get rowidex of gridview
int linha = ((System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.Control)(sender)).BindingContainer)).RowIndex;
//Get the ID in case of editing a item
ViewState["codigo"] = gdvValores.DataKeys[linha]["id"].ToString();
}