Solved

ListView Control Help!

Posted on 2011-09-26
10
299 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

770 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