How do I add a buttonfield to my gridview?

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

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>
 
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
 
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.
 
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
}
 
Back
Top