Solved

How to bind to edit controls in ItemDataBound for DataGrid?

Posted on 2009-07-13
5
501 Views
Last Modified: 2013-11-08
I have a DataGrid that has a column with an EditItemTemplate coded below. I want to use the OnItemDataBound event to make changes to the "edtMsgEdit" control which is in EditItemTemplate. But I keep getting null when I try to reference it in the ItemDataBound.

Is there a way to bind to the edit controls from the ItemDataBound event in the code-behind?
<asp:TemplateColumn HeaderText="Content">

	            <ItemTemplate>

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

	            </ItemTemplate>

	            <EditItemTemplate>

                    <aka:CustomEditor ID="edtMsgEdit" runat="server">

                    </aka:CustomEditor>

	            </EditItemTemplate>            

            </asp:TemplateColumn>

Open in new window

0
Comment
Question by:bemara57
  • 2
  • 2
5 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 24844575
Just wondering what kind of control is that?
Is that DataList control you are talking about in .net 2+
Or
DataGrid Control in .net earlier than 2.0?
0
 
LVL 14

Accepted Solution

by:
Dustin Hopkins earned 500 total points
ID: 24846275
hmm, are you using an if statement in you itemdatabound, so it will only try to bind the item being in edit mode?
For example see below.

Hope this helps,
Dustin
 if (e.Item.ItemType == ListItemType.EditItem) {

        TextBox x = e.Item.FindControl("edtMsgEdit");

    }

Open in new window

0
 

Author Comment

by:bemara57
ID: 24849286
quru sami, it is actually a custom control I created. Unfortunately, there is a property I cannot bind to from the aspx markup, so I have to do it in the code-behind.

dusion, I think this is exactly what I need, but how do I reference the row data so I can use the binded property "Content" for example? In my code example below, how do put Bind("Content") in edtMsgEdit.Content?


        protected void dgMessages_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

        {

            DataGridItem row = e.Item as DataGridItem;

             if (e.Item.ItemType == ListItemType.EditItem) 

             {

                //INITIALIZE VARIABLES

                CustomEditor edtMsgEdit = row.FindControl("edtMsgEdit") as CustomEditor;
 

                //COPY HTML INTO EDITOR FOR LATER EDITING

                edtMsgEdit.Content = ???;

            }

        }

Open in new window

0
 

Author Comment

by:bemara57
ID: 24849314
Oh quru sami, I misunderstood your question... it's a DataGrid from .NET 2.0.
0
 
LVL 14

Assisted Solution

by:Dustin Hopkins
Dustin Hopkins earned 500 total points
ID: 24851744
Well, you could use the Eval method of databinder, and reference a dataitem.

Hope this helps,
Dustin
CustomEditor edtMsgEdit = row.FindControl("edtMsgEdit") as CustomEditor;

 

                //COPY HTML INTO EDITOR FOR LATER EDITING

                edtMsgEdit.Content = DataBinder.Eval(e.Item.DataItem, "Content").ToString;

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now