Click here to Skip to main content
15,911,707 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Repeater that shows safety messages from the SafetyMessages Table in the database. Since I will put this Repeater in many pages of the Website, I want to this Repeater to show different messages in each page (I want the repeater to show the data from the database in each page in different order). For example, if it shows message#1 in this page as a first message then message#2 and so on, it should show message#10 as a first message in the other web page and so on.
So how to do that?

My query is very simple like this:
SQL
SELECT MessageID, MessageDesc, MessageAuthor FROM [SafetyMessage]


Is this issue related to the query? Or it can be done from the Repeater itself?
Posted

The simplest way to achieve this is to make a UserControl [^] so that you will be able to pass the desired message in each page. You don't need to have a repeater control to display the message. The user control may be designed using labels that will display the message and a query that will accept different criteria in each page. Criteria will be passed from each page as property from the user control. I put a code fragment that helps you as a starting point.
C#
public partial class MyMessageBoard : UserControl
    {

        public string Criteria
        {
            get
            {

                if (ViewState["Criteria"] != null)
                    return ViewState["Criteria"].ToString();
                    else
                        return " assign default criteria";

            }
            set {
                if (value != null)
                    ViewState["Criteria"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DisplayMessage();
            }
        }

        private void DisplayMessage()
        {
            // Put your ADO.Net code that helps to select message based on the criteria
            // SELECT MessageID, MessageDesc, MessageAuthor FROM [SafetyMessage] WHERE Criteria
            // Execute the query and get the result
            // Finally display the result in the label control.
            // lblMessage.Text = "";
        }

    }
 
Share this answer
 
Comments
matrix388 23-Dec-11 8:43am    
Could you please tell me how I can retrieve the data from the database and put it inside a Label or Repeater control? I am struggling with this step.
You can solve in by either changing in query or in code--

you can use order by clause in query or change sortexpression in different instances of repeater data source.
--Pankaj
 
Share this answer
 
Comments
matrix388 11-Dec-11 13:08pm    
Thanks for your help, but could you please show me your answers in a part of the code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900