?
Solved

ListView Control Help!

Posted on 2011-09-26
10
Medium Priority
?
306 Views
Last Modified: 2012-08-14
Hello Experts,

I have two ListView Controls. The first ListView Control is usded to retrieve all Course Names in my DB. The second ListView Control is needed to view the Master / Detail information for the Selected Course Names.

The problem I'm having is getting the Master / Detail information for the second ListView Control.

I'm using ASP.NET 4, ADO.NET for retrieving the data from DB and C# as the programming language. Below is my current setup.

STEP 1: Retrieve data to first ListView Control (this works)
STEP 2: Display Master / Detail information from selected value from STEP 1 (need help with this step).



HTML:
<asp:ListView ID="lvCourses" GroupItemCount="2" runat="server">
                    <GroupTemplate>
                        <div class="listview">
                            <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
                        </div>
                    </GroupTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" Text='<%# Eval("ghaco_name") %>' CssClass="label"
                            Width="280px" runat="server"></asp:LinkButton>
                    </ItemTemplate>
                    <AlternatingItemTemplate>
                        <asp:LinkButton ID="LinkButton1" Text='<%# Eval("ghaco_name") %>' CssClass="label"
                            Width="280px" runat="server"></asp:LinkButton>
                        <%--<asp:Label ID="Label2" Text='<%# Eval("ghaco_name") %>' CssClass="label" runat="server" Width="280px"></asp:Label>--%>
                    </AlternatingItemTemplate>
                    <SelectedItemTemplate>                    
                        <asp:HyperLink ID="ProductLink" runat="server" Text='<% #Eval("Name")%>' NavigateUrl='<%#"index.aspx?ghaoc_id=" + Eval("ghaoc_id") %>' />
                    </SelectedItemTemplate>
                </asp:ListView>
                <br />
                <asp:ListView ID="lvCoursesDesc" runat="server">
                    <ItemTemplate>
                        <asp:Label ID="Label4" Text='<%# Eval("ghaco_desc") %>' CssClass="label" runat="server"
                            Width="500px"></asp:Label>
                    </ItemTemplate>
                </asp:ListView>

Open in new window



CODEBEHIND:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;

public partial class programinfo_ghap_index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RetrieveCourses();
    }

    protected void RetrieveCourses()
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveCourses";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                lvCourses.DataSource = rdr;
                lvCourses.DataBind();
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }

    protected void RetrieveCoursesDesc()
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            int ghaoc_id = Convert.ToInt32(Request.QueryString["ghaoc_id"]);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveCoursesDesc";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@ghaoc_id", SqlDbType.Int).Value = ghaoc_id;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                lvCoursesDesc.DataSource = rdr;
                lvCoursesDesc.DataBind();
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }

    protected void LinkButton1_Click(object sender, CommandEventArgs e)
    {
        RetrieveCoursesDesc();
    }
}

Open in new window

0
Comment
Question by:asp_net2
[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
  • 5
  • 2
10 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36598402

try this

  protected void RetrieveCoursesDesc()
    {

if (Request.QueryString["ghaoc_id"] != "")
{
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
           

 int ghaoc_id = Convert.ToInt32(Request.QueryString["ghaoc_id"]);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveCoursesDesc";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@ghaoc_id", SqlDbType.Int).Value = ghaoc_id;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                lvCoursesDesc.DataSource = rdr;
                lvCoursesDesc.DataBind();
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }
}
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36598875
Hi pratima_mcs,

That did not work :(

I don't get any error messages when running the code,  I just don't get any results to appear in the ListView Control "lvCoursesDesc" after clicking on a link from the returned data in the ListView Control "lvCourses".

Could it have something to do with the way I have the links setup within the ListView Control "lvCourses" in regards to passing the parameter "ghaoc_id" in the link to the other ListView Control "lvCoursesDesc"?

I tested my Stored Procedures and they all work fine.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36600978
Hi pratima_mcs,

Are you still able to help with this post?
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 2000 total points
ID: 36702803
check on this

 int ghaoc_id = Convert.ToInt32(Request.QueryString["ghaoc_id"]);

whether you are getting Request.QueryString["ghaoc_id"]
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36707475
Ok, how does the rest of the code/markup look to you?
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36707529
@pratima_mcs:

Ok, I set a breakpoint on int ghaoc_id = Convert.ToInt32(Request.QueryString["ghaoc_id"]); and it appears that the line never gets touched which is why I'm not getting anything returned based on the ghaoc_id value.

Now, the part that I need help with. I need to click on value in first ListView and then see the Master/Details of that value in the second ListView Control. In order for me to do that though I need to pass the value (ghaoc_id) from the value selected in the first ListView to the second ListView correct? If so, what am I doing wrong??
0
 
LVL 4

Author Closing Comment

by:asp_net2
ID: 36718752
I was not calling the Event in Page_Load. Once I did that it worked.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

800 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