This is a migrated thread and some comments may be shown as answers.

RadGrid OnCmmand event for dynamically created LinkButton

12 Answers 423 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sam
Top achievements
Rank 1
Sam asked on 08 Apr 2013, 05:03 PM
I have added LinkButton dynamically. Everything is coming properly like style and related attributes. But OnCommand For LinkButton not triggering and if click on link button which i have been added it disappears. From UI. And Event Listener not triggering properly.
Please Suggest me.
Here my code
protected void Aspx_RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
        {
             if (e.Item is GridDataItem)
               {
                 GridDataItem zitem = (GridDataItem)e.Item;
                HtmlGenericControl zobjSpanLabel = (HtmlGenericControl)e.Item.FindControl("span_StoryLabels_IceBox");
                  LinkButton zobjLableLinkButton = new LinkButton();
                        zobjLableLinkButton.Text ="Test";
                        zobjLableLinkButton.Command += new CommandEventHandler(GetThingsRelatedToEntity);
                        zobjLableLinkButton.CommandArgument = "1234";
                        zobjLableLinkButton.CommandName = "";                        
                        zobjLableLinkButton.Attributes.Add("style", "text-decoration:none;color: #006633;cursor: pointer;font-family: arial,sans-serif;font-size: 12px;");
                        zobjLableLinkButton.CssClass = "stroylabels";
                        zobjSpanLabel .Controls.Add(zobjLableLinkButton);
}

protected void GetThingsRelatedToEntity(object sender, CommandEventArgs e)
{
//My Code Here
}

Thanks in advance.

12 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 08 Apr 2013, 06:23 PM
Hello,

Please try with the below code snippet.

protected void Aspx_RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem zitem = (GridDataItem)e.Item;
                HtmlGenericControl zobjSpanLabel = (HtmlGenericControl)e.Item.FindControl("span_StoryLabels_IceBox");
                LinkButton zobjLableLinkButton = new LinkButton();
                zobjLableLinkButton.Text = "Test";
                zobjLableLinkButton.CommandName = "MyCommand";
                zobjLableLinkButton.CommandArgument = "1234";

                zobjLableLinkButton.Attributes.Add("style", "text-decoration:none;color: #006633;cursor: pointer;font-family: arial,sans-serif;font-size: 12px;");
                zobjLableLinkButton.CssClass = "stroylabels";
                zobjSpanLabel.Controls.Add(zobjLableLinkButton);
            }
        }
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "MyCommand")
    {
        // perform your operation here
    }
}

Note1 : Please add Linkbutton in ItemCreated event in place of itemDataBound event.
OR
Note2 : Please add Click event in-place of command event in LinkButton:

Thanks,
Jayesh Goyani
0
Shinu
Top achievements
Rank 2
answered on 09 Apr 2013, 04:38 AM
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 Column,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 data-binding 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. Hope this helps.

Thanks,
Shinu
0
Sam
Top achievements
Rank 1
answered on 09 Apr 2013, 02:29 PM
Thanks For Reply.

      I have been tried both methods. But i didn't get anything. Please give me a example for this scenario if you have any.

Thanks in advance.

0
Accepted
Shinu
Top achievements
Rank 2
answered on 10 Apr 2013, 08:39 AM
Hi,

Try the following code.
c#;
protected void RadGrid2_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = (GridDataItem)e.Item;
            HtmlGenericControl zobjSpanLabel = (HtmlGenericControl)e.Item.FindControl("span_StoryLabels_IceBox");
            LinkButton hlControl = new LinkButton();
            hlControl.Text = "test";
            hlControl.CommandName = "test";
            hlControl.CommandArgument = "test";
            hlControl.Command += new CommandEventHandler(hlControl_Command);
             zobjSpanLabel.Controls.Add(hlControl);
        }
}
 void hlControl_Command(object sender, CommandEventArgs e)
 {
 }
protected void RadGrid2_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = (GridDataItem)e.Item;
            HtmlGenericControl zobjSpanLabel = (HtmlGenericControl)e.Item.FindControl("span_StoryLabels_IceBox");
            LinkButton hlControl = new LinkButton();
            hlControl.Text = "test";
            zobjSpanLabel .Controls.Add(hlControl);
        }
}

Thanks,
Shinu
0
Sam
Top achievements
Rank 1
answered on 11 Apr 2013, 07:48 AM
Thanks For Reply.

I removed the adding dynamic label in ItemDataBound. I moved this one to ItemCreated. Now it is working Fine.

And For Further Operations I used DataKeyValue.
0
Sam
Top achievements
Rank 1
answered on 11 Apr 2013, 07:59 AM
I have another Problem.
For LinkButton i added OnCommandEvent

In my Page I have 5 RadGrids. I'm displaying 4 grids on Page Load. And 5th RadGrid td i want to make it visible in LinkButton 's OnCommandEvent Which i have been added dynamically.
like
--ASPX Page
<table>
     <tr>
        <td>RadGrid1</td>
       
<td>RadGrid2</td>
       
<td>RadGrid3</td>
       
<td>RadGrid4</td>
       
<td runat="server" id="td_RadGrid5" visible="false">RadGrid5</td>
   
</tr>
</table>


--C# in [OnCommandEvent --

void hlControl_Command(object sender, CommandEventArgs e)
 {
       if(e.CommandArgument != null)
        {
           td_RadGrid5.Visible=true;
           RadGrid5.DataSource= //Some value;
           RadGrid5.Rebind();
        }
 }

I have NeedDataSource and all related Event handlers in my code and i have implementd.

My problem is td is not enabling in RadGrid's Ajax. I have added all radgrids to Ajax Settings.

Please Suggest me any solution.
0
Shinu
Top achievements
Rank 2
answered on 11 Apr 2013, 08:09 AM
Hi,

If you have already populated the grid inside the td, setting the visibility of td alone will render the RadGrid.
<td runat="server" id="td_RadGrid5" visible="false">
                    RadGrid5
          <telerik:RadGrid AllowFilteringByColumn="true" ID="RadGrid5" AutoGenerateColumns="true"
                        runat="server"  onneeddatasource="RadGrid5_NeedDataSource">
                    </telerik:RadGrid>
</td>

c#:
void hlControl_Command(object sender, CommandEventArgs e)
 {
       if(e.CommandArgument != null)
        {
           td_RadGrid5.Visible=true;
        }
 }

Thanks,
Shinu
0
Sam
Top achievements
Rank 1
answered on 11 Apr 2013, 11:41 AM
I performed in the same approach but i didn't get anything. Please do the needful.

Thanks in Advance.
0
Sam
Top achievements
Rank 1
answered on 12 Apr 2013, 12:46 PM
Please provide me any demo or sample application with RadGrid Show and Hide in RadAjax as mentioned above. Please i need to do it as early as possible. Please help me.

Thanks in advance.
0
Eyup
Telerik team
answered on 16 Apr 2013, 08:49 AM
Hi Samuel,

Please try to toggle the grid's visibility instead. I have prepared a sample RadGrid web site to demonstrate how you can achieve the requested functionality. Can you check out the attached application and verify that it works as expected on your end, too?

Additionally, you can use a single RadAjaxPanel to wrap the table:
http://www.telerik.com/help/aspnet-ajax/ajax-ajaxpanel.html

All the best,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Sam
Top achievements
Rank 1
answered on 16 Apr 2013, 03:59 PM
Thanks For Reply.

          It worked for me. But in my page i have 6-10 rad grids which are horizontally aligned(This grids count will be increased in future). If i place radgrid visible=false still td will be in page it self it will show the empty spaces if i performed visible=false on in between radgrids.

Thanks in advance.

0
Eyup
Telerik team
answered on 19 Apr 2013, 06:43 AM
Hello Samuel,

I am glad that the provided sample was helpful.

You can wrap the grids in a single asp:Panel and toggle only the panel's visibility.

Hope this helps.

Greetings,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Sam
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Sam
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or