473,382 Members | 1,710 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,382 software developers and data experts.

ItemCreated FindControl problem c#

All I need to do is set the border property of an image control to border=0.
I want to do this at runtime for the first image on the page in a datalist.
In the ItemCreated event I'm trying to use FindControl to set a reference to
the img control so I can change the attribute. But when I run the code below
I get the error: "Object reference not set to an instance of an object." I
assume its not finding the control and returning null? Anyway, you can see
what I'm trying to do, can anyone tell me how to get it to work? (code below)

******* code *******
public void main_ItemCreated(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
HtmlImage imgPatron = new HtmlImage();
imgPatron = (HtmlImage)e.Item.FindControl(Request.Form["patron"]);
if(imgPatron!=null)
{
image_count();
if(i_count == 1)
{
imgPatron.Border = 0;
}
}

}

************

Thanks for any help!
Nov 19 '05 #1
2 3165
Wrong event. I recommend using PreRender event since the page is fully built
at that stage. You can use ItemDataBound if you wish, but PreRender is
easier.

Eliyahu

"tparks69" <tp******@discussions.microsoft.com> wrote in message
news:B6**********************************@microsof t.com...
All I need to do is set the border property of an image control to border=0. I want to do this at runtime for the first image on the page in a datalist. In the ItemCreated event I'm trying to use FindControl to set a reference to the img control so I can change the attribute. But when I run the code below I get the error: "Object reference not set to an instance of an object." I assume its not finding the control and returning null? Anyway, you can see what I'm trying to do, can anyone tell me how to get it to work? (code below)
******* code *******
public void main_ItemCreated(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
HtmlImage imgPatron = new HtmlImage();
imgPatron = (HtmlImage)e.Item.FindControl(Request.Form["patron"]);
if(imgPatron!=null)
{
image_count();
if(i_count == 1)
{
imgPatron.Border = 0;
}
}

}

************

Thanks for any help!

Nov 19 '05 #2
What is exactly the "Request.Form["patron"]"?

I think you should write:
.....FindControl(ID/name of the server side control);
Hope this help

"tparks69" wrote:
All I need to do is set the border property of an image control to border=0.
I want to do this at runtime for the first image on the page in a datalist.
In the ItemCreated event I'm trying to use FindControl to set a reference to
the img control so I can change the attribute. But when I run the code below
I get the error: "Object reference not set to an instance of an object." I
assume its not finding the control and returning null? Anyway, you can see
what I'm trying to do, can anyone tell me how to get it to work? (code below)

******* code *******
public void main_ItemCreated(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
HtmlImage imgPatron = new HtmlImage();
imgPatron = (HtmlImage)e.Item.FindControl(Request.Form["patron"]);
if(imgPatron!=null)
{
image_count();
if(i_count == 1)
{
imgPatron.Border = 0;
}
}

}

************

Thanks for any help!

Nov 19 '05 #3

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

Similar topics

1
by: Kevin Hector | last post by:
Hi. Can anyone explain why, when setting the Text of a Label control in the ItemCreated event of a Repeater (example below), the Text value is NOT persisted to ViewState? I know this can be fixed by...
2
by: Lars Netzel | last post by:
Hey! I have a ButtonColumn (PushButton) which I need to address in the DataGrid's ItemCreated to be able to set the Text on the ButtonColumn (yes I need to change this in runtime). regards...
4
by: Souri Challa | last post by:
Hello All, When a datagrid in a web form is getting re populated from view state on post back, it is firing the datagrid Item Created event but the DataGridItem(e.Item.DataItem is null) in the...
9
by: Manuel | last post by:
I have a Datagrid with 9 elements. The problem is that when I sort the grid by a column, it present more columns than there are. This is my code for the ItemCreated event: Private pNo As...
1
by: tshad | last post by:
I am trying understand when DataLists ItemCreated is called. I put function that attaches a popup to my delete button: Sub DataList_ItemCreated(Sender As Object, e As DataListItemEventArgs)...
0
by: tparks69 | last post by:
All I want to do is change the border of the first img control in my html to 0. The other images I want to have border=1. I have an event on my datalist that calls the following code: ...
4
by: Dennis E. Jones, Jr. | last post by:
I'm creating a dynamic control for each row in a REPEATER based on database values. ItemDataBound creates the control for the initial load (not postback), but I cannot get the control recreated...
0
by: fig000 | last post by:
Hi, I have a datalist with bound data from a database. If I wanted to access one of the fields I would normally use the itemdatabound event to access that field on each row. However in this...
0
by: K B | last post by:
Hi, I have a DetailsView and on the ItemCreated event I can hide a control based on meeting criteria, but I can not figure out how to hide the row. Example, If ddlType.selectedvalue = 1 Then...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.