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

where i will write code???

3 Answers 69 Views
Grid
This is a migrated thread and some comments may be shown as answers.
shivesh
Top achievements
Rank 1
shivesh asked on 11 Nov 2010, 06:06 AM
Hi,
I am using a grid button that's code is given below tell in which plzce i will write code for that operation??
this button is inside grid it has not property like onclientclick like asp:button??
Thanks
 </telerik:GridTemplateColumn>
                    <telerik:GridButtonColumn ButtonType="PushButton" Text="Save" UniqueName="Save">
                    <FooterStyle Font-Bold="true" Font-Size="Medium" />
 </telerik:GridButtonColumn>

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 11 Nov 2010, 07:28 AM
Hello Shivesh,

There are two ways to write client side code for the GridButtonColumn. One is adding a CommandName to GridButtonColumn and then attach an 'OnCommad' client event to grid . Inside that event handler you can write necessary code for that button.

ASPX:
<telerik:RadGrid ID="RadGrid2" . . . .>
    <MasterTableView>
        <Columns>
           <telerik:GridButtonColumn ButtonType="PushButton" Text="Save" UniqueName="Save"
               CommandName="Save">
           </telerik:GridButtonColumn>
        </Columns>
    </MasterTableView>
    <ClientSettings>
        <ClientEvents OnCommand="OnCommand" />
    </ClientSettings>
</telerik:RadGrid>

Java Script:
<script type="text/javascript">
   function OnCommand(sender, args) {
     if (args.get_commandName() == "Save") {
          //your code
        }
    }
</script>

Another option is accessing the GridButtonColumn from code behind and attach an 'onclick' client event like below.

C#:
protected void RadGrid2_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           Button saveButton = (Button)item["Save"].Controls[0];
           saveButton.Attributes.Add("onclick", "save();");
       }
   }

Java Script:
<script type="text/javascript">
    function save() {
       //your code
    }
</script>

Thanks,
Princy.
0
shivesh
Top achievements
Rank 1
answered on 11 Nov 2010, 08:25 AM
hi Princy,
Thanks for help can u provide me sample code for  javascript save button code with radgrid where using data with dropdown textbox and label.
0
Princy
Top achievements
Rank 2
answered on 12 Nov 2010, 07:03 AM
Hello Shivesh,

I am not quite sure about your setup. I guess when clicking "Save" button, you want to access the controls in the same row. If so the following code snippet will be helpful.

ASPX:
<telerik:RadGrid ID="RadGrid2" runat="server" OnItemCreated="RadGrid2_ItemCreated">
   <MasterTableView>
      <Columns>
         <telerik:GridTemplateColumn>
           <ItemTemplate>
              <asp:Label ID="Label1" runat="server"></asp:Label>
           </ItemTemplate>
         </telerik:GridTemplateColumn>
         <telerik:GridButtonColumn ButtonType="PushButton" Text="Save" 
           UniqueName="Save">
         </telerik:GridButtonColumn>
       </Columns>
     </MasterTableView>
  </telerik:RadGrid>

C#:
protected void RadGrid2_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           Button saveButton = (Button)item["Save"].Controls[0];
           saveButton.Attributes.Add("onclick", "save('" + item.ItemIndex + "');");
        }
   }

Java Script:
<script type="text/javascript">
     function save(index) {
        var grid = $find("<%=RadGrid2.ClientID %>");
        var MasterTable = grid.get_masterTableView();
        var row = MasterTable.get_dataItems()[index];
        var label = row.findElement("Label1"); // accessing controls inside ItemTemplate
    }
</script>


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