?
Solved

How to bind to edit controls in ItemDataBound for DataGrid?

Posted on 2009-07-13
5
Medium Priority
?
516 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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!
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

765 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