我有一个gridview,我动态生成绑定字段。我在gridview中的第一列是图像,而其他三列是动态生成的。
当我点击第一列中的图像时,我想要第二列值。我该怎么办?此外,我无法在点击图片按钮时触发事件。
Aspx代码:
<table>
<tr>
<td>
<div style="overflow-y: scroll; max-height: 97.5vh">
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
OnRowCommand="OnRowCommand" Width="500px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" id="EditImage" style="cursor: pointer" height="24" width="24" src="images/plus.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</table>
cs文件
// THIS DISPLAYS THE GRIDVIEW WITH 4 COLUMNS
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = // Calling SP and displaying the results;
// 3 COLUMNS: ID, NAME, TITLE
foreach (DataColumn col in dt.Columns)
{
BoundField bfield = new BoundField();
bfield.DataField = col.ColumnName;
bfield.HeaderText = col.ColumnName;
gvCustomers.Columns.Add(bfield);
}
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
//I THINK, NEED TO WRITE CODE HERE FOR CLICKING ON IMAGE BUTTON
}
答案 0 :(得分:1)
使用图片按钮:
<asp:ImageButton runat="server" ID="imgBtn" CommandArgument='<%#Eval("ValueContainer") %>' ImageUrl="~/images/plus.png" />
on cs:
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
//I THINK, NEED TO WRITE CODE HERE FOR CLICKING ON IMAGE BUTTON
var value = e.CommandArgument;
}
预呈现:
protected void OnPreRender(object sender, EventArgs e)
{
foreach(GridViewRow row in gvCustomers.Rows)
{
var imgBtn = (ImageButton)row.FindControl("imgBtn");
imgBtn.CommandArgument = row.Cells[1].Text;
}
}
答案 1 :(得分:0)
如果您正在使用它来尝试访问BoundField列中的值,并且如果您在示例代码中声明了aspx页面中的按钮,则可以将以下内容用作列。
<asp:ButtonField ButtonType="Image" runat="server" CommandName="MyButton" ImageUrl="~/Images/Update.jpg" />
在守则背后。
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
//CommandArgument returns row index of clicked button
int nCommandArg = Convert.ToInt32(e.CommandArgument.ToString());
return;
}