Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

ListView Control Help!

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

Avatar of Pratima
Pratima
Flag of India image


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();
            }
        }
    }
}
Avatar of Brian

ASKER

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.
Avatar of Brian

ASKER

Hi pratima_mcs,

Are you still able to help with this post?
ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

Ok, how does the rest of the code/markup look to you?
Avatar of Brian

ASKER

@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??
Avatar of Brian

ASKER

I was not calling the Event in Page_Load. Once I did that it worked.