Solved

How to bind to edit controls in ItemDataBound for DataGrid?

Posted on 2009-07-13
5
500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

13 Experts available now in Live!

Get 1:1 Help Now