Quantcast
Channel: ASP.NET 101 » Code Samples
Viewing all articles
Browse latest Browse all 10

Conditional Gridview Text – Checkboxes

$
0
0

This code sample shows how to either show or make invisible, a checkbox in each row of the Gridview, along with making text conditional, based on certain criteria. In this case, if the Postal code starts with a non-numeric character, we change it to “Alt Text”, and we set the Visible property of the checkbox in that row to “False”

<%@ Import Namespace=”System.Drawing” %>
<script language=”VB” Runat=”server”>
Sub doDataCheck(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim chkExp as CheckBox
If e.Row.RowType = DataControlRowType.DataRow Then
Dim sCode as String=e.Row.Cells(7).text
If Not isNumeric(sCode.Substring(1,1)) Then
e.Row.Cells(7).text=”<i style=’color:red’>(Alt Text)</i>”
chkExp= CType(e.row.FindControl(“ck1″), Checkbox)
chkExp.Visible=”False”
End If
End If
End Sub
</script>
<html>
<head runat=”server”>
<meta name=”GENERATOR” Content=”ASP Express 4.5″>
<title>Conditional GridView Cell Coloring</title>
</head>
<body>
<form id=”form1″ Runat=”server”>
<asp:GridView OnRowDataBound=”doDataCheck” AutoGenerateColumns=”False”
DataSourceID=”SqlDataSource1″ ID=”GridView2″ runat=”Server”>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID=”ck1″ runat=”server”></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=”FirstName” HeaderText=”First” />
<asp:BoundField DataField=”LastName” HeaderText=”Last” />
<asp:BoundField DataField=”Title” HeaderText=”Title” />
<asp:BoundField DataField=”Address” HeaderText=”Address” />
<asp:BoundField DataField=”City” HeaderText=”City” />
<asp:BoundField DataField=”Region” HeaderText=”Region” />
<asp:BoundField DataField=”PostalCode” HeaderText=”PostalCode” />
<asp:BoundField DataField=”country” HeaderText=”country” />
</Columns>
<HeaderStyle BackColor=”Blue” Font-Bold=”True” ForeColor=”White” />
</asp:GridView>
<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:NorthwindConnectionString %>”
SelectCommand=”Select FirstName, LastName, Title, Address, City, Region, PostalCode, country from Employees”>
</asp:SqlDataSource>
</form>
</body>
</html>


Viewing all articles
Browse latest Browse all 10

Trending Articles