3. 9/2/2015 Create LinkButton in RadGrid dynamically without Item Template tags in aspx page(Using code behind file) Grid UI for ASP.NET AJAX Forum
http://www.telerik.com/forums/createlinkbuttoninradgriddynamicallywithoutitemtemplatetagsinaspxpageusingcodebehindfile 3/15
="Both"
Skin
="Outlook">
<
ClientSettings
>
<
Scrolling
AllowScroll
="True"
UseStaticHeaders
="false"
aveScrollPosition
="true
FrozenColumnsCount
="0"
/>
</
ClientSettings
>
</
telerik
:
RadGrid
>
MainGrid2.DataSource = dT;
//datasource is DataTable
string
colName = dT.Columns[0].ColumnName;
//getting the first column name
GridButtonColumn
btncol =
new
GridButtonColumn
();
btncol.ButtonType =
GridButtonColumnType
.LinkButton;
btncol.DataTextField = colName;
this
.MainGrid2.MasterTableView.Columns.Add(btncol);
// in the output,a new column is added in the grid at the firstPlace.
MainGrid2.DataBind();
I attached one image in which i clearly mentioned my query...
DataTable dT = HtmlTableParser.ParseTable(pvt);
neededoutput.JPG
string colName = dT.Columns[0].ColumnName; GridButtonColumn btncol = new GridButtonColumn(); btncol.ButtonType = GridButtonColumnType.LinkButton; btncol.DataTextField = colName; this.MainGrid2.MasterTableView.Columns.Add(btncol); MainGrid2.DataSource = dT; MainGrid2.DataBind();
Reply
Princy Link to this postPosted 20 Sep 2010
6. 9/2/2015 Create LinkButton in RadGrid dynamically without Item Template tags in aspx page(Using code behind file) Grid UI for ASP.NET AJAX Forum
http://www.telerik.com/forums/createlinkbuttoninradgriddynamicallywithoutitemtemplatetagsinaspxpageusingcodebehindfile 6/15
17421 posts
Member since:
Mar 2007
C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
LinkButton linkBtn = (LinkButton)item["NewButton"].Controls[0];
linkBtn.ToolTip ="new tooltip"; //setting ToolTip
linkBtn.CssClass = "PivotGrid"; // assigning CSSClass
}
}
Thanks,
Princy.
Reply
Alexis
23 posts
Member since:
Aug 2010
Link to this postPosted 22 Sep 2010
Hi Princy,
As of now ur coding is working fine for me.... now i need ur help for someother work....
My Req is :
1). I’m placing cursor on Amelia(u can see in that attachment), I want to show the toop tip with that name.
but its showing New Tool Tip.
How can I get what I’m looking for this implementation. Can u help me on this Princy?
2).Is it possible to show the Rad Grid header in some other way?
Alexis Princy Smith Michel
Age Salary Age Salary Age Salary Age Salary
25 5000US
D
26 5000US
D
35 6000USD 24 4000USD
I want to show the Rad Grid Header with 2 headers... Main Header, and sub header with each main
header... also i need this to be done by dynamically... We get this header as DataRow from Data Table.
but we are loop throughing to display this format on header, but we cant get this format on Rad Grid...
Can u plz help us regarding this....
3) How can i assign Fore color to Rad Grid Headers alternate columns in programmatically?
We need Solution urgently... Plz Help us as soon as possible....
Thanks in Advance,
Alexis
problemontoolttip.JPG
Reply
Princy
ANSWER
Link to this postPosted 23 Sep 2010
Hello Alexis,
1) Try the following code in ItemDataBound event to set Tooltip with that cell value.
C#:
10. 9/2/2015 Create LinkButton in RadGrid dynamically without Item Template tags in aspx page(Using code behind file) Grid UI for ASP.NET AJAX Forum
http://www.telerik.com/forums/createlinkbuttoninradgriddynamicallywithoutitemtemplatetagsinaspxpageusingcodebehindfile 10/15
GridDataItem item = (GridDataItem)e.Item;
if (item["FieldName"].Text == " ")
{
LinkButton MyLinkButton = new LinkButton();
MyLinkButton.Text = "Add";
item["FieldName"].Controls.Add(MyLinkButton);
item["FieldName"].ForeColor = System.Drawing.Color.Red;
MyLinkButton.Click += new EventHandler( MyLinkButton_Click);
}
}
}
void MyLinkButton_Click(object sender, EventArgs e)
{
}
Thanks,
Shinu.
Reply
Jittu
18 posts
Member since:
Sep 2012
Link to this postPosted 25 Sep 2012
Hi Shinu,
Thanks for the reply .I have tried doing that but its not working. It doesn't even add the Link button.
Rajini
Reply
Shinu
17764 posts
Member since:
Mar 2007
Link to this postPosted 25 Sep 2012
Hi,
In general the proper place for adding controls to the grid items is in ItemCreated. But in the case of
adding controls to the cells of GridBoundColumn,you cannot use ItemCreated only, but a combination of
ItemCreated and ItemDataBound. This is due to the fact that the control created in ItemCreated will be
erased when databinding this control. Also, if you create the control in ItemDataBound when the controls
are created from ViewState, the grid will not raise ItemDataBound, and the control will not be created and
would not raise postback events. The solution for such cases is to create the control in ItemDataBound
and recreate this control if needed on ItemCreated for subsequent postbacks. Here is the sample code.
C#:
11. 9/2/2015 Create LinkButton in RadGrid dynamically without Item Template tags in aspx page(Using code behind file) Grid UI for ASP.NET AJAX Forum
http://www.telerik.com/forums/createlinkbuttoninradgriddynamicallywithoutitemtemplatetagsinaspxpageusingcodebehindfile 11/15
C#:
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
if (item["FieldName"].Text == " ")
{
LinkButton MyLinkButton = new LinkButton();
MyLinkButton.Text = "Add";
item["FieldName"].Controls.Add(MyLinkButton);
item["FieldName"].ForeColor = System.Drawing.Color.Red;
MyLinkButton.Click += new EventHandler(MyLinkButton_Click);
}
}
}
void MyLinkButton_Click(object sender, EventArgs e)
{
}
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs
e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
if (item["FieldName"].Text == " ")
{
LinkButton MyLinkButton = new LinkButton();
MyLinkButton.Text = "Add";
item["FieldName"].Controls.Add(MyLinkButton);
item["FieldName"].ForeColor = System.Drawing.Color.Red;
}
}
}
Thanks,
Shinu.
Reply
Jittu
18 posts
Member since:
Sep 2012
Link to this postPosted 26 Sep 2012
Hi Shinu,
It dint work out . When I click on the Add Link Button it does a post back and removes the Add link button
itself. It does not do a click event.Please help me on this.
Thanks,
Rajini
Reply
Shinu
17764 posts
Member since:
Mar 2007
Link to this postPosted 27 Sep 2012
Hi,
Please try the following code snippet as a work around for this issue.
C#:
public static ArrayList IndexArray = new ArrayList();
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs
e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
LinkButton MyLinkButton = new LinkButton();
12. 9/2/2015 Create LinkButton in RadGrid dynamically without Item Template tags in aspx page(Using code behind file) Grid UI for ASP.NET AJAX Forum
http://www.telerik.com/forums/createlinkbuttoninradgriddynamicallywithoutitemtemplatetagsinaspxpageusingcodebehindfile 12/15
LinkButton MyLinkButton = new LinkButton();
MyLinkButton.Text = "Add";
MyLinkButton.ID = "ID";
if (item["FieldName"].Text == " ")
{
IndexArray.Add(item.GetDataKeyValue("DataKeyName").ToString());
//storing the DataKeyValue of the row whose cell value is empty in an ArrayList
item["FieldName"].Controls.Add(MyLinkButton);
item["FieldName"].ForeColor =
System.Drawing.Color.Red;
}
}
}
void MyLinkButton_Click(object sender, EventArgs e)
{
//your code
}
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
LinkButton MyLinkButton = new LinkButton();
MyLinkButton.Text = "Add";
MyLinkButton.ID = "ID";
if (item["FieldName"].Text == " ")
{
item["FieldName"].Controls.Add(MyLinkButton);
item["FieldName"].ForeColor = System.Drawing.Color.Red;
}
foreach (string id in IndexArray)
{
string datakey = item.GetDataKeyValue("DataKeyName").ToString();
if (datakey == id)
{
item["FieldName"].Controls.Add(MyLinkButton);
item["FieldName"].ForeColor = System.Drawing.Color.Red;
MyLinkButton.Click += new EventHandler(MyLinkButton_Click);
//attaching click events for the cell with empty value
}
}
}
}
Thanks,
Shinu.
Reply
Jittu
18 posts
Member since:
Sep 2012
Link to this postPosted 27 Sep 2012
Hi Shinu,
Thanks for your reply .It works perfectly fine.
Thank you so much.
THanks,
Rajini
Reply
14. 9/2/2015 Create LinkButton in RadGrid dynamically without Item Template tags in aspx page(Using code behind file) Grid UI for ASP.NET AJAX Forum
http://www.telerik.com/forums/createlinkbuttoninradgriddynamicallywithoutitemtemplatetagsinaspxpageusingcodebehindfile 14/15
LinkButton link = new LinkButton();
link.Text = "Add";
link.ID = "LinkID";
if (item["PortfolioName"].Text == " ")
{
IndexArray.Add(item.GetDataKeyValue("ID").ToString());
item["PortfolioName"].Controls.Add(link);
item["PortfolioName"].ForeColor = System.Drawing.Color.Red;
}
}
}
protected void PortfolioLink_Click(object sender, EventArgs e)
{
}
void GridCommon_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
LinkButton link = new LinkButton();
link.Text = "Add";
link.ID = "LinkID";
if (item["PortfolioName"].Text == " ")
{
item["PortfolioName"].Controls.Add(link);
item["PortfolioName"].ForeColor = System.Drawing.Color.Red;
}
foreach (string id in IndexArray)
{
string datakey = item.GetDataKeyValue("ID").ToString();
if (datakey == id)
{
item["PortfolioName"].Controls.Add(link);
item["PortfolioName"].ForeColor =
System.Drawing.Color.Red;
link.Click += new EventHandler(PortfolioLink_Click);
}
}
}
}
Reply
Kostadin
1369 posts
ADMIN
Link to this postPosted 18 Oct 2013
Hello Arun,
I have already answered your support ticket, I would ask you to continue our conversation there. I am
providing the answer here if somebody else facing a similar issue as yours.
"I assume that is too late in page live cycle to attache an event handler of the button on one of those
events (ItemDataBound and ItemCreated). A suitable solution is to create the link button on item load
event."
Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the
ä