473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to access Datagrid Linkbutton with OnItemDataBound?

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

Nov 16 '05 #1
1 3522
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

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: tshad | last post by:
How do I find (and set) a couple of labels in the Footer after a DataGrid is filled? I have a bunch of DataGrids that get displayed nested inside a DataList. The datagrid looks like: ...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
16
by: tshad | last post by:
I am trying to center a button in a datagrid column in asp.net and it doesn't seem to work. I have the following: <asp:TemplateColumn ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">...
1
by: Tim::.. | last post by:
Can someone please tell me how I turn an ItemTemplate into a link? I don't what to use a HyperlinkColumn as I have two lines in one column in the datagrid coming from different sources! I would...
6
by: tshad | last post by:
I have a datagrid where each column has both a label and a linkbutton. I need to have the label left justified and the linkbutton right justified. In HTML, I would have to set up 2 cells to do...
2
by: Frank | last post by:
Hello, I am developing with VS.Net 2003 on the asp 1.1 platform. I am a few days new to using a datagrid. I found a nice tutorial and had no problems using an editable datagrid with textboxes...
0
by: rockdale | last post by:
Hi, All: I dynamic add an arrow up/down image in OnItemDataBound event for my datagrid, it works fine. But I have another textbox which trigger postback and doing something, after this event get...
2
by: Hardy Wang | last post by:
Hi all, We have DataGrid control in Web Form, our client requires to be able to click anywhere of a row to fire the event same as LinkBotton column is clicked. We we did in ASP.NET 1.1 is in...
2
by: tshad | last post by:
I am having a problem with my Datagrid that displays my Files via GetFiles() If I use a HyperLinkColumn - I can use the column name "Name" which is what the result from GetFiles(). ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.