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

[Solved] Access grid values on ItemCreated Event

4 Answers 830 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kerry
Top achievements
Rank 1
Kerry asked on 28 Jul 2009, 04:00 AM
Hello, I am building the  onClick attribute of a HyperLink column within a RadGrid on the ItemCreated event, such as 

editLink.Attributes[

"onclick"] = "return ShowAttachForm();";

This calls a Javascript function to open a RadWindow.  I now need to pass parameters to the javascript function but I am having problems accessing the value of the other DataBoundGrid columns...it seems like the columns do not yet have a value.  How can I access the value of a column on the ItemCreated event of a RadGrid so I can do something like

 


editLink.Attributes[

"onclick"] = String.Format("return ShowAttachForm('{0}','{1}');", dataItem["JobNo"].Text, dataItem["ClientNo"].Text);

 



Thanks Kerry

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 Jul 2009, 04:51 AM
Hello Kerry,

 Item Created event of the grid is fired before the item is data-bound. Thus no data will be available in the cells' text or input controls. So inorder to access a cell value, you can use the ItemDataBound event:
c#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
         { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
            HyperLink editLink = (HyperLink)dataItem["HyperLInkColumnUniqueName"].Controls[0];  
            editLink.Attributes["onclick"] = String.Format("return ShowAttachForm('{0}','{1}');", dataItem["JobNo"].Text, dataItem["ClientNo"].Text); 
         } 
    } 

Thanks
Princy.
0
Kerry
Top achievements
Rank 1
answered on 28 Jul 2009, 05:13 AM
Hi , thanks for the quick response, however I think I need to test what control I am trying to set the onclick attribute for, the code you gave me above causes an error trying to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.HyperLink.

Kerry
0
Kerry
Top achievements
Rank 1
answered on 05 Aug 2009, 07:34 PM
THanks for the reponse but the code above causes an error trying to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.HyperLink. 

Thanks
0
Princy
Top achievements
Rank 2
answered on 06 Aug 2009, 07:23 AM
Hello Kerry,

According to your initial post you wanted to add an onclick attribute to a HyperLinkColumn, thus the above code could be used for such a case. But since you are getting a cast error, it would be better if you could paste your aspx markup code here and also specify to which control do you want to add an onclick attribute.

Thanks
Princy.
Tags
Grid
Asked by
Kerry
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Kerry
Top achievements
Rank 1
Share this question
or