Solved

ListView Control Help!

Posted on 2011-09-26
10
305 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
Technology Partners: 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!

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
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).

724 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