C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Trigger a child control's event which is inside a datagrid


Posted Date: 07 Apr 2005    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Parvathi GandhiMember Level: Silver    
Rating: 1 out of 5Points: 10



In some situations we need to perform some operations on clicking the checkbox inside a datagrid.
You need to perform the following steps.

Populate the datagrid with data from database.
Add one template column in the HTML source and add a checkbox in that.I have given below the HTML source for this.

Let me explain the scenario in which we can apply this.
Consider that we have a datagrid which is having the list of employees.
In each row of the datagrid we have one check box and while clicking the check box we need to perform some database operations.
Here 'DoOperation' is a user defined procedure which will contain the code to perform the required operation.
Call this function inside the itemdatabound event as given below.


Private Sub dgdTest_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdTest.ItemDataBound
Try
Dim ChkTemp as CheckBox
chkTemp = DirectCast(e.Item.FindControl("chkTest"), CheckBox)
If Not chkTemp Is Nothing Then
AddHandler chkTest.CheckedChanged, AddressOf DoOperation
End If

Catch ex As Exception
'handle exceptions here
End Try
End Sub


In the following procedure you have to write the required functionality.


Public Sub DoOperation(ByVal sender As Object, ByVal e As System.EventArgs)

Try
Dim intCounter As Integer

For intCounter = 0 To dgdTest.Items.Count - 1

Dim chkTest As CheckBox
chkTest = CType(dgdTest.Items(i).FindControl("chkTest"), CheckBox)

If Not chkTest Is CType(sender, CheckBox) Then

Else

If CType(sender, CheckBox).Checked = True Then

'write code

End If

End If
Next
Catch ex As Exception
'handle exceptions here
End Try

End Sub



In the HTML source we have to give the procedure name as given below.Don't forget to set the autopostback property to true.


<asp:datagrid id="dgdTest" runat="server">
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle HorizontalAlign="Center" Width="18%" CssClass="gridHeader"></HeaderStyle>
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name")%>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Department">
<HeaderStyle HorizontalAlign="Center" Width="18%" CssClass="gridHeader"></HeaderStyle>
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
<ItemTemplate>
<asp:Label ID="lblDept" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Department")%>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:datagrid id="dgdTest" Runat="server" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Check">
<ItemTemplate>
<asp:CheckBox id="chkTest" AutoPostBack="True" OnCheckedChanged="SourceCheck" Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp;datagrid>



Happy Coding...




Responses

Author: Siddesh Kapadi    13 Apr 2005Member Level: Bronze   Points : 0
I didnot find this article useful as this doesnot give the detailed describtion and the proper execution of the code


Author: Parvathi Gandhi    18 Apr 2005Member Level: Silver   Points : 0
The article contains only the required code not the entire code of the page.
If u have a requirement like this u can use this code.
Now i have added some more description.
Have a look.

-Parvathi.K


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Dynamically change the DataGrid/DataList background and Alternating Color
Previous Resource: A generic loading of Data in a DropdownList using DataAccessLayer & caching mechanism
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use