我有一个带tempaltefield按钮的gridview, 我想在选定的按钮行中创建一个具有单元格值的会话, 任何人都可以帮我,我试过这个,但没有工作:
protected void ImageButton1_Click1(object sender, ImageClickEventArgs e) { Session["mysession"] = GridView1.SelectedRow.Cells[1].Text; }
答案 0 :(得分:0)
首先,如果它只是模板字段中的图像按钮,实际上你不选择de row。这行可能会抛出异常,因为SelectedRow为null。 但是如果你使用命令来选择,那是正确的。可能您的事件(ImageButton1_Click1)未分配给您的图像(OnClick)。
您可以尝试这样的事情:
protected void Page_Load(object sender, EventArgs e)
{
try
{
//Add and event RowDataBound
grvGrid.RowDataBound += new GridViewRowEventHandler(grvGrid_RowDataBound);
}
catch
{
//...throw
}
}
protected void grvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.Header)
{
//...
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Add an ImageButton foreach row in GridView
ImageButton ibtImageAlt = new ImageButton();
ibtImageAlt.ImageUrl = "App_Images/y.gif";
//ImageButton's ID will be the index of the row
ibtImageAlt.ID = e.Row.RowIndex.ToString();
ibtImageAlt.ForeColor = System.Drawing.Color.White;
ibtImageAlt.Font.Overline = false;
ibtImageAlt.Click += ibtImageAlt_Click;
}
}
catch
{
//...throw
}
}
protected void ibtImageAlt_Click(object sender, EventArgs e)
{
try
{
//Catch the ImageButton ID and the row in GridView
//An example to catch the value of the row selected by the ImageButton
Int32 intIndexRow = Convert.ToInt32(((ImageButton)sender).ID);
String strTest = grvGrid.Rows[intIndexRow].Cells[0].Text;
}
catch
{
//...throw
}
}