ButtonField in Code Behind

Last post 05-09-2008 10:26 AM by keyboardcowboy. 8 replies.

Sort Posts:

  • ButtonField in Code Behind

    05-08-2008, 3:48 PM
    • Loading...
    • sfast
    • Joined on 01-21-2008, 8:55 PM
    • Posts 54

    I have buttonfield in GridView. How can I access ButtonField in GridView1_RowDataBound. I need to hide this button on basis of a field in the table.

       <asp:buttonfield buttontype="Image" commandname="OK" headertext="Click to OK"
                           ImageUrl="images\green_button.gif" />

     

       string errorFlag = (DataBinder.Eval(e.Row.DataItem, "error_flag")).ToString() ;
                    
                   if ( errorFlag == "X" )  {
                         Button bt = (Button)e.Row.FindControl("OK") ;
                 
                         bt.Visible = false;  }

     

    It doesn;t work.

     

     

     

  • Re: ButtonField in Code Behind

    05-08-2008, 4:01 PM
    • Loading...
    • lberan
    • Joined on 02-12-2008, 2:23 PM
    • Pittsburgh, PA
    • Posts 274

    i have just learned this from Ed.

    http://forums.asp.net/t/1258625.aspx

    Lynn

    Please remember to click "Mark as Answer" on the post to all the replies that have helped you so that these answers could help others. Thanks.
  • Re: ButtonField in Code Behind

    05-08-2008, 4:04 PM
    • Loading...
    • keyboardcowboy
    • Joined on 06-14-2007, 3:39 PM
    • Thornhill Ontario
    • Posts 770

    find control searches for the control by ID, you did not specify an ID for your button field. <asp:buttonField id="btnfield1"...

    Please remember to click "Mark as Answer" on this post if it helped you.
  • Re: ButtonField in Code Behind

    05-08-2008, 5:37 PM
    • Loading...
    • sfast
    • Joined on 01-21-2008, 8:55 PM
    • Posts 54

     Gave me error

    'System.Web.UI.WebControls.ButtonField' does not have a public property named 'id'.


     

  • Re: ButtonField in Code Behind

    05-08-2008, 6:52 PM
    • Loading...
    • xanderer
    • Joined on 04-17-2008, 4:31 PM
    • Posts 36

     Give the buttonfield a id

  • Re: ButtonField in Code Behind

    05-08-2008, 7:32 PM
    • Loading...
    • dinesh_sp
    • Joined on 12-03-2007, 5:41 AM
    • Melbourne
    • Posts 366

    Because you are finding control by CommandName you are not able to do so, You need to find control by its ID.
    <asp:buttonfield buttontype="Image" ID="buttonName" headertext="Click to OK"
                           ImageUrl="images\green_button.gif" /> 

       string errorFlag = (DataBinder.Eval(e.Row.DataItem, "error_flag")).ToString() ;                 
                   if ( errorFlag == "X" )  {
                        Button bt = ctype(e.Row.FindControl("buttonName") , Button)
                         bt.Visible = false;  }

    Hope this helps.

  • Re: ButtonField in Code Behind

    05-09-2008, 10:13 AM
    • Loading...
    • sfast
    • Joined on 01-21-2008, 8:55 PM
    • Posts 54

     Thankyou all for helping me.

    But ButtonField does not have "id" .

    I get this error -  

    Type 'System.Web.UI.WebControls.ButtonField' does not have a public property named 'id'.


     

  • Re: ButtonField in Code Behind

    05-09-2008, 10:25 AM
    • Loading...
    • lberan
    • Joined on 02-12-2008, 2:23 PM
    • Pittsburgh, PA
    • Posts 274

    convert buttonfield to templatefield.  the link I sent you should help

    Lynn

    Please remember to click "Mark as Answer" on the post to all the replies that have helped you so that these answers could help others. Thanks.
  • Re: ButtonField in Code Behind

    05-09-2008, 10:26 AM
    Answer
    • Loading...
    • keyboardcowboy
    • Joined on 06-14-2007, 3:39 PM
    • Thornhill Ontario
    • Posts 770

    hmm apparently id is not a buttonfield property. You are supposed to reference them by index accoring to msdn 

    To determine the index of the record that raises the command event, use the CommandArgument property of the event argument that is passed to the command event for the data-bound control. The ButtonField class automatically populates the CommandArgument property with the appropriate index value. (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield.aspx)

    Another option is to just wrap the buttonfield in a panel and hide the panel when you want, this will hide the button with it 

    <asp:Panel id="pnlButtonHide" runat="server>
        <asp:ButtonField>
    </asp:Panel>
      
    //When you want to hide
    ((Panel)whereyourpanelis.findControl("pnlButtonHide")).Visible = fasle;
     
    Please remember to click "Mark as Answer" on this post if it helped you.
Page 1 of 1 (9 items)