We help IT Professionals succeed at work.

C# Dataview Template button 2.0

1,295 Views
Last Modified: 2013-12-17
Good day

I have a Asp.net VS2008 on .net 2.0 where I have 3 dataviews each data view contains a view button, when the view button is clicked, I need to catch the value of column[0] in die dataview, the redirect to a 2nd form  passing the value from the column[0] to the second page.
How do i  Catch the click event on the button and get the value from column[0]?

Click on the button and I get:

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.

Thanx
Phillip
HTML 
 
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <pages validateRequest="false" enableEventValidation="false" /> 
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView3" runat="server" BackColor="White" 
        BorderColor="Black" BorderStyle="Groove" BorderWidth="1px" CellPadding="3" 
        GridLines="Vertical" 
        
        
        style="z-index: 1; left: 342px; top: 441px; position: absolute; height: 133px; width: 322px" 
        AutoGenerateColumns="False">
         <Columns>
            <asp:BoundField HeaderText="Province" DataField="Province" />
            <asp:BoundField HeaderText="Test OK" DataField="TestOk" />
            <asp:BoundField HeaderText="Faulty" DataField="Faulty" />
            <asp:TemplateField HeaderText="View">
                <ItemTemplate>
                    <asp:Button ID="View" runat="server" CommandName="Edit" Text="View" 
                        onclick="View_Click" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#CCFF66" Font-Bold="True" ForeColor="Black" 
             BorderColor="Black" BorderStyle="Groove" HorizontalAlign="Center" 
             VerticalAlign="Middle" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>
    <asp:GridView ID="GridView1" runat="server" BackColor="White" 
        BorderColor="Black" BorderStyle="Groove" BorderWidth="1px" CellPadding="3" 
        GridLines="Vertical" 
        
        
        style="z-index: 1; left: 678px; top: 440px; position: absolute; height: 133px; width: 322px" 
        AutoGenerateColumns="False">
         <Columns>
            <asp:BoundField HeaderText="Province" DataField="Province" />
            <asp:BoundField HeaderText="Test OK" DataField="TestOk" />
            <asp:BoundField HeaderText="Faulty" DataField="Faulty" />
            <asp:TemplateField HeaderText="View">
                <ItemTemplate>
                    <asp:Button ID="View" runat="server" CommandName="Edit" Text="View" 
                        onclick="View_Click" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#9933FF" Font-Bold="True" ForeColor="White" 
             BorderColor="Black" BorderStyle="Groove" HorizontalAlign="Center" 
             VerticalAlign="Middle" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>
    <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/FIFA-HIRAC.png" 
        
        
        style="z-index: 1; left: 309px; top: 27px; position: absolute; height: 396px; width: 875px;" />
    <p>
    <asp:GridView ID="GridView2" runat="server" BackColor="White" 
        BorderColor="Black" BorderStyle="Groove" BorderWidth="1px" CellPadding="3" 
        GridLines="Vertical" 
        
        style="z-index: 1; left: 7px; top: 440px; position: absolute; height: 133px; width: 324px" 
        AutoGenerateColumns="False" 
        onselectedindexchanged="GridView2_SelectedIndexChanged">
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <Columns>
            <asp:BoundField HeaderText="Province" DataField="Province" />
            <asp:BoundField HeaderText="Test OK" DataField="TestOk" />
            <asp:BoundField HeaderText="Faulty" DataField="Faulty" />
            <asp:TemplateField HeaderText="View">
                <ItemTemplate>
                    <asp:Button ID="View" runat="server" CommandArgument="Edit" CommandName="Edit" 
                        onclick="View_Click" Text="View" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#0066FF" Font-Bold="True" ForeColor="White" 
            BorderColor="Black" BorderStyle="Groove" HorizontalAlign="Center" 
            VerticalAlign="Middle" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>
    </p>
    </form>
</body>
</html>
 
 
C#
 
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FillDG(GridView2,1);
        FillDG(GridView1,2);
        FillDG(GridView3,3);
        
 
    }
   
    private void FillDG(GridView MyGrid, int Group)
    {
        DataTable dt = new DataTable("Fault Info");
        dt.Columns.Add("Province", System.Type.GetType("System.String"));
        dt.Columns.Add("TestOk", System.Type.GetType("System.String"));
        dt.Columns.Add("Faulty", System.Type.GetType("System.String"));
        object[] x = new object[3];
        if (Group == 1)
        {
            x[0] = "Eastern Cape";
            x[1] = "10";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "Free State";
            x[1] = "8";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Gauteng";
            x[1] = "15";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "KwaZulu-Natal";
            x[1] = "12";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Mpumalanga";
            x[1] = "8";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Limpopo Province";
            x[1] = "8";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Northern Cape";
            x[1] = "7";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "North-West Province";
            x[1] = "6";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Western Cape";
            x[1] = "16";
            x[2] = "2";
            dt.Rows.Add(x);
        }
        else if (Group == 2)
        {
            x[0] = "Eastern Cape";
            x[1] = "15";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Free State";
            x[1] = "10";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "Gauteng";
            x[1] = "20";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "KwaZulu-Natal";
            x[1] = "18";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Mpumalanga";
            x[1] = "12";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "Limpopo Province";
            x[1] = "10";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Northern Cape";
            x[1] = "11";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "North-West Province";
            x[1] = "9";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Western Cape";
            x[1] = "22";
            x[2] = "0";
            dt.Rows.Add(x);
        }
        else
        {
            x[0] = "Eastern Cape";
            x[1] = "10";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Free State";
            x[1] = "8";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Gauteng";
            x[1] = "15";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "KwaZulu-Natal";
            x[1] = "12";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "Mpumalanga";
            x[1] = "8";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Limpopo Province";
            x[1] = "8";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Northern Cape";
            x[1] = "7";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "North-West Province";
            x[1] = "6";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Western Cape";
            x[1] = "16";
            x[2] = "5";
            dt.Rows.Add(x);
        }
        MyGrid.DataSource = dt;
        MyGrid.DataBind();
 
 
        foreach (GridViewRow row in MyGrid.Rows)
        {
            
            Int32 intValue = Convert.ToInt32(row.Cells[2].Text);
            if (intValue == 1)
            {
                row.Cells[2].BackColor = System.Drawing.Color.LightCoral;
            }
            else if (intValue == 2)
            {
                row.Cells[2].BackColor = System.Drawing.Color.Coral;
            }
            else if (intValue == 5)
            {
                row.Cells[2].BackColor = System.Drawing.Color.Red;
            }
            else if (intValue >= 4)
            {
                row.Cells[2].BackColor = System.Drawing.Color.DarkRed;
            }
        }
    }
}

Open in new window

Comment
Watch Question

Commented:
You'll want to set the e.commandarguement value. What this allows you to do is send a value (even a databound one) to your codebehind.

So on your button, you could have something similiar to this:


Code:
<?xml:namespace prefix = asp /> <asp:Button ID="View" runat="server" CommandName="Edit" CommandArgument='<%# Eval("ValueID") %>' Text="View"
                        onclick="View_Click" />


What that does, is give the e.commandarguement.tostring() the same value as the databound column ValueID (obviously put in your own databound column) of the button when you call it in the code behind.

So if you had in your codebehind:


Code:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs ) Handles GridView1.RowCommand
If e.CommandName = "Edit" Then
Response.Write(e.CommandArgument.ToString())
End If
End Sub

It would write whatever the value of the databound clumn is.
 
Let me know if you have any questions

Commented:
Ugh.... it made the first code sample ugly... here it is again without all that extra crap.  Last time I try to post in Rich Text mode here....


<asp:Button ID="View" runat="server" CommandName="Edit" CommandArgument='<%# Eval("ValueID") %>' Text="View" onclick="View_Click" />

Author

Commented:
Hi
 I get  this error o  the HTML side:
'ASP.default_aspx' does not contain a definition for 'View_Click' and no extension method 'View_Click' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)
I inserted the code like this:

<asp:GridView ID="GridView2" runat="server" BackColor="White"
BorderColor="Black" BorderStyle="Groove" BorderWidth="1px" CellPadding="3"
GridLines="Vertical"

style="z-index: 1; left: 10px; top: 307px; position: absolute; height: 133px; width: 324px"
AutoGenerateColumns="False" > 
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<Columns>
<asp:BoundField HeaderText="Province" DataField="Province" />
<asp:BoundField HeaderText="Test OK" DataField="TestOk" />
<asp:BoundField HeaderText="Faulty" DataField="Faulty" />
<asp:TemplateField HeaderText="View">
<ItemTemplate>

<asp:Button ID="View" runat="server" CommandName="Edit" CommandArgument='<%# Eval("ValueID") %>' Text="View" onclick="View_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" />

</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#0066FF" Font-Bold="True" ForeColor="White"
BorderColor="Black" BorderStyle="Groove" HorizontalAlign="Center"
VerticalAlign="Middle" />
<AlternatingRowStyle BackColor="#DCDCDC" />
</asp:GridView>
Thanx for the help!

Author

Commented:
<asp:GridView ID="GridView2" runat="server" BackColor="White"
        BorderColor="Black" BorderStyle="Groove" BorderWidth="1px" CellPadding="3"
        GridLines="Vertical"
       
        style="z-index: 1; left: 10px; top: 307px; position: absolute; height: 133px; width: 324px"
        AutoGenerateColumns="False" >
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <Columns>
            <asp:BoundField HeaderText="Province" DataField="Province" />
            <asp:BoundField HeaderText="Test OK" DataField="TestOk" />
            <asp:BoundField HeaderText="Faulty" DataField="Faulty" />
            <asp:TemplateField HeaderText="View">
                <ItemTemplate>
                <asp:Button ID="View" runat="server" CommandName="Edit" CommandArgument='<%# Eval("ValueID") %>' Text="View" onclick="View_Click" />
                    <asp:Button ID="Button2" runat="server" Text="Button" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#0066FF" Font-Bold="True" ForeColor="White"
            BorderColor="Black" BorderStyle="Groove" HorizontalAlign="Center"
            VerticalAlign="Middle" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>

Author

Commented:
arhame:
Can you do this in C# please.
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As    System.Web.UI.WebControls.GridViewCommandEventArgs ) Handles GridView1.RowCommand
If e.CommandName = "Edit" Then
Response.Write(e.CommandArgument.ToString())
End If
End Sub

 
 

Commented:
Sorry for the slow response - I was out of town at a clients office all day yesterday and not behind my computer.  Here that is converted to C#

protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit") {
        Response.Write(e.CommandArgument.ToString());
    }
}

Also from the linkbutton remove the onclick="View_Click" - unless you've added something more to it since.

Commented:
Keep in mind that all Response.write(e.CommandArguement.ToString()); is going to do is display the value on the page.  If you want to redirect to another page then you'd need to do something like this:

Response.Redirect("www.yourwebsite.com/yourpage.aspx?ID=" & e.CommandArguement.ToString)

That will put the value into a querystring that you can grab on the next page.

Author

Commented:
This is not going well .... Now I get the... below error   Im tryin this on gridview2
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.  

HTML:
 
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>FIFA</title>
    </head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView3" runat="server" BackColor="White" 
        BorderColor="Black" BorderStyle="Groove" BorderWidth="1px" CellPadding="3" 
        GridLines="Vertical" 
        style="z-index: 1; left: 361px; top: 308px; position: absolute; height: 133px; width: 322px" 
        AutoGenerateColumns="False">
         <Columns>
            <asp:BoundField HeaderText="Province" DataField="Province" />
            <asp:BoundField HeaderText="Test OK" DataField="TestOk" />
            <asp:BoundField HeaderText="Faulty" DataField="Faulty" />
            <asp:CommandField ShowSelectButton="True" />
        </Columns>
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#CCFF66" Font-Bold="True" ForeColor="Black" 
             BorderColor="Black" BorderStyle="Groove" HorizontalAlign="Center" 
             VerticalAlign="Middle" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>
    <asp:GridView ID="GridView1" runat="server" BackColor="White" 
        BorderColor="Black" BorderStyle="Groove" BorderWidth="1px" CellPadding="3" 
        GridLines="Vertical" 
        style="z-index: 1; left: 713px; top: 309px; position: absolute; height: 133px; width: 322px" 
        AutoGenerateColumns="False">
         <Columns>
            <asp:BoundField HeaderText="Province" DataField="Province" />
            <asp:BoundField HeaderText="Test OK" DataField="TestOk" />
            <asp:BoundField HeaderText="Faulty" DataField="Faulty" />
            <asp:CommandField ShowSelectButton="True" />
        </Columns>
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#9933FF" Font-Bold="True" ForeColor="White" 
             BorderColor="Black" BorderStyle="Groove" HorizontalAlign="Center" 
             VerticalAlign="Middle" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>
    <p>
    <asp:GridView ID="GridView2" runat="server" BackColor="White" 
        BorderColor="Black" BorderStyle="Groove" BorderWidth="1px" CellPadding="3" 
        GridLines="Vertical" 
        style="z-index: 1; left: 10px; top: 307px; position: absolute; height: 133px; width: 324px" 
        AutoGenerateColumns="False" >
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <Columns>
            <asp:BoundField HeaderText="Province" DataField="Province" />
            <asp:BoundField HeaderText="Test OK" DataField="TestOk" />
            <asp:BoundField HeaderText="Faulty" DataField="Faulty" />
            <asp:TemplateField HeaderText="View">
                <ItemTemplate>
                <asp:Button ID="View" runat="server" CommandName="Edit" CommandArgument='<%# Eval("Province") %>' Text="View"   />
 
               </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#0066FF" Font-Bold="True" ForeColor="White" 
            BorderColor="Black" BorderStyle="Groove" HorizontalAlign="Center" 
            VerticalAlign="Middle" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>
    </p>
    <p>&nbsp;</p>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
        style="z-index: 1; left: 139px; top: 665px; position: absolute" Text="Button" />
    <p>&nbsp;</p>
    <asp:Image ID="Image1" runat="server" ImageUrl="~/FIFA-HIRAC.png" style="z-index: 1; left: 69px; top: 27px; position: absolute; height: 280px; width: 915px;" />
    </form>
</body>
</html>
 
C#:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FillDG(GridView2,1);
        FillDG(GridView1,2);
        FillDG(GridView3,3);
    }
    private void FillDG(GridView MyGrid, int Group)
    {
        DataTable dt = new DataTable("Fault Info");
        dt.Columns.Add("Province", System.Type.GetType("System.String"));
        dt.Columns.Add("TestOk", System.Type.GetType("System.String"));
        dt.Columns.Add("Faulty", System.Type.GetType("System.String"));
        object[] x = new object[3];
        if (Group == 1)
        {
            x[0] = "Eastern Cape";
            x[1] = "10";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "Free State";
            x[1] = "8";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Gauteng";
            x[1] = "15";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "KwaZulu-Natal";
            x[1] = "12";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Mpumalanga";
            x[1] = "8";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Limpopo Province";
            x[1] = "8";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Northern Cape";
            x[1] = "7";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "North-West Province";
            x[1] = "6";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Western Cape";
            x[1] = "16";
            x[2] = "2";
            dt.Rows.Add(x);
        }
        else if (Group == 2)
        {
            x[0] = "Eastern Cape";
            x[1] = "15";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Free State";
            x[1] = "10";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "Gauteng";
            x[1] = "20";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "KwaZulu-Natal";
            x[1] = "18";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Mpumalanga";
            x[1] = "12";
            x[2] = "3";
            dt.Rows.Add(x);
 
            x[0] = "Limpopo Province";
            x[1] = "10";
            x[2] = "4";
            dt.Rows.Add(x);
 
            x[0] = "Northern Cape";
            x[1] = "11";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "North-West Province";
            x[1] = "9";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Western Cape";
            x[1] = "22";
            x[2] = "0";
            dt.Rows.Add(x);
        }
        else
        {
            x[0] = "Eastern Cape";
            x[1] = "10";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Free State";
            x[1] = "8";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Gauteng";
            x[1] = "15";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "KwaZulu-Natal";
            x[1] = "12";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "Mpumalanga";
            x[1] = "8";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Limpopo Province";
            x[1] = "8";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Northern Cape";
            x[1] = "7";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "North-West Province";
            x[1] = "6";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Western Cape";
            x[1] = "16";
            x[2] = "5";
            dt.Rows.Add(x);
        }
        MyGrid.DataSource = dt;
        MyGrid.DataBind();
        
        foreach (GridViewRow row in MyGrid.Rows)
        {
           
            Int32 intValue = Convert.ToInt32(row.Cells[2].Text);
            if (intValue == 1)
            {
                row.Cells[2].BackColor = System.Drawing.Color.LightCoral;
            }
            else if (intValue == 2)
            {
                row.Cells[2].BackColor = System.Drawing.Color.Coral;
            }
            else if (intValue == 5)
            {
                row.Cells[2].BackColor = System.Drawing.Color.Red;
            }
            else if (intValue >= 4)
            {
                row.Cells[2].BackColor = System.Drawing.Color.DarkRed;
            }
        }
    }
    protected void GridView2_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            Response.Write(e.CommandArgument.ToString());
        }
    }
 
    
}

Open in new window

Author

Commented:
I gor the previose error fixed,
now I get  The GridView 'GridView2' fired event RowEditing which wasn't handled.

Author

Commented:
The previous error is now gone as well, but I still can't catch the click, I got a break on the C# event.

Commented:
Try changing the CommandName="Edit" to CommandName="View".  Please post your current code again after you got rid of the last 2 errors.

Also we'll need to change the other code to:
protected void GridView2_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName == "View")
        {
            Response.Write(e.CommandArgument.ToString());
        }
    }

Author

Commented:
No, luck, I got a break point on the protected void GridView2_RowCommand , Code does not stop at the breakpoint
 

Author

Commented:
Any help please?

Author

Commented:
Please I need help on this.

Commented:
Weird, for some reason it started putting the responses from you in my E-mail spam filter.

vemmenpc, one thing I want you to try is only populate the gridviews if the page isnot postback.  I have seen that cause quite a few issues when you databind each postback and then try to call events on what was posted.

If that doesn't fix the problem please update me with current code and what else you've tried.
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (Page.IsPostback) { 
    } 
    else { 
        //Do Nothing, don't re-fill the datagrids 
        //Populate the data 
        FillDG(GridView2, 1); 
        FillDG(GridView1, 2); 
        FillDG(GridView3, 3); 
    } 
}

Open in new window

Commented:
Have you tried that vemmenpc?  Did you disappear like I did for a while?  Lol.

Author

Commented:
I will try it today, and provide feedback within the next 5 hours. Was out of the office, went to a Cape Town office to load software.
 
 

Author

Commented:
The only way I could catch the click event is with
protected void View_Click(object sender, EventArgs e)
{
Response.Write("666");
}
But How do I get the row information, this is with the IsPostback event added as per your previos sugestion. Will it be posabel to mail the project to you. vemmenpc @ telkom . co .za  I will zip the project.
 

HTML CODE
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>FIFA</title>
    </head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView3" runat="server" BackColor="White" 
        BorderColor="Black" BorderStyle="Groove" BorderWidth="1px" CellPadding="3" 
        GridLines="Vertical" 
        style="z-index: 1; left: 361px; top: 308px; position: absolute; height: 133px; width: 322px" 
        AutoGenerateColumns="False">
         <Columns>
            <asp:BoundField HeaderText="Province" DataField="Province" />
            <asp:BoundField HeaderText="Test OK" DataField="TestOk" />
            <asp:BoundField HeaderText="Faulty" DataField="Faulty" />
            <asp:CommandField ShowSelectButton="True" />
        </Columns>
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#CCFF66" Font-Bold="True" ForeColor="Black" 
             BorderColor="Black" BorderStyle="Groove" HorizontalAlign="Center" 
             VerticalAlign="Middle" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>
    <asp:GridView ID="GridView1" runat="server" BackColor="White" 
        BorderColor="Black" BorderStyle="Groove" BorderWidth="1px" CellPadding="3" 
        GridLines="Vertical" 
        style="z-index: 1; left: 713px; top: 309px; position: absolute; height: 133px; width: 322px" 
        AutoGenerateColumns="False">
         <Columns>
            <asp:BoundField HeaderText="Province" DataField="Province" />
            <asp:BoundField HeaderText="Test OK" DataField="TestOk" />
            <asp:BoundField HeaderText="Faulty" DataField="Faulty" />
            <asp:CommandField ShowSelectButton="True" />
        </Columns>
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#9933FF" Font-Bold="True" ForeColor="White" 
             BorderColor="Black" BorderStyle="Groove" HorizontalAlign="Center" 
             VerticalAlign="Middle" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <asp:Image ID="Image1" runat="server" ImageUrl="~/FIFA-HIRAC.png" style="z-index: 1; left: 69px; top: 27px; position: absolute; height: 280px; width: 915px;" />
    <asp:GridView ID="GridView2" runat="server" BackColor="White" 
        BorderColor="Black" BorderStyle="Groove" BorderWidth="1px" CellPadding="3" 
        GridLines="Vertical" 
        style="z-index: 1; left: 10px; top: 307px; position: absolute; height: 133px; width: 324px" 
        AutoGenerateColumns="False" 
        onselectedindexchanged="GridView2_SelectedIndexChanged" >
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <Columns>
            <asp:BoundField HeaderText="Province" DataField="Province" />
            <asp:BoundField HeaderText="Test OK" DataField="TestOk" />
            <asp:BoundField HeaderText="Faulty" DataField="Faulty" />
            <asp:TemplateField HeaderText="View">
                <ItemTemplate>
                <asp:Button ID="View" runat="server" CommandArgument='<%# Eval("Province") %>' 
                        Text="View"   PostBackUrl="~/Default.aspx" onclick="View_Click"   />
               </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#0066FF" Font-Bold="True" ForeColor="White" 
            BorderColor="Black" BorderStyle="Groove" HorizontalAlign="Center" 
            VerticalAlign="Middle" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>
    </form>
</body>
</html>
 
C# code
 
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
 
public partial class _Default : System.Web.UI.Page 
{
    DataTable dt = new DataTable("Fault Info");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            FillDG(GridView2, 1);
           // FillDG(GridView1,2);
           // FillDG(GridView3,3);
        }
    }
    private void FillDG(GridView MyGrid, int Group)
    {
        
        dt.Columns.Add("Province", System.Type.GetType("System.String"));
        dt.Columns.Add("TestOk", System.Type.GetType("System.String"));
        dt.Columns.Add("Faulty", System.Type.GetType("System.String"));
        object[] x = new object[3];
        if (Group == 1)
        {
            x[0] = "Eastern Cape";
            x[1] = "10";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "Free State";
            x[1] = "8";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Gauteng";
            x[1] = "15";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "KwaZulu-Natal";
            x[1] = "12";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Mpumalanga";
            x[1] = "8";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Limpopo Province";
            x[1] = "8";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Northern Cape";
            x[1] = "7";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "North-West Province";
            x[1] = "6";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Western Cape";
            x[1] = "16";
            x[2] = "2";
            dt.Rows.Add(x);
        }
        else if (Group == 2)
        {
            x[0] = "Eastern Cape";
            x[1] = "15";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Free State";
            x[1] = "10";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "Gauteng";
            x[1] = "20";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "KwaZulu-Natal";
            x[1] = "18";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Mpumalanga";
            x[1] = "12";
            x[2] = "3";
            dt.Rows.Add(x);
 
            x[0] = "Limpopo Province";
            x[1] = "10";
            x[2] = "4";
            dt.Rows.Add(x);
 
            x[0] = "Northern Cape";
            x[1] = "11";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "North-West Province";
            x[1] = "9";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Western Cape";
            x[1] = "22";
            x[2] = "0";
            dt.Rows.Add(x);
        }
        else
        {
            x[0] = "Eastern Cape";
            x[1] = "10";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Free State";
            x[1] = "8";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Gauteng";
            x[1] = "15";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "KwaZulu-Natal";
            x[1] = "12";
            x[2] = "2";
            dt.Rows.Add(x);
 
            x[0] = "Mpumalanga";
            x[1] = "8";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Limpopo Province";
            x[1] = "8";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "Northern Cape";
            x[1] = "7";
            x[2] = "0";
            dt.Rows.Add(x);
 
            x[0] = "North-West Province";
            x[1] = "6";
            x[2] = "1";
            dt.Rows.Add(x);
 
            x[0] = "Western Cape";
            x[1] = "16";
            x[2] = "5";
            dt.Rows.Add(x);
        }
        MyGrid.DataSource = dt;
        MyGrid.DataBind();
        foreach (GridViewRow row in MyGrid.Rows)
        {
            Int32 intValue = Convert.ToInt32(row.Cells[2].Text);
            if (intValue == 1)
            {
                row.Cells[2].BackColor = System.Drawing.Color.LightCoral;
            }
            else if (intValue == 2)
            {
                row.Cells[2].BackColor = System.Drawing.Color.Coral;
            }
            else if (intValue == 5)
            {
                row.Cells[2].BackColor = System.Drawing.Color.Red;
            }
            else if (intValue >= 4)
            {
                row.Cells[2].BackColor = System.Drawing.Color.DarkRed;
            }
        }
    }
    private void view()
    {
        Response.Write("666");
    }
    protected void GridView2_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName == "View")
        {
            Response.Write(e.CommandArgument.ToString());
        }
    }
    void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
       // binddata();
    }
    void GridView2_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Display the company name in italics.
            e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
        }
    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
                // Response.Write("666");
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
       // Response.Write("666");
    }
    protected void View_Click(object sender, EventArgs e)
    {
        Response.Write("666");
    }
}

Open in new window

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hey, thanx for this response ant patience with me on this one, Im not sure if I struck a Blond moment with this one, but yes&
Phillip
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.