how to get click event from a button in gridview
I am working on a app where i need to get the click event of a button in gridview. I am not getting it in row command may be because i am adding it dynamically as follows:
protected void gviewTemplate_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text != e.Row.Cells[2].Text)
{
e.Row.BackColor = System.Drawing.Color.Red;
LinkButton lbtnReload = new LinkButton();
lbtnReload.CommandArgument = e.Row.Cells[12].Text;
lbtnReload.Attributes.Add("onclick", "javascript:ShowDiv()");
lbtnReload.CommandName = "reload";
lbtnReload.Text = "Reload开发者_如何学JAVA";
e.Row.Cells[1].Controls.Add(lbtnReload);
DataTable dt = (DataTable)ViewState["update"];
DataRow dr = dt.NewRow();
dr["id"] = e.Row.Cells[0].ToString();
dr["image"] = e.Row.Cells[1].ToString();
dt.Rows.Add(dr);
}
}
}
protected void gviewTemplate_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "reload")
{
hdnfield.value = int.Parse(e.CommandArgument.ToString());
}
btnFuImage.Visible = true; fuploadImage.Visible = true;
}
I have displlayed it in gridview in following manner
<asp:BoundField ItemStyle-Width="100" DataField="Uploded" HeaderText="Uploaded Image" >
I need to get values from first column of this grid and update it into some hidden variable so that i can use it later. How will i do it. Thanks in advance
For a button, use a ButtonField and specify a commandname, which will fire the rowcommand event. BoundField doesn't render a button. If you want to click a boundfield, that's a more specialized setup...
Which are you trying to use as the click?
If you are just trying to grab one value and store it in a HiddenField for later why dont you just put that value in a javascript function and put it in the HiddenField from there
lbtnReload.Attributes.Add("onclick", "javascript:ShowDiv(); SaveValue('" + value + "'); return false;);
This will prevent you from having to go to the server which will be much faster
Also why dont you just use a TemplateField and make a button in there it will be easier to control everything yourself through that
精彩评论