Programmatic access to TemplateColumn?

M

matt

hello,

i have a datagrid w/ a TemplateColumn in it, like so:

<asp:TemplateColumn HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelected" Runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>

....in my c# code-behind, how do i access the checkbox? i need to add a
javascript onClick attribute.

thanks!
matt
 
P

papaja

In general you access to web controls like this
e.Item.FindControl("MyCheckBoxID")
or (e.Item.Cells(n).Controls(0), where n is zero-based number of column
where this control appears.

If it is in second column, then n will be 2!
 
M

matt

well, that's correct if youre doing something on a grid event, say a
button click or whatever.

but perhaps i should clarify -- i want to access from Page_Load and add
the javascript Attribute to *all* the row's checkboxes. this way, each
row of grid has a server-side CheckBox w/ a javascript attribute tagged
on.

tho maybe... i should do this from the grid's ItemDataBound or
ItemCreated, and try to use a syntax like the one youve provided.


matt
 
P

papaja

check out this code, maybe you will find something useful.


<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<html>
<script runat="server">

Function CreateDataSource() As ICollection

' Create sample data for the DataList control.
Dim dt As DataTable = New DataTable()
Dim dr As DataRow

' Define the columns of the table.
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("CurrencyValue",
GetType(Double)))

' Populate the table with sample values.
Dim i As Integer

For i = 0 To 8

dr = dt.NewRow()

dr(0) = i
dr(1) = "Description for item " & i.ToString()
dr(2) = 1.23 * (i + 1)

dt.Rows.Add(dr)

Next i

Dim dv As DataView = New DataView(dt)
Return dv

End Function


Sub Page_Load(sender As Object, e As EventArgs)

' Load sample data only once, when the page is first loaded.
If Not IsPostBack Then

ItemsList.DataSource = CreateDataSource()
ItemsList.DataBind()

End If

End Sub

Sub Item_Bound(sender As Object, e As DataListItemEventArgs)

If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then


' Retrieve the Label control in the current DataListItem.
Dim PriceLabel As Label = _
CType(e.Item.FindControl("PriceLabel"), Label)

' Retrieve the text of the CurrencyColumn from the
DataListItem
' and convert the value to a Double.
Dim Price As Double = Convert.ToDouble( _
(CType(e.Item.DataItem,
DataRowView)).Row.ItemArray(2).ToString())

' Format the value as currency and redisplay it in the
DataList.
PriceLabel.Text = Price.ToString("c")

End If

End Sub

</script>

<body>

<form runat=server>

<h3>DataList ItemDataBound Example</h3>

<asp:DataList id="ItemsList"
BorderColor="black"
CellPadding="5"
CellSpacing="5"
RepeatDirection="Vertical"
RepeatLayout="Table"
RepeatColumns="3"
ShowBorder="True"
OnItemDataBound="Item_Bound"
runat="server">

<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>

<AlternatingItemStyle BackColor="Gainsboro">
</AlternatingItemStyle>

<HeaderTemplate>

List of items

</HeaderTemplate>

<ItemTemplate>

Description: <br>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>

<br>

Price:
<asp:Label id="PriceLabel"
runat="server"/>

</ItemTemplate>

</asp:DataList>

</form>

</body>
</html>
 
M

matt

thanks!

this is what i was looking for. like so:

public void Item_Bound(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
CheckBox chkSelected = (CheckBox)e.Item.FindControl("chkSelected");
chkSelected.Attributes.Add("onClick",
"setCheckboxClickCount(this.checked);");
}
}



matt
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top