how can i add LinkButton in DataList Dynamically at a runt time.
In my website i m Fetching Author Name from table and i want to create linkButton Dynamically According to author if for a book there is one author then linkButton will be 1, if 2 authors then linkbutton will be 2.please help me thanx in advance.

omkar mhaiskarPosted Apr 26, 2011, 5:43 AM
ok i got your problem.
sorry for late reply just for some i was out of station so couldn't reply you quickly.
here is some code try it and let me know.
design page.
need to write following code:-
code behind.
protected void dttest_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem | e.Item.ItemType == ListItemType.Item)
{
string strval = ((Label)(e.Item.FindControl("lblAuthor"))).Text;
PlaceHolder newplaceholder = (PlaceHolder)e.Item.FindControl("PlaceHolder1");
string[] Author = (lnk.Text).Split(',');
for (int i = 0; i < Author.Length; i++)
{
LinkButton lnkAuthor = new LinkButton();
lnkAuthor.ID = lnkAuthor.Text + i;
//Table t = (Table)e.Item.FindControl("DivLnk");
newplaceholder.Controls.Add(lnkAuthor);
}
}
}
Thanks And Reagrds
Omkar.
Ankit NandekarPosted Apr 21, 2011, 6:03 AM
omkar this is my source code
I have tried with table but its not working for me
this is my c# code
Label lnk = (Label)e.Item.FindControl("lblAuthor");
string[] Author = (lnk.Text).Split(',');
for (int i = 0; i < Author.Length; i++)
{
LinkButton lnkAuthor = new LinkButton();
lnkAuthor.ID = lnkAuthor.Text +i;
Table t = (Table)e.Item.FindControl("DivLnk");
t.Controls.Add(lnkAuthor);
}
this code showing me error bcoz of table tell me how to do this think using place holder.
I m making a array of all author name and upto all author name i m using for loop to add control is it right process?
plz tell me
omkar mhaiskarPosted Apr 21, 2011, 5:48 AM
As per your question you just need to put placeholder in datalist where you want to show the links.
and as well as put one label. in the same div. and make sure that the label should be invisible. making style="Display:none".
simple procedure to solve your problem.
bring the data which you want to show in the datalist. bind as per your requirement. and bind the bookid to the label.
show when you try to bring author list it will be good for you.
also use rowdatabound to the bind authorlist.
in which when you are binding all data per row it will recdirect to rowdatabound for perticular row. at the same time you jsut find the label from row then you will get bookid. bring all the data(authorlist) through bookid in rowdatabound.
on that time find placeholder from same row . and you have also actual number of authors also.
so just as per rowcount you add linkbutton dynamically in placeholder.
so just try this. if you get any problem. feel free ask me any doubt.
Thanks And Regards
Omkar.