PC Review


Reply
Thread Tools Rate Thread

How to access Datagrid Linkbutton with OnItemDataBound?

 
 
Rodusa
Guest
Posts: n/a
 
      29th Dec 2004
I keep getting this error when trying to modify the text property of a
linkbutton inside a datagrid template column.

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:




Line 234: b.Text="Replace with this item"; // ERROR HAPPENS HERE

Line 235:

Line 236: for (int i=0; i < (e.Item.Cells.Count)-1; i++)







// CODE

public void CheckRow(object sender, DataGridItemEventArgs e)

{

string keyword = Request.QueryString["keyword"];

if (keyword!=null && keyword!=null) // Replace item event happens
(uses button Replace this item)

{

// changes button label

string test= e.Item.Cells.Count.ToString();

//gets first template column. It needs to be accessed differently
from databound items

LinkButton b = (LinkButton)e.Item.FindControl("btAddReplace");

b.Text="Replace with this item";



for (int i=0; i < (e.Item.Cells.Count)-1; i++)

{

Response.Write(i.ToString()+e.Item.Cells[i].Text+"<br>");

}

}

}






I also tried these two other alternatives and no success:


LinkButton b = (LinkButton)e.Item.Cells[0].Controls[0];

DataBoundLiteralControl b = b.Controls[0] as
DataBoundLiteralControl;




The error only happens when I try to access the control property
"b.Text". The instance of it is recognized by the framework.
thank you

Rod

 
Reply With Quote
 
 
 
 
Rodusa
Guest
Posts: n/a
 
      29th Dec 2004
I've just figured out. I forgot to skip the Datagrid headers

if (e.Item.ItemType == ListItemType.SelectedItem || e.Item.ItemType
== ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
code here
}

Rod

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ht get column in DataRowItem of datagrid at OnItemDataBound xzzy Microsoft ASP .NET 3 18th Jun 2006 07:03 PM
LinkButton in a datagrid??? =?Utf-8?B?VGltOjouLg==?= Microsoft ASP .NET 4 26th Jan 2005 08:17 AM
LinkButton without DataGrid Eamon Microsoft ASP .NET 1 28th Apr 2004 04:23 PM
get the last row OnItemDataBound in Datagrid vincent Microsoft C# .NET 0 11th Dec 2003 04:10 AM
DataGrid OnItemDataBound problem in APS.NET C# Steven Microsoft ASP .NET 1 9th Jul 2003 06:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:19 PM.