csetzkorn
asked on
datalist, EditItemTemplate problem
Hi,
I have a datalist with a dropdownlist (projects)
<asp:datalist …
<ItemTemplate>
…
</ItemTemplate>
<EditItemTemplate>
…
<asp:DropDownList ID = "projects" Runat="server" DataTextField="project_tit le" DataValueField="project_ui d" AutoPostBack = "true" OnSelectedIndexChanged="Pr ojectSelec tionChange d" ></asp:DropDownList>
…
<asp:ListBox …
</EditItemTemplate>
I can capture changes of the dropdownlist ‘projects’ like this:
protected void ProjectSelectionChanged(ob ject sender, EventArgs e)
{
}
I am just wondering whether it is possible to access, for example, a ListBox within the EditItemTemplates. Is it possible and how? Many thanks.
Chris
I have a datalist with a dropdownlist (projects)
<asp:datalist …
<ItemTemplate>
…
</ItemTemplate>
<EditItemTemplate>
…
<asp:DropDownList ID = "projects" Runat="server" DataTextField="project_tit
…
<asp:ListBox …
</EditItemTemplate>
I can capture changes of the dropdownlist ‘projects’ like this:
protected void ProjectSelectionChanged(ob
{
}
I am just wondering whether it is possible to access, for example, a ListBox within the EditItemTemplates. Is it possible and how? Many thanks.
Chris
ASKER
Sure, I usually use something like this:
e.Item.FindControl("name")
But I cannot use it in ProjectSelectionChanged (see above). How can I access an item (a ListBox) from the ProjectSelectionChanged method? I know that the item must exist at this point in time, otherwise the method could not have been invoked
Ta
Chris
e.Item.FindControl("name")
But I cannot use it in ProjectSelectionChanged (see above). How can I access an item (a ListBox) from the ProjectSelectionChanged method? I know that the item must exist at this point in time, otherwise the method could not have been invoked
Ta
Chris
The DropDownList has a SelectedIndex property to get the corresponding item from the DataList.
Bob
Bob
ASKER
Sorry, but there must be a misunderstanding. I want to change a ListBox object depending on the current selection of the dropdownlist. I know how to access the selectedindex of the dropdownlist but I do not know how to access the LISTBOX OBJECT from within the ProjectSelectionChanged method (see orginal post).
Ok, maybe I am misunderstanding. If you have the DataList item, and you use item.FindControl("ListBox1 "), you can get a reference to the ListBox control.
Bob
Bob
ASKER
the problem is I cannot use item.FindControl("ListBox1 ") in the ProjectSelectionChanged method
Why not?
Bob
Bob
ASKER
coz i don't receive DataListItemEventArgs but plain EventArgs from the dropdownlist.
Let's try this VB.NET example:
Dim item As DataListItem = Me.DataList1.Items(Me.proj ects.Selec tedIndex)
Dim lb As ListBox = item.FindControl("ListBox1 ")
Bob
Dim item As DataListItem = Me.DataList1.Items(Me.proj
Dim lb As ListBox = item.FindControl("ListBox1
Bob
ASKER
sorry, i don't get it. What's 'me'? Is it 'this' in C#?
also, to access my dropdownlist i would have to use something like this:
((DropDownList)sender).Sel ectedValue
in the ProjectSelectionChanged method. i cannot access the dropdownlist directly as you suggest.
also, to access my dropdownlist i would have to use something like this:
((DropDownList)sender).Sel
in the ProjectSelectionChanged method. i cannot access the dropdownlist directly as you suggest.
That's where it helps to indicate language, .NET framework, etc.
This is untested code:
DropDownList ddlProjects = (DropDownList)sender;
DataListItem item = this.DataList1.Items[ddlPr ojects.Sel ectedIndex ];
ListBox lb = item.FindControl("ListBox1 ");
Bob
This is untested code:
DropDownList ddlProjects = (DropDownList)sender;
DataListItem item = this.DataList1.Items[ddlPr
ListBox lb = item.FindControl("ListBox1
Bob
ASKER
I am sorry but why should the SelectedIndex of a dropdownlist give me access to my <asp:ListBox object (see original code)?
Still I have tried your code (you never know):
DropDownList ddlProjects = (DropDownList)sender;
DataListItem item = this.table_details_datalis t.Items[dd lProjects. SelectedIn dex];
ListBox lb = (ListBox) item.FindControl("tables_l eft");
It produces an exception ...
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
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.ArgumentOutOfRangeE xception: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source Error:
Line 419:
Line 420: DropDownList ddlProjects = (DropDownList)sender;
Line 421: DataListItem item = this.table_details_datalis t.Items[dd lProjects. SelectedIn dex];
Line 422: ListBox lb = (ListBox) item.FindControl("tables_l eft");
Line 423:
Still I have tried your code (you never know):
DropDownList ddlProjects = (DropDownList)sender;
DataListItem item = this.table_details_datalis
ListBox lb = (ListBox) item.FindControl("tables_l
It produces an exception ...
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
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.ArgumentOutOfRangeE
Parameter name: index
Source Error:
Line 419:
Line 420: DropDownList ddlProjects = (DropDownList)sender;
Line 421: DataListItem item = this.table_details_datalis
Line 422: ListBox lb = (ListBox) item.FindControl("tables_l
Line 423:
Yep, you're right, and now I can see the bigger picture. You need to determine which DataListItem the DropDownList is bound to. You might be able to do that with a foreach and the UniqueID of the control.
Bob
Bob
ASKER
The DropDownList and ListBox are 'independent'. When the DropDownList is changed, the ListBox is reinitialised wit values from the database (depending on the SelectedValue in the DropDownList).
An idea I have is to put the ListBox in a Session object in the Item_Created method. I could then access the ListBox from within the ProjectSelectionChanged method. What do you think?
An idea I have is to put the ListBox in a Session object in the Item_Created method. I could then access the ListBox from within the ProjectSelectionChanged method. What do you think?
I think that I would like to see that happen, because I don't think that will work.
Bob
Bob
ASKER
Ok you are right. I cannot make the ListBox persistent. I also believe now that it is impossible to access it from within the ProjectSelectionChanged method.
Chris
Chris
This is what I was talking about:
protected void DropDownList1_SelectedInde xChanged(o bject sender, EventArgs e)
{
DropDownList ddlSender = (DropDownList)sender;
foreach (DataListItem item in this.DataList1)
{
DropDownList ddl = item.FindControl();
if (ddl.UniqueID == ddlSender.UniqueID)
{
ListBox lb = item.FindControl("ListBox1 ");
// Do something with the ListBox.
break;
}
}
}
Bob
protected void DropDownList1_SelectedInde
{
DropDownList ddlSender = (DropDownList)sender;
foreach (DataListItem item in this.DataList1)
{
DropDownList ddl = item.FindControl();
if (ddl.UniqueID == ddlSender.UniqueID)
{
ListBox lb = item.FindControl("ListBox1
// Do something with the ListBox.
break;
}
}
}
Bob
ASKER
this doesn't work (I tried it before).
(1) foreach does not work because System.Web.UI.WebControls. DataList does not contain a public definition for GetEnumerator
this would work:
for (int c = 0; c < this.table_details_datalis t.Items.Co unt; c++ )
{
}
but there is only one item - the dropdownlist. I cannot access the ListBox as you suggest.
C
(1) foreach does not work because System.Web.UI.WebControls.
this would work:
for (int c = 0; c < this.table_details_datalis
{
}
but there is only one item - the dropdownlist. I cannot access the ListBox as you suggest.
C
.NET version? 2.0 or 1.1?
Bob
Bob
ASKER
2.0
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks this works!
Bob