Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 18th, 2007, 06:08 AM
Authorized User
 
Join Date: Mar 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Replace Gridview field if null with new field

I want to replace a gridview field depending if the value is null or blank. For example there are 4 fields, ID, Course, Mark 1 and Mark 2. The 3 headings are ID, Course and Mark. If mark1 is blank then it should automatically call mark2, however this ain't happening. This is the code I have used.

Code:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">


    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
        //GridView1_RowDataBound();
    }

    public void BindData()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connBMC"].ConnectionString);
        conn.Open();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = "Select * from iftest";
        SqlDataReader dr = cmd.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        conn.Close();

    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label myLabel1 = (Label)e.Row.FindControl("lblMark1");
            string text = myLabel1.Text;
            Label myLabel2 = (Label)e.Row.FindControl("lblMark2");

            if (text == null || text.Length < 1)
            {
                myLabel2.Visible = true;

            }
        }

    }







</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connBMC %>"
            SelectCommand="SELECT * FROM [iftest]"></asp:SqlDataSource>

    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                    SortExpression="ID" />
                <asp:BoundField DataField="Course" HeaderText="Course" SortExpression="Course" />
                <asp:TemplateField HeaderText="Mark">
<ItemTemplate>

<asp:Label ID="lblMark1" Text='<%# Eval("Mark1") %>' runat="server"></asp:Label>

<asp:Label ID="lblMark2" Text='<%# Eval("Mark2") %>' runat="server" Visible="False"></asp:Label>

</ItemTemplate>

</asp:TemplateField>

            </Columns>
        </asp:GridView>


        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:connBMC %>"
            SelectCommand="SELECT * FROM [iftest]"></asp:SqlDataSource>
    </form>
</body>
</html>
 
Old June 18th, 2007, 06:22 AM
Authorized User
 
Join Date: Mar 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorted, redeveloped code in case anyone else needs to sort this.

Code:
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">


    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }

    public void BindData()
    {


        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connBMC"].ConnectionString);
        conn.Open();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = "Select * from iftest";
        SqlDataReader dr = cmd.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        conn.Close();


    }

    public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {


        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label myLabel1 = (Label)e.Row.FindControl("lblMark1");
            Label myLabel2 = (Label)e.Row.FindControl("lblMark2");
            string text;
            text = myLabel1.Text;

            if (text == null || text.Length < 1)
            {
                myLabel1.Visible = false;
                myLabel2.Visible = true;

            }
            else
            {
                myLabel1.Visible = true;
                myLabel2.Visible = false;

            }

        }

    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connBMC %>"
            SelectCommand="SELECT * FROM [iftest]"></asp:SqlDataSource>

    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"> 
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                    SortExpression="ID" />
                <asp:BoundField DataField="Course" HeaderText="Course" SortExpression="Course" />
                <asp:TemplateField HeaderText="Mark">
                <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# 
Bind("Mark1") %>'></asp:TextBox>

                </EditItemTemplate>
<ItemTemplate>

<asp:Label ID="lblMark1" Text='<%# Eval("Mark1") %>' runat="server"></asp:Label>

<asp:Label ID="lblMark2" Text='<%# Eval("Mark2") %>' runat="server"></asp:Label>


</ItemTemplate>

</asp:TemplateField>

            </Columns>
        </asp:GridView>
        &nbsp;

        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:connBMC %>"
            SelectCommand="SELECT * FROM [iftest]"></asp:SqlDataSource>
    </form>
</body>
</html>







Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I handle NULL resultset field? cm.cruz Classic ASP Basics 1 January 28th, 2008 06:29 PM
Null inserted by form even when field has default Kia VB Databases Basics 1 July 24th, 2007 01:47 PM
Copy previous field record if next field is null ecampos Access VBA 6 June 23rd, 2006 12:55 PM
entering Null value for Date field jordancrandall Access ASP 1 January 12th, 2005 01:29 PM
Null Value for Date/Time Field ksegars4 Crystal Reports 2 November 17th, 2004 11:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.