Solved

How to bind to edit controls in ItemDataBound for DataGrid?

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS On fail action 5 37
abstract class C# 1 30
VS 2015 is giving [No relevant source lines] 2 24
need to convert function to c# 5 22
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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

790 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