Changing font on Pushbutton control

B

Blasting Cap

I am having trouble changing the font for a PushButton control in a
datagrid button column. I have seen several posts refer to styles and
simple changes to the HTML for font changes but most of those referred
to a LinkButton type of control. But in cases where someone is using the
PushButton there are no posted solutions. Whenever these changes are
made to the PushButton control they do not make any requested changes.

I have seen one board that posted the following code in C# that stated
it would fix the problem.

if(e.Item.ItemType.Item ||
e.Item.ItemType.AlternatingItem)
{
Button btn = (Button)e.Item.Cells[X].Controls[0];
btn.Font.Name = "verdana";
FontUnit.Point(8);
}

I am not sure if this type of code would go before the databind call or
after or what...

Any suggestions on how to do this with VB.Net code?

Any help or advice would be greatly appreciated.

BC
 
K

Ken Cox [Microsoft MVP]

Hi Blasting (using a real name is considered more polite)

Here's some code that should get you going. You need to get a reference to
the button and then change the button's characteristics much the way you saw
in the C# code.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

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

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not IsPostBack Then
dg1.DataSource = CreateDataSource()
dg1.DataBind()
End If
End Sub

Protected Sub dg1_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
Dim btn As Button
If e.Item.ItemType <> ListItemType.Header Or e.Item.ItemType <>
ListItemType.Footer Then
btn = e.Item.FindControl("pushbutton1")
If Not IsNothing(btn) Then
btn.Font.Italic = True
btn.Font.Name = "verdana"
btn.Font.Size = FontUnit.Point(8)
End If
End If
End Sub

Function CreateDataSource() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New Data.DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New Data.DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New Data.DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function


</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:datagrid id="dg1" runat="server" onitemcreated="dg1_ItemCreated">
<columns>
<asp:templatecolumn headertext="StringValue">
<itemtemplate>
<asp:button id="pushbutton1" runat="server"
causesvalidation="false" commandname="" text='<%# DataBinder.Eval(Container,
"DataItem.StringValue") %>' />
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</div>
</form>
</body>
</html>


Blasting Cap said:
I am having trouble changing the font for a PushButton control in a
datagrid button column. I have seen several posts refer to styles and
simple changes to the HTML for font changes but most of those referred to a
LinkButton type of control. But in cases where someone is using the
PushButton there are no posted solutions. Whenever these changes are made
to the PushButton control they do not make any requested changes.

I have seen one board that posted the following code in C# that stated it
would fix the problem.

if(e.Item.ItemType.Item ||
e.Item.ItemType.AlternatingItem)
{
Button btn = (Button)e.Item.Cells[X].Controls[0];
btn.Font.Name = "verdana";
FontUnit.Point(8);
}

I am not sure if this type of code would go before the databind call or
after or what...

Any suggestions on how to do this with VB.Net code?

Any help or advice would be greatly appreciated.

BC
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top