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

how to access template columns controls

7 Answers 189 Views
Grid
This is a migrated thread and some comments may be shown as answers.
anand
Top achievements
Rank 1
anand asked on 18 Sep 2008, 02:20 PM
hi,

 i am editing records, i am using manual method for that, under 

RadGrid2_UpdateCommand i am trying to access template columns controls  i am getting null value

here is my code

<telerik:GridTemplateColumn HeaderText="Mobil" UniqueName="TempMobil" Visible="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox2"

ErrorMessage="RangeValidator" MaximumValue="999999999" MinimumValue="00000000" Text="*"></asp:RangeValidator>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label3" runat="server" Text='<%# Bind("Mobil") %>'></asp:Label></ItemTemplate></telerik:GridTemplateColumn> 


protected void RadGrid2_UpdateCommand(object source, GridCommandEventArgs e)

{

string text = ((TextBox)e.Item.FindControl("TextBox2")).Text;

}

7 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 18 Sep 2008, 02:52 PM
Hello Anand,

Try this approach:
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) 
    TextBox textBox = (e.Item as GridEditFormItem)["CategoryID"].Controls[0] as TextBox; 

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
anand
Top achievements
Rank 1
answered on 18 Sep 2008, 02:58 PM
hi, it not working , i added it gives me text box value as null
0
Bruno
Top achievements
Rank 2
answered on 18 Sep 2008, 09:06 PM
Anand,

place a breakpoint over the line below and try to find out the desired control in the collection.

quote from Daniel's code snippet
TextBox textBox = (e.Item as GridEditFormItem)["CategoryID"].Controls[0] as TextBox;  

Also make sure that you changed CategoryID to your column's actual name - TempMobil for example.

There are so many forum posts concerning this matter - you may just try the Search function as an alternative.
0
Princy
Top achievements
Rank 2
answered on 19 Sep 2008, 05:06 AM
Hello Anand,

Try out the following code to access a control in the EditItemTemplate of  a TemplateColumn.
aspx:
<telerik:GridTemplateColumn UniqueName="Template1">                      
                     <EditItemTemplate> 
                             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
                     </EditItemTemplate> 
</telerik:GridTemplateColumn>   

cs:
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item.IsInEditMode) 
        { 
            TextBox txtBox = (TextBox)(e.Item as GridEditableItem)["Template1"].FindControl("TextBox1"); 
        } 
    } 

Princy.
0
anand
Top achievements
Rank 1
answered on 19 Sep 2008, 07:50 AM
not working, still i am getting  value from text box is empty
0
Shinu
Top achievements
Rank 2
answered on 19 Sep 2008, 09:13 AM
Hi Anand,

Try the following code snippet in the UpdateCommand event and see if is working.

CS:
  protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) 
    { 
        GridEditableItem edititem = (GridEditableItem)e.Item; 
        TextBox txtbx = (TextBox)edititem["TempMobil"].FindControl("TextBox2"); 
    } 


Thanks
Shinu.
0
anand
Top achievements
Rank 1
answered on 24 Sep 2008, 09:13 AM

<

head runat="server">

<title>Reports</title>

<telerik:radcodeblock id="RadCodeBlock1" runat="server">

<script type="text/javascript">

var activeTable;

var colIndex;

function ColumnContextMenu(sender, eventArgs)

{

activeTable = sender.get_masterTableView();

colIndex = GetRealCellIndex(sender.get_masterTableView(), eventArgs.get_gridColumn().get_element());

}

function GetRealCellIndex(tableView, cell)

{

for(var i = 0; i < tableView.get_columns().length; i++)

{

if(tableView.get_columns()[i].get_element()== cell)

{

return i;

}

}

}

function ClientItemClicked(sender, eventArgs)

{

switch(eventArgs.get_item().get_text())

{

case "ID":

activeTable.showColumn(0);

break;

/*case "IP Address":

activeTable.showColumn(1);

break;*/

case "Dato":

activeTable.showColumn(1);

break;

/*case "Tid":

activeTable.showColumn(3);

break;*/

case "Firma":

activeTable.showColumn(2);

break;

case "Medlem":

activeTable.showColumn(3);

break;

case "Navn":

activeTable.showColumn(4);

break;

case "E-post":

activeTable.showColumn(5);

break;

case "Mobil":

activeTable.showColumn(6);

break;

case "Kveldsarrangement":

activeTable.showColumn(7);

break;

case "Adresse":

activeTable.showColumn(8);

break;

case "Postnr":

activeTable.showColumn(9);

break;

case "Sted":

activeTable.showColumn(10);

break;

case "Hide column":

activeTable.hideColumn(colIndex);

break;

default:

break;

}

}

function RestoreColumns()

{

var grid = $find("<%=RadGrid2.ClientID %>");

for(i = 0; i < grid.get_masterTableView().get_columns().length; i++)

{

grid.get_masterTableView().showColumn(i);

}

}

</script>

</telerik:radcodeblock>

</

head>

<

body>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server">

</asp:ScriptManager>

<div style="font-family: Arial; text-align: center">

<h1>

Admin Report

</h1>

</div>

<br />

<br />

<div>

<div style="float: left">

<asp:LinkButton ID="LinkButton1" runat="server" Text="Export to Excel" Font-Names="Arial"

Font-Size="Small" onclick="LinkButton1_Click"></asp:LinkButton>

</div>

<div style="float: left; width: 20px;">

</div>

<div style="float: left">

<asp:LinkButton ID="LinkButton2" runat="server" Text="Export To PDF" Font-Names="Arial"

Font-Size="Small" onclick="LinkButton2_Click"></asp:LinkButton>

</div>

</div>

<br />

<br />

<div>

<telerik:radgrid id="RadGrid2" allowfilteringbycolumn="True" allowpaging="True" allowsorting="True"

runat="server" gridlines="None" pagesize="100" enablelinqexpressions="False"

onitemdatabound="RadGrid2_ItemDataBound" allowmultirowedit="True"

onitemcreated="RadGrid2_ItemCreated" onupdatecommand="RadGrid2_UpdateCommand"

onitemcommand="RadGrid2_ItemCommand" onprerender="RadGrid2_PreRender" oneditcommand="RadGrid2_EditCommand1"

skin="WebBlue" onitemdeleted="RadGrid2_ItemDeleted"

autogeneratedeletecolumn="True" ondeletecommand="RadGrid2_DeleteCommand"

AutoGenerateEditColumn="True">

<ExportSettings ExportOnlyData="True" IgnorePaging="True" OpenInNewWindow="True">

<Pdf PageHeight="" PageWidth="1300px" Title="Report" PaperSize="A4" PageBottomMargin="10px"

PageLeftMargin="10px" PageRightMargin="10px" PageTopMargin="10px" PageTitle="Report" />

</ExportSettings>

<PagerStyle Mode="NextPrevAndNumeric" />

<

MasterTableView autogeneratecolumns="False" editmode="PopUp" datakeynames="Company_ID">

<

RowIndicatorColumn>

<

HeaderStyle Width="20px"></HeaderStyle>

</

RowIndicatorColumn>

<

ExpandCollapseColumn>

<

HeaderStyle Width="20px"></HeaderStyle>

</

ExpandCollapseColumn>

<Columns>

<telerik:GridTemplateColumn UniqueName="Template1" HeaderText="ID">

<HeaderStyle HorizontalAlign="Center" />

<ItemTemplate>

<%

# RadGrid2.Items.Count + 1%>

</ItemTemplate>

</telerik:GridTemplateColumn>

<telerik:GridBoundColumn DataField="Company_ID" HeaderText="ID"

UniqueName="CompanyID" ReadOnly="True" visible="False">

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="IP_Address" HeaderText="IP Address"

UniqueName="IPAddress" ReadOnly="True" visible="False">

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="Date_Of_Registration" HeaderText="Dato"

UniqueName="DateOfRegistration" DataFormatString="{0:dd. MM. yyyy} "

DataType="System.DateTime" ReadOnly="True">

<HeaderStyle HorizontalAlign="Center" />

<ItemStyle HorizontalAlign ="Center" />

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="Time_Of_Registration" HeaderText="Tid"

UniqueName="TimeOfRegistration" DataFormatString="{0:HH:mm:ss tt} " ReadOnly="True" visible="False">

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="Company_Name" HeaderText="Firma"

UniqueName="CompanyName">

<HeaderStyle HorizontalAlign="Center" />

</telerik:GridBoundColumn>

<telerik:GridBoundColumn HeaderText="Medlem" UniqueName="Medlem" ReadOnly="True" >

<HeaderStyle HorizontalAlign="Center" />

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="Member_Inma" HeaderText="MemberOfInma"

UniqueName="MemberOfInma" Visible="False" ReadOnly="True">

<HeaderStyle HorizontalAlign="Center" />

</telerik:GridBoundColumn>

<%

--<telerik:GridTemplateColumn HeaderText="MemberOfInma" UniqueName="TempMemberOfInma" Visible="False">

<HeaderStyle HorizontalAlign="Center" />

<EditItemTemplate>

<asp:RadioButton ID="RadioButton1" runat="server" GroupName="gr1" Checked='<%# DataBinder.Eval(Container, "DataItem.Member_Inma").ToString()=="1"? true : false %>' />

</EditItemTemplate>

<ItemTemplate>

</ItemTemplate>

</telerik:GridTemplateColumn>--

%>

<telerik:GridBoundColumn DataField="Member_Anfo" HeaderText="Member Anfo"

UniqueName="MemberOfAnfo" Visible="False" ReadOnly="True">

<HeaderStyle HorizontalAlign="Center" />

</telerik:GridBoundColumn>

<telerik:GridTemplateColumn HeaderText="MemberOfAnfo" UniqueName="TempMemberOfAnfo" Visible="False">

<HeaderStyle HorizontalAlign="Center" />

<EditItemTemplate>

<asp:RadioButton ID="RadioButton2" runat="server" GroupName="gr1" Checked='<%# DataBinder.Eval(Container, "DataItem.Member_Anfo").ToString()=="1"? true : false %>' />

</EditItemTemplate>

<ItemTemplate>

</ItemTemplate>

</telerik:GridTemplateColumn>

<telerik:GridBoundColumn DataField="Not_a_Member" HeaderText="No Member"

UniqueName="NotMember" Visible="False" ReadOnly="True">

</telerik:GridBoundColumn>

<telerik:GridTemplateColumn HeaderText="MemberOfAnfo" UniqueName="TempNotMember" Visible="False">

<HeaderStyle HorizontalAlign="Center" />

<EditItemTemplate>

<asp:RadioButton ID="RadioButton3" runat="server" GroupName="gr1" Checked='<%# DataBinder.Eval(Container, "DataItem.Not_a_Member").ToString()=="1"? true : false %>' />

</EditItemTemplate>

<ItemTemplate>

</ItemTemplate>

</telerik:GridTemplateColumn>

<telerik:GridBoundColumn DataField="Name" HeaderText="Navn"

UniqueName="Name">

<HeaderStyle HorizontalAlign="Center" />

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="Designation" HeaderText="Stilling"

UniqueName="Designation">

<HeaderStyle HorizontalAlign="Center" />

</telerik:GridBoundColumn>

<%

-- <telerik:GridBoundColumn DataField="Email" HeaderText="E-post"

UniqueName="Email">

</telerik:GridBoundColumn>--

%>

<telerik:GridTemplateColumn HeaderText="Email" UniqueName="TempEmail" Visible="True">

<HeaderStyle HorizontalAlign="Center" />

<EditItemTemplate>

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label2" runat="server" Text='<%# Bind("Email") %>'></asp:Label>

</ItemTemplate>

</telerik:GridTemplateColumn>

<%

--<telerik:GridBoundColumn DataField="Mobil" HeaderText="Mobil"

UniqueName="Mobil">

</telerik:GridBoundColumn>--

%>

<telerik:GridTemplateColumn HeaderText="Mobil" UniqueName="TempMobil" Visible="False">

<HeaderStyle HorizontalAlign="Center" />

<EditItemTemplate>

<asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>

<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox2"

ErrorMessage="RangeValidator" MaximumValue="999999999" MinimumValue="00000000" Text="*"></asp:RangeValidator>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label3" runat="server" Text='<%# Bind("Mobil") %>'></asp:Label>

</ItemTemplate>

</telerik:GridTemplateColumn>

<telerik:GridBoundColumn DataField="Evening_Arrangement" HeaderText="KA"

UniqueName="EveningArrangement" ReadOnly="True">

<HeaderStyle HorizontalAlign="Center" />

</telerik:GridBoundColumn>

<%

-- <telerik:GridTemplateColumn HeaderText="KA" UniqueName="TempKveldsarrangement" visible ="False">

<HeaderStyle HorizontalAlign="Center" />

<EditItemTemplate>

<asp:RadioButton ID="RadioButton4" runat="server" Checked='<%# DataBinder.Eval(Container, "DataItem.Evening_Arrangement").ToString()=="1"? true : false %>' />

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label4" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Evening_Arrangement").ToString()=="1"? "ja" : "nei" %>'></asp:Label>

</ItemTemplate>

</telerik:GridTemplateColumn>

--

%>

<telerik:GridBoundColumn DataField="Invocing_Address" HeaderText="Adresse"

UniqueName="InvocingAddress">

<HeaderStyle HorizontalAlign="Center" />

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="Invoicing_Post_Nr" HeaderText="Postnr"

UniqueName="InvoicingPostNr">

<HeaderStyle HorizontalAlign="Center" />

<ItemStyle HorizontalAlign="Right" />

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="Invoicing_Posted" HeaderText="Sted"

UniqueName="InvoicingPosted">

<HeaderStyle HorizontalAlign="Center" />

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="Digital_Marketing_Info_Mail"

HeaderText="Nyhetsmail" UniqueName="DigitalMarketingInfoMail" ReadOnly="True">

<HeaderStyle HorizontalAlign="Center" />

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="Similar_Arrangements_SMS" HeaderText="SMS"

UniqueName="SimilarArrangementsSMS" ReadOnly="True">

<HeaderStyle HorizontalAlign="Center" />

</telerik:GridBoundColumn>

</Columns>

</

MasterTableView>

<

ClientSettings AllowColumnHide="true">

<ClientEvents OnColumnContextMenu="ColumnContextMenu" />

</ClientSettings>

<

FilterMenu Skin="WebBlue" EnableTheming="True">

<

CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>

</

FilterMenu>

</telerik:radgrid>

</div>

<div>

<telerik:radcontextmenu id="RadMenu1" runat="server" skin="Outlook" onclientitemclicked="ClientItemClicked">

<Items>

<telerik:RadMenuItem Text="Show column">

<Items>

<telerik:RadMenuItem Text="ID" />

<%

--<telerik:RadMenuItem Text="IP Address" />--%>

<telerik:RadMenuItem Text="Dato" />

<%

-- <telerik:RadMenuItem Text="Tid" />--%>

<telerik:RadMenuItem Text="Firma" />

<telerik:RadMenuItem Text="Medlem" />

<telerik:RadMenuItem Text="Navn" />

<telerik:RadMenuItem Text="E-post" />

<telerik:RadMenuItem Text="Mobil" />

<telerik:RadMenuItem Text="Kveldsarrangement" />

<telerik:RadMenuItem Text="Adresse" />

<telerik:RadMenuItem Text="Postnr" />

<telerik:RadMenuItem Text="Sted" />

</Items>

</telerik:RadMenuItem>

<telerik:RadMenuItem Text="Hide column" />

</Items>

<Targets>

<telerik:ContextMenuTagNameTarget TagName="thead" />

</Targets>

</telerik:radcontextmenu>

</div>

<div>

<asp:Label ID="Label1" runat="server" />

</div>

</form>

</

body>

hi, this is my gride which contain  template columns, i  want to make editable gride which, but i am not able to access values after changing, i am getting only old values, and some times not getting values. after editing i want to update that vlues in database

Tags
Grid
Asked by
anand
Top achievements
Rank 1
Answers by
Daniel
Telerik team
anand
Top achievements
Rank 1
Bruno
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Share this question
or