Solved

How to bind to edit controls in ItemDataBound for DataGrid?

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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