skip to Main Content

I have just a simple message page which consists of From: Text: and a Submit button, then I have another page, which contains nothing, it’s my "Message Board" the most recent posted message goes on top of the board, both are aspx pages with master page.

I have a SQL DB, I’m already assuming there will be a table with From: Message:(with varchar i think), but what i don’t understand how it will get inserted into the messageboard page in a most recent to oldest list fashion.

Message.aspx – From: Text: Submit
MessageBoard.aspx – just a div , messages submitted will appear here in a drop down list

I want it to be super simple no cool features, only "Submit the message" -> "Appears on MessageBoard.aspx to everyone",
and that’s it

2

Answers


  1. You don’t explain what you mean by "what i don’t understand how it will get inserted into the messageboard page in a most recent to oldest list fashion", so I can only guess.

    When a new message is posted, you insert it into the database, including a DateTime column. Your message list page then just grabs the latest nn messages, ordered by newest first.

    I’m assuming that you know how to do that. If not, do some reading about Entity Framework Core, as that provides a very good way of handling databases.

    So, in princple, your question is no more complex than that. However, there are many variations on this, such as having the message list updated in real time, for which you should use SignalR, but without more specific explanation of what you want, it’s hard to make any suggestions.

    Login or Signup to reply.
  2. Ok, there are seveal moving parts.

    Assuming you have SQL server running. Assuming you have a valid conneciton?

    Ok, then on the post a new message page, you have this markup:

            <h3>Post a message</h3>
    
            <h4>enter your name</h4>
            <asp:TextBox ID="txtName" runat="server" Width="250px"></asp:TextBox>
    
            <br />
    
            <h4>Enter your message</h4>
            <asp:TextBox ID="txtMsg" runat="server" Height="185px" Width="520px"
                TextMode="MultiLine" Font-Size="Large" style="border-radius:20px;border:solid 2px"
                ></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="cmdNewMessage" runat="server" Text="Post Message" CssClass="btn"
                OnClick="cmdNewMessage_Click" />
    

    And code behind looks like this:

        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void cmdNewMessage_Click(object sender, EventArgs e)
        {
            string strSQL =
                @"INSERT INTO tblMessages (UName, Message, MessageDate)
                VALUES (@UName, @Message, @MessageDate)";
    
            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
            {
                using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
                {
                    conn.Open();
                    cmdSQL.Parameters.Add("@UName", SqlDbType.NVarChar).Value = txtName.Text;
                    cmdSQL.Parameters.Add("@Message",SqlDbType.NVarChar).Value = txtMsg.Text;
                    cmdSQL.Parameters.Add("@MessageDate", SqlDbType.NVarChar).Value = DateTime.Now;
                    cmdSQL.ExecuteNonQuery();
                }
            }
            Response.Redirect("MessageBoard.aspx");
    
        }
    

    So, it looks like this:

    enter image description here

    when you hit post message, we jump to this page, and markup:

            <asp:Button ID="cmdPost" runat="server" 
                Text="Post a new message" 
                CssClass="btn" OnClick="cmdPost_Click" />
            <br />
            <br />
            <h2>Messages</h2>
    
            <asp:GridView ID="GridView1" runat="server"  Width="50%"
                AutoGenerateColumns="False" DataKeyNames="ID"  >
                <Columns>
                    <asp:BoundField DataField="UName" HeaderText="Posted by"  />
                    <asp:BoundField DataField="MessageDate" HeaderText="At" ItemStyle-Width="180px" />
                    <asp:TemplateField HeaderText="Message" >
                        <ItemTemplate>
                            <asp:Textbox ID="txtMsg" runat="server" TextMode="MultiLine" Width="100%" 
                                Text='<%# Eval("Message") %>' 
                                Height='<%# (Regex.Matches(Eval("Message").ToString() , System.Environment.NewLine).Count + 1) * 30 %>' 
                                >
                            </asp:Textbox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
    
    
            </asp:GridView>
    

    And code is:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadGrid();
        }
    
        void LoadGrid()
        {
    
            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
            {
                string strSQL = "SELECT * FROM tblMessages ORDER BY MessageDate DESC";
                using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
                {
                    conn.Open();
                    GridView1.DataSource = cmdSQL.ExecuteReader();
                    GridView1.DataBind();
                }
            }
        }
    
        protected void cmdPost_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewMessage.aspx");
        }
    

    And we now see/have this:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search