?
Solved

How to use user control in a repeater

Posted on 2009-07-15
2
Medium Priority
?
405 Views
Last Modified: 2013-11-05
I have created a user control in ASP.NET that contains 11 different gridviews, each intended to display a different set of people (like a depth chart).  I also have a dropdownlist with 30 items, where each item would have different people in the gridview.  How does one go about using the repeater control to accomplish this?

Adding to the fun:  In the codebehind for the control, I am doing formatting on each entry based upon certain criteria (basically, changing the font color if certain conditions are met).
0
Comment
Question by:houstonastros
[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
2 Comments
 
LVL 20

Accepted Solution

by:
ddayx10 earned 500 total points
ID: 24865405
1) Setup a databinding routine for your gridview(s)
2) Set dropdownlist property autopostback=true
3) Add selectedindexchanged event to dropdownlist
4) In selectedindexchanged event use the value and/or text to get new data and re-bind to gridview(s).

There's a sample that does that here:
http://www.experts-exchange.com/Programming/Languages/.NET/Q_24481708.html

I'll also past the code below (see snippet).
****ASPX PAGE (could just as easily be ASCX page)****
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<span>New Title: </span>
	<asp:TextBox ID="txtTitle" runat="server" />
	<asp:Button ID="Button1" runat="server" Text="Add Record" OnClick="Button1_Click" />
	<span>Adds a record to the database.</span>
	<p style="margin: 5px 0px; padding: 0px;">
		Dropdown bound to available id values in database table.</p>
	<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
	</asp:DropDownList>
	<p style="margin: 5px 0px; padding: 0px;">
		GridView shows items with id equal to dropdownlist selected value.</p>
	<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
	</asp:GridView>
	
	</form>
</body>
</html>
 
 
****CODE BEHIND****
using System.Data.SqlClient;
 
public partial class Default4 : System.Web.UI.Page
{
    public string connStr
    {
        get
        {
            string conn = ConfigurationManager.ConnectionStrings["conn"].ToString();
            return conn;
 
        }
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindToDDL();
            bindToGrid();
        }
 
 
    }
 
    protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindToGrid();
    }
 
    private void bindToDDL()
    {
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand("SELECT catID,title FROM roLIST", conn);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        ddl1.DataTextField = "title";
        ddl1.DataValueField = "catID";
        ddl1.DataSource = dr;
        ddl1.DataBind();
        dr.Close();
        conn.Close();
        conn.Dispose();
        
    }
 
 
    private void bindToGrid()
    {
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand("SELECT * FROM roLIST WHERE catID = @choice",conn);
        cmd.Parameters.Add(new SqlParameter("@choice", Convert.ToInt32(ddl1.SelectedValue)));
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        conn.Close();
        conn.Dispose();
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand("INSERT INTO roLIST (title, listOrder) VALUES(@title, @listOrder)", conn);
        cmd.Parameters.Add(new SqlParameter("@title", txtTitle.Text.ToString()));
        cmd.Parameters.Add(new SqlParameter("@listOrder", Convert.ToInt32(ddl1.Items.Count)+1));
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        conn.Dispose();
        bindToDDL();
        bindToGrid();
 
    }
}

Open in new window

0

Featured Post

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!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

771 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