Currently i'm working on Lucky Draw System. I've already can call out the data from database and display it using label. But how can i make it display one by one in label to make the system more interesting.
This is my code :
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
string constr = ConfigurationManager.ConnectionStrings["lucky"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string query = "SELECT TOP 1[EMP_ID]FROM EMPLOYEES WHERE[Attendance] = 'Present'ORDER BY NEWID()";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
Label1.Text = dt.Rows[0]["EMP_ID"].ToString();
}
else
{
Label1.Text += "Cannot draw! ";
}
}
}
}
}
}
}
It won't work with C#. C# runs server side to build an HTML page that is then sent to the browser so at this point the whole string would be revealed anyway.
This isn't going to work unless you do it on the client. Doing it on the server simply sends all the information at one time to the browser when the page is rendered. You could set the value to a hidden form field, and then using javascript or jQuery loop
over the characters and put them into the content of a client container like a span. A delay will be needed otherwise it will just end up showing all the letters at the same time since the loop will process incredibly quickly.
Don't forget to mark useful responses as Answer if they helped you towards a solution.
I agree with vahid, I create a sample it seems that everything works well. If it not achieve your requirement, please explain more details about your problem. Also, you could make a similar sample to tell us what you want.
Best regards,
Angie
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
You can try to
make Ajax request to a WebMethod to retrieve data from database and dynamically append the data and display text letter by letter on client side, the following sample code is for your reference.
Member
3 Points
20 Posts
How to display Label Letter By Letter In C#
Apr 04, 2018 03:13 AM|Ash93|LINK
Currently i'm working on Lucky Draw System. I've already can call out the data from database and display it using label. But how can i make it display one by one in label to make the system more interesting.
This is my code :
Contributor
3899 Points
1387 Posts
Re: How to display Label Letter By Letter In C#
Apr 04, 2018 05:08 AM|vahid bakkhi|LINK
please try below code :
or you can use below code :
Please MARK AS ANSWER if suggestion helps.
None
0 Points
3 Posts
Re: How to display Label Letter By Letter In C#
Apr 04, 2018 05:19 AM|pravinkumar.v.birajdar|LINK
Member
3 Points
20 Posts
Re: How to display Label Letter By Letter In C#
Apr 04, 2018 06:26 AM|Ash93|LINK
Hi pravin, your code didn't work. I've already try it.
Member
3 Points
20 Posts
Re: How to display Label Letter By Letter In C#
Apr 04, 2018 06:30 AM|Ash93|LINK
Hi vahid, what 's' stand for. Because it show error "The name 's' does not exist in current context"
Contributor
3899 Points
1387 Posts
Re: How to display Label Letter By Letter In C#
Apr 04, 2018 06:49 AM|vahid bakkhi|LINK
you must replace your string to 's' below like :
Please MARK AS ANSWER if suggestion helps.
Member
3 Points
20 Posts
Re: How to display Label Letter By Letter In C#
Apr 04, 2018 09:09 AM|Ash93|LINK
It still not working vahid.
Contributor
3899 Points
1387 Posts
Re: How to display Label Letter By Letter In C#
Apr 04, 2018 09:27 AM|vahid bakkhi|LINK
what is your problem right now , please give mo more detils
Please MARK AS ANSWER if suggestion helps.
All-Star
40110 Points
12972 Posts
Re: How to display Label Letter By Letter In C#
Apr 04, 2018 09:38 AM|PatriceSc|LINK
Hi,
It won't work with C#. C# runs server side to build an HTML page that is then sent to the browser so at this point the whole string would be revealed anyway.
You'll likely have to to use client client side JavaScript instead. Try https://stackoverflow.com/questions/7264974/show-text-letter-by-letter
Contributor
3899 Points
1387 Posts
Re: How to display Label Letter By Letter In C#
Apr 04, 2018 10:56 AM|vahid bakkhi|LINK
for server side you can use a literal control for do it below code :
in html put :
and in code behind :
and for your solution you must put a literal control side you label control :
and in code behind change :
Please MARK AS ANSWER if suggestion helps.
None
0 Points
1 Post
Re: How to display Label Letter By Letter In C#
Apr 04, 2018 12:43 PM|Prabhat|LINK
{
Label1.Text += c + "\n";
}
I think this will work
All-Star
25891 Points
5831 Posts
Re: How to display Label Letter By Letter In C#
Apr 04, 2018 11:34 PM|markfitzme|LINK
This isn't going to work unless you do it on the client. Doing it on the server simply sends all the information at one time to the browser when the page is rendered. You could set the value to a hidden form field, and then using javascript or jQuery loop over the characters and put them into the content of a client container like a span. A delay will be needed otherwise it will just end up showing all the letters at the same time since the loop will process incredibly quickly.
Contributor
2586 Points
1723 Posts
Re: How to display Label Letter By Letter In C#
Apr 05, 2018 05:06 AM|Rameezwaheed|LINK
Hi,
Try to bind the data in Data list control like below and Repeat the columns as per your need
Thanks
Mark as an answer if it helps
All-Star
32769 Points
3794 Posts
Microsoft
Re: How to display Label Letter By Letter In C#
Apr 05, 2018 07:40 AM|Angie xu - MSFT|LINK
Hi Ash93,
I agree with vahid, I create a sample it seems that everything works well. If it not achieve your requirement, please explain more details about your problem. Also, you could make a similar sample to tell us what you want.
Best regards,
Angie
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
All-Star
38056 Points
5149 Posts
Microsoft
Re: How to display Label Letter By Letter In C#
Apr 05, 2018 07:50 AM|Fei Han - MSFT|LINK
Hi Ash93,
You can try to make Ajax request to a WebMethod to retrieve data from database and dynamically append the data and display text letter by letter on client side, the following sample code is for your reference.
Html code:
jQuery code:
With Regards,
Fei Han
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue.