How do I add a buttonfield to my gridview?

Discussion in 'ASP .Net' started by Mike P, Jun 9, 2008.

  1. Mike P

    Mike P Guest

    I have a grid view that I have formatted using table objects (see
    below). But now I want to add a buttonfield within <td> tags, it will
    not let me. How do I add a buttonfield?

    <asp:GridView ID="GridView1" runat="server"
    DataSourceID="SqlDataSource1" ShowHeader="false"
    BackColor="#dddddd" DataKeyNames="ProjectID,TaskID"
    AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <table width="450">
    <tr>
    <td width="100px">
    <asp:label
    id="lblProjectName" Runat="server" Font-Bold="true" BackColor="Red"
    ForeColor="White">PROJECT NAME</asp:label>
    </td>
    <td width="300px" align=left>
    <asp:Label
    ID="lblProjectName2" runat="server" Text='<%# Bind("ProjectName") %>'
    Font-Bold="true" BackColor="red" ForeColor="White"></asp:Label>
    </td>
    </tr>
    <tr>
    <td width="100px">
    <asp:label
    id="lblProjectAssignedTo" Runat="server">Assigned To</asp:label>
    </td>
    <td width="300px" align=left>
    <asp:Label
    ID="lblProjectAssignedTo2" runat="server" Text='<%#
    Bind("ProjectAssignedToName") %>'></asp:Label>
    </td>
    </tr>
    <tr>
    <td width="100px">
    <asp:label
    id="lblProjectStartDate" Runat="server">Start Date/Time</asp:label>
    </td>
    <td width="300px" align=left>
    <asp:Label
    ID="lblProjectStartDate2" runat="server" Text='<%#
    Bind("ProjectStartDate") %>'></asp:Label>
    </td>
    </tr>
    <tr>
    <td width="100px">
    <asp:label
    id="lblProjectEndDate" Runat="server">End Date/Time</asp:label>
    </td>
    <td width="300px" align=left>
    <asp:Label
    ID="lblProjectEndDate2" runat="server" Text='<%# Bind("ProjectEndDate")
    %>'></asp:Label>
    </td>
    </tr>
    <tr>
    <td>
    //trying to add a button
    field here
    </td>
    </tr>
    </table>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>




    *** Sent via Developersdex http://www.developersdex.com ***
    Mike P, Jun 9, 2008
    #1
    1. Advertising

  2. Mike P

    Munna Guest

    Hi

    i add a button and worked okay for me...

    <asp:GridView ID="GridView1" runat="server"
    DataSourceID="SqlDataSource1" ShowHeader="false"
    BackColor="#dddddd" DataKeyNames="ProjectID,TaskID"
    AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <table width="450">
    <tr>
    <td width="100px">
    <asp:Label ID="lblProjectName"
    runat="server" Font-Bold="true" BackColor="Red"
    ForeColor="White">PROJECT NAME</asp:Label>
    </td>
    <td width="300px" align="left">
    <asp:Label ID="lblProjectName2"
    runat="server" Text='<%# Bind("ProjectName") %>'
    Font-Bold="true" BackColor="red"
    ForeColor="White"></asp:Label>
    </td>
    </tr>
    <tr>
    <td width="100px">
    <asp:Label ID="lblProjectAssignedTo"
    runat="server">Assigned To</asp:Label>
    </td>
    <td width="300px" align="left">
    <asp:Label ID="lblProjectAssignedTo2"
    runat="server" Text='<%# Bind("ProjectAssignedToName") %>'></
    asp:Label>
    </td>
    </tr>
    <tr>
    <td width="100px">
    <asp:Label ID="lblProjectStartDate"
    runat="server">Start Date/Time</asp:Label>
    </td>
    <td width="300px" align="left">
    <asp:Label ID="lblProjectStartDate2"
    runat="server" Text='<%# Bind("ProjectStartDate") %>'></asp:Label>
    </td>
    </tr>
    <tr>
    <td width="100px">
    <asp:Label ID="lblProjectEndDate"
    runat="server">End Date/Time</asp:Label>
    </td>
    <td width="300px" align="left">
    <asp:Label ID="lblProjectEndDate2"
    runat="server" Text='<%# Bind("ProjectEndDate")%>'></asp:Label>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Button runat="server"
    ID="mydesiredButton" Text="DesiredButton" />
    </td>
    </tr>
    </table>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

    Best of luck

    Munna
    www.munna.shatkotha.com
    www.munna.shatkotha.com/blog
    www.shatkotha.com
    Munna, Jun 9, 2008
    #2
    1. Advertising

  3. Mike P

    Mike P Guest

    When I add a Button rather than a ButtonField (which I currently can't
    add), when I click on the button I get the following error :

    Invalid postback or callback argument. Event validation is enabled
    using <pages enableEventValidation="true"/> in configuration or <%@ Page
    EnableEventValidation="true" %> in a page. For security purposes, this
    feature verifies that arguments to postback or callback events originate
    from the server control that originally rendered them. If the data is
    valid and expected, use the
    ClientScriptManager.RegisterForEventValidation method in order to
    register the postback or callback data for validation.


    *** Sent via Developersdex http://www.developersdex.com ***
    Mike P, Jun 9, 2008
    #3
  4. Mike P

    Munna Guest

    Hi

    Give the button a command name

    <asp:Button ID="somebutton" runat="server" Text="Abutton"
    CommandName="myCommandName" />

    subscript the itemcommand event of the datagrid or gridview

    and on item command

    protected void ListView1_ItemCommand(object sender,
    ListViewCommandEventArgs e)
    {
    if (e.CommandName == "myCommandName")
    {
    //put your code here
    }

    Best of luck

    Munna
    www.munna.shatkotha.com
    www.munna.shatkotha.com/blog
    www.shatkotha.com
    }
    Munna, Jun 9, 2008
    #4
  5. Mike P

    Mike P Guest

    Mike P, Jun 9, 2008
    #5
  6. Mike P

    Munna Guest

    Munna, Jun 9, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edwin Knoppert
    Replies:
    1
    Views:
    5,262
    Edwin Knoppert
    Nov 18, 2005
  2. Replies:
    0
    Views:
    5,810
  3. =?Utf-8?B?bGFuZW0=?=

    How to get text of gridview buttonfield

    =?Utf-8?B?bGFuZW0=?=, Dec 14, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    15,281
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Dec 14, 2005
  4. Rob Roberts
    Replies:
    2
    Views:
    15,783
    Rob Roberts
    Apr 11, 2006
  5. Replies:
    3
    Views:
    9,810
Loading...

Share This Page