Friday 6 February 2015

Generate Random Numbers in Button_Click Event Get Textbox Values Using Asp.Net C#

No comments    
categories: , , ,
Generate Random Numbers in Button_Click Event Get Textbox Values


Generate Random Numbers Given Input From TextBox  Click The Button Event Display The Random Numbers To Labels. 
      

                              Download Coding
                                    
                                               Download                        

                          DEMO





HTML CODING

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr><td>
                <asp:Label ID="Label1" runat="server" 
               Text="Minimum Range"></asp:Label>
                </td><td>
             <asp:TextBox ID="txtMinimumRange"
             runat="server"></asp:TextBox>
                </td></tr>
            <tr><td>
                <asp:Label ID="Label2" runat="server" 
                     Text="Maximum Range"></asp:Label>
                </td><td>
                    <asp:TextBox ID="txtMaximumRange" 
               runat="server"></asp:TextBox>
                </td></tr>
            <tr><td colspan ="2">
                <asp:Button ID="RandomNumber" runat="server" OnClick="RandomNumber_Click" Text="Get Random" />
                <br />
                <table>
                    <tr><td>
                        <asp:Label ID="Label3" runat="server" Font-                           Size="X-Large" 
             Text="Generate Random Numbers"></asp:Label>
                        </td></tr><tr><td>
                    <asp:Label ID="lblGetRandom1" 
        runat="server" ForeColor="#9900CC" Font-Size="Large">
                    </asp:Label>
                    </td></tr>
                    <tr>
                        <asp:Label ID="lblGetRandom2" runat="server" 
              ForeColor="#CC3300" Font-Size="Large"></asp:Label>
                        </td></tr>
                    <tr><td>
                        
                        <asp:Label ID="lblGetRandom3" runat="server" 
         Font-Size="Large" ForeColor="#3333CC"></asp:Label></td></tr>
                </table></td></tr>
           

        </table>
    </div>
    </form>
</body>
</html>


C# CODING


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Random_Number : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void RandomNumber_Click(object sender, EventArgs e)
    {
        // r.Next(MinimumValue,MaximumValue);

        if (Convert.ToInt32(txtMinimumRange.Text) < Convert.ToInt32(txtMaximumRange.Text))
        {

            Random r = new Random();

            lblGetRandom1.Text = r.Next(Convert.ToInt32(txtMinimumRange.Text), Convert.ToInt32(txtMaximumRange.Text)).ToString();

            lblGetRandom2.Text = r.Next(Convert.ToInt32(txtMinimumRange.Text), Convert.ToInt32(txtMaximumRange.Text)).ToString();
           
            lblGetRandom3.Text = r.Next(Convert.ToInt32(txtMinimumRange.Text), Convert.ToInt32(txtMaximumRange.Text)).ToString();


        }

        else
        {

            Response.Write("<script>alert('Number(s) Range Not Valid')</script>");
        }

    }

  
}
















0 comments:

Post a Comment