DATAGRID DROPDOWN PROBLEM

S

Savas Ates

It is in my <EditItemTemplate> Tag..

<asp:DropDownList Runat=server ID="CIdeefixeSatis" SelectedIndex='<%#
SelectMyIndex(DataBinder.Eval(Container, "DataItem.ideefixesatis")) %>'


<asp:ListItem Value="True">Stokta Var</asp:ListItem>


<asp:ListItem Value="False">Stokta Yok</asp:ListItem>


</asp:DropDownList>

In my codebehind

Public Function SelectMyIndex(ByVal indexno As Boolean)

ideefixesatisc = CType(Page.FindControl("CIdeefixeSatis"),
DropDownList)
Response.Write(ideefixesatisc)


End Function
There is no problem with that code..
However When i try to reach
ideefixesatisc. (properties of ideefixesatisc object for example
ideefixesatisc.DataTextField = "OK" )

It returns
Object reference not set to an instance of an object.
ideefixesatisc.DataTextField = "OK"

What can be the problem. . Any idea will be appreciated..
 
G

Guest

FindControl only finds controls within the scope of the control that the
method is run from. i.e. Page.FindControl will only find control that exist
within the scope of the page. You should use the FindControl method of the
parent control to the control that you are looking for. You say your
DropDownList is in an EditItemTemplate tage so you should use the findControl
method of whatever data bound control your EditItemTemplate is in.

For Example if your DropDownList was within the EditItemTemplate of a
DetailsView called MyDetailsView you would use:

MyDetailsView.FindControl("CIdeefixeSatis")

The only exception to this is a gridview or similar control where there
could be multiple instances of your control, in this case you would have to
make sure you called the FindControl method on the correct row within your
GridView.

For FindControl to work you need to amke sure that each child control has a
unique ID, e.g. whilst ASP.net will let you put a control with the same ID in
your EditItemTemplate and Your InsertItemTemplate if you called FindControl
method on the parent control it would give you an error that there were
multiple controls with the same ID and it cannot determine which one you are
looking for.
 
S

Savas Ates

I have that hierarchie

<asp:DataGrid id="DataGrid1"
<Columns>
<EditItemTemplate>
<asp:DropDownList Runat=server ID="CIdeefixeSatis"

and CIdeefixeSatis is the only one dropdown named as "CIdeefixeSatis"..

I tried
ideefixesatisc = CType(DataGrid1.FindControl("CIdeefixeSatis"),
DropDownList)

ideefixesatisc.DataTextField = "Stokta Yok"



it is still the same result.. What do you Offer.. By the way thaks for your
quick response..
 
G

Guest

The reason DataGrid1.FindControl doesn't work is because there might be
multiple controls because a DataGrid can have lots of rows.

Not sure how you would do it in a DataGrid. I started .net with V2 so i
always use GridViews and i have never used a DataGrid. With a GridView you
need to call the FindControl method of the relevent row in the Rows
collection or call it in something like the OnRowDataBound or OnRowCreated
event handler. A Gridview doesn't havethe rows collection or any suitable
event handler so i aqm not sure might have to wait for someone with a bit
more experience than me for help on this one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top