Solved

datalist, EditItemTemplate problem

Posted on 2006-11-03
22
510 Views
Last Modified: 2012-05-05
Hi,

I have a datalist with a dropdownlist (projects)

<asp:datalist …
      <ItemTemplate>
              …
      </ItemTemplate>

      <EditItemTemplate>

            …
            <asp:DropDownList ID = "projects" Runat="server" DataTextField="project_title" DataValueField="project_uid" AutoPostBack = "true" OnSelectedIndexChanged="ProjectSelectionChanged" ></asp:DropDownList>

            …

            <asp:ListBox …

      </EditItemTemplate>

I can capture changes of the dropdownlist ‘projects’ like this:

protected void ProjectSelectionChanged(object 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
0
Comment
Question by:csetzkorn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 11
22 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17866020
Yes, you can use Item.FindControl(id) to find the ListBox, but the timing of this is critical, since you are changing modes.  I normally don't define an EditItemTemplate, but let them edit all the DataList items all the time.

Bob
0
 

Author Comment

by:csetzkorn
ID: 17866048
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17866066
The DropDownList has a SelectedIndex property to get the corresponding item from the DataList.

Bob
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:csetzkorn
ID: 17866098
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).
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17866120
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
0
 

Author Comment

by:csetzkorn
ID: 17866148
the problem is I cannot use item.FindControl("ListBox1") in the ProjectSelectionChanged method
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17866159
Why not?

Bob
0
 

Author Comment

by:csetzkorn
ID: 17866178
coz i don't receive DataListItemEventArgs but plain EventArgs from the dropdownlist.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17866223
Let's try this VB.NET example:

Dim item As DataListItem = Me.DataList1.Items(Me.projects.SelectedIndex)
Dim lb As ListBox = item.FindControl("ListBox1")

Bob
0
 

Author Comment

by:csetzkorn
ID: 17866271
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).SelectedValue

in the ProjectSelectionChanged method. i cannot access the dropdownlist directly as you suggest.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17866325
That's where it helps to indicate language, .NET framework, etc.  

This is untested code:

DropDownList ddlProjects = (DropDownList)sender;
DataListItem item = this.DataList1.Items[ddlProjects.SelectedIndex];
ListBox lb = item.FindControl("ListBox1");

Bob
0
 

Author Comment

by:csetzkorn
ID: 17866393
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_datalist.Items[ddlProjects.SelectedIndex];
ListBox lb = (ListBox) item.FindControl("tables_left");

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.ArgumentOutOfRangeException: 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_datalist.Items[ddlProjects.SelectedIndex];
Line 422:        ListBox lb = (ListBox) item.FindControl("tables_left");
Line 423:
 
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17880382
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
0
 

Author Comment

by:csetzkorn
ID: 17880422
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?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17880438
I think that I would like to see that happen, because I don't think that will work.

Bob
0
 

Author Comment

by:csetzkorn
ID: 17880470
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17880496
This is what I was talking about:

    protected void DropDownList1_SelectedIndexChanged(object 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
0
 

Author Comment

by:csetzkorn
ID: 17880625
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_datalist.Items.Count; c++ )
{

}

but there is only one item - the dropdownlist. I cannot access the ListBox as you suggest.

C
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17880641
.NET version?  2.0 or 1.1?

Bob
0
 

Author Comment

by:csetzkorn
ID: 17880651
2.0
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 17880673
Try this (replacing DropDownList and Listbox1 with IDs for the controls--I left out some things before:

   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlSender = (DropDownList)sender;
        foreach (DataListItem item in this.DataList1.Items)
        {
            DropDownList ddl = (DropDownList)item.FindControl("DropDownList1");
            if (ddl.UniqueID == ddlSender.UniqueID)
            {
                ListBox lb = (ListBox)item.FindControl("ListBox1");
                // Do something with the ListBox.
                break;
            }
        }
    }

Bob
0
 

Author Comment

by:csetzkorn
ID: 17880760
thanks this works!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET MVC - Views 3 61
asp web application 3 55
Best book for Internet security 4 52
How do i create a simple array in a public class? 6 36
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question