tags:

views:

407

answers:

2

Hi,

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";
            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

A: 

Hello,

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?

Brian
thanks for responding, i cant use buttonfield as my button will not be displayed in each row. It will be displayed on the basis of some condition in rowcommand
pankaj
Hey, ok, well you can use a TemplateField with a button inside of it with a CommandName. In RowDataBound or RowCreated, you can access that button and show or hide it by setting the Visible property.
Brian
A: 

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

jmein
it looks good to me, i am going to try it, can u please explain me the purpose of return false in this line. thanks for replying
pankaj
returning false will prevent it from going to the server. Whenever you click a button and you dont need it to go to the server (like in this case) you need to add return false because by default it is return true which causes a postback.
jmein
okkkkk jmein, thanks for the knowledge, i once saw this thing earlier but could not understand what it meant for but now i know it and yes ur code did help me, thanks again
pankaj
Also since you did not have return false in there in the first place that might be why your RowCommand wasnt getting hit. You might want to check that out to verify, just so you know in the future. However, in this case, a purely javascript solution seems to be best.
jmein