I want Create Label Dynamically

Posted by Karthikreddy under ASP.NET on 3/14/2013 | Points: 10 | Views : 5109 | Status : [Member] | Replies : 7
I want Create Label Dynamically with out panel or PlaceHolder

I am creating a web page and in that I want to show data on label. The data will be shown through a SQL connection. I know how to create labels but I don't know how to create them to a particular place of web page

k@rth!k
karthikreddy08a50@gmail.com



Responses

Posted by: Satyapriyanayak on: 3/14/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm6.aspx.cs" Inherits="WebApplication17.WebForm6" %>

<!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>
<script type="text/javascript">
function test()
{
var formtag = document.createElement("form");
var labeltag=document.createTextNode("Name:");
formtag.appendChild(labeltag);
document.body.appendChild(formtag);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="create label" onclick="test()"/>
</div>
</form>
</body>
</html>


If this post helps you mark it as answer
Thanks

Karthikreddy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 3/14/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

you can do it in C# also.

  Label txtlabel  = new Label();

txtlabel.Text = "hi";
txtlabel.ID = "hello";
Page.Controls.Add(txtlabel);


Give the text as what you retrieve from the database.

Thanks & Regards,
Santhi .V

Karthikreddy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/14/2013 [Member] Starter | Points: 25

Up
0
Down
hi Satyapriyanayak

this is my code.
i want to create two dynamic labels .after first label i need place some text then second label need to display (for example i need fetch question from db and it display in label bellow that label need display four options ) i didn't find solution can u provide the solution for this

for (int i = 1; i<3 ; i++)

{
Label label = new Label();
label.ID = "Label" + i;
label.Text = "Label" + i;
div1.Controls.Add(label);
div1.Controls.Add(new LiteralControl("<br />"));
}


<div id="div1"  runat="server" ></div>


k@rth!k
karthikreddy08a50@gmail.com

Karthikreddy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 3/14/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
try this code

private void Dclip()
{
Dt = ReturnDt("Select * from dbo.Table_1");
for (int i = 1; i<3 ; i++)
{
Label label = new Label();
label.ID = "Label" + i;
if (i == 1)
{
label.Text = "Label" + i;
}else if(i==2)
{
label.Text = Dt.DefaultView[0][0].ToString();
}
div1.Controls.Add(label);
div1.Controls.Add(new LiteralControl("<br />"));
}
}

public DataTable ReturnDt(string RecStr)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = RecStr;
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(Dt);
return Dt;
}

Mark as Answer if its helpful to you

Karthikreddy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/14/2013 [Member] Starter | Points: 25

Up
0
Down
Thanks for responding

Please find attachments (i attached one image i want out put like that )

but i used separate labels and radio button list to get that output but i want to create labels dynamically
 Download source file

k@rth!k
karthikreddy08a50@gmail.com

Karthikreddy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 3/14/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Here is the code:
It gives what exactly you need:
Design page:
<asp:Table ID="Table1" runat="server">

</asp:Table>

cs page:
protected void Page_Load(object sender, EventArgs e)

{
BindLabel();
}


public void BindLabel()
{
DataTable dt = new DataTable();
dt.Columns.Add("Question");
dt.Columns.Add("Option1");
dt.Columns.Add("Option2");
dt.Columns.Add("Option3");
dt.Columns.Add("Option4");
dt.Rows.Add("What is our Country Name?", "India", "Srilanka", "Pakistan", "Bangladesh");
dt.Rows.Add("What is our National Animal?", "Tiger", "Lion", "Elephant", "Girafee");


if (dt.Rows.Count > 0)

{
int count = dt.Rows.Count;
for (int i = 0 ; i < count; i++)
{
TableRow tr = new TableRow();
tr.ID = "tr" + i;
TableCell tc = new TableCell();
tc.ID = "tc" + i;


Label lbl = new Label();
lbl.ID = "ID" + i;
lbl.Text = dt.Rows[i]["Question"].ToString();
tc.Controls.Add(lbl);
tr.Cells.Add(tc);

Table1.Rows.Add(tr);

for (int j = 1; j <= 4; j ++)
{


TableRow troption = new TableRow();
troption.ID = "tr" + i + j;

TableCell tcoption = new TableCell();
tcoption.ID = "tc" + i + j;
RadioButton rdoption = new RadioButton();
rdoption.ID = "rdoption" + i + j;

Label lboption = new Label();
lboption.ID = "option" + i + j;
lboption.Text = dt.Rows[i]["Option"+j].ToString();
tcoption.Controls.Add(rdoption);
tcoption.Controls.Add(lboption);
troption.Cells.Add(tcoption);
Table1.Rows.Add(troption);
}
}

}
}


Thanks & Regards,
Santhi .V

Karthikreddy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 3/14/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Just that function does all your requirement.

This will generate the control dynamically till you have the values in the database.
It depends on the row count.
Just copy and paste the code.
You will get it.
You need to fire the events from the code behind.
That would be the next step.


Thanks & Regards,
Santhi .V

Karthikreddy, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response