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

Access edit control in GridTemplateColumn

4 Answers 193 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Helen
Top achievements
Rank 1
Helen asked on 13 Nov 2013, 04:33 PM
Hi,

I'm very new to Telerik controls and I've been reading the documentation and some of the examples but I still couldn't figure out this problem.
I'm trying to validate the address after the user entered into the textboxes and click on an image button. I have no trouble getting the values from GridBoundColumns setup for my city, state, zip.  However, I can't access my address textbox in the GridTemplateColumn.  Please help~

Thank you so much,

Helen

Here's is my aspx code:

 

<telerik:GridTemplateColumn DataField="bldgstreet1" FilterControlAltText="Filter address column" HeaderText="Address" SortExpression="address" UniqueName="addressTemplate">

 

 

<EditItemTemplate>

 

<asp:TextBox ID="address1" runat="server" Text='<%# Bind("bldgstreet1") %>'></asp:TextBox><br />

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

</EditItemTemplate>

<InsertItemTemplate>

<asp:TextBox ID="address1" runat="server" Text='<%# Bind("bldgstreet1") %>'></asp:TextBox><br />

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

</InsertItemTemplate>

<ItemTemplate>

<asp:Label ID="addressLabel" runat="server" Text='<%# Eval("bldgstreet1") + " " + Eval("bldgstreet2") %>'></asp:Label>

</ItemTemplate>

</telerik:GridTemplateColumn>

<telerik:GridBoundColumn DataField="bldgcity" FilterControlAltText="Filter bldgcity column" HeaderText="City" SortExpression="bldgcity" UniqueName="bldgcity">

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="bldgstate" FilterControlAltText="Filter bldgstate column" HeaderText="State" SortExpression="bldgstate" UniqueName="bldgstate">

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="bldgzip" FilterControlAltText="Filter bldgzip column" HeaderText="Zip" SortExpression="bldgzip" UniqueName="bldgzip">

</telerik:GridBoundColumn>


I tried doing this in my click event:

protected void CertifyAddressImageButton_Click(object sender, ImageClickEventArgs e)
{
 
 
    foreach (GridEditableItem item in gdvBuilding.EditItems)
    {
 
 
        string bldgname = ((TableCell)item["bldgname"]).Text.Trim();
        Debug.WriteLine("bldgname=" + bldgname);
        string zip = ((TableCell)item["bldgzip"]).Text.Trim();
        Debug.WriteLine("zip=" + zip);
 
        TableCell cell = item["addressTemplate"];
        string address1 = (cell.Controls[0] as TextBox).Text;
 
        //    This also doesn't work
 
        //    string address1 = (item.FindControl("address1") as TextBox).Text;
        //    Debug.WriteLine("address=" + address1);
 
        Debug.WriteLine("address=" + address1);
    }
   
 
    //CertifyAddress();
}

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 14 Nov 2013, 04:50 AM
Hi Helen,

Please try the following code snippet to access a GridTemplateColumn in Edit Mode.

C#:
protected void CertifyAddressImageButton_Click(object sender, ImageClickEventArgs e)
{
    foreach (GridDataItem item in gdvBuilding.EditItems)
    {
        GridEditableItem edititem = (GridEditableItem)item.EditFormItem;
        string id = (edititem["bldgcity"].Controls[0] as TextBox).Text; //Access BoundColumn
        //Access TemplateColumn TextBox in EditMode
        string address1 = (edititem.FindControl("address1") as TextBox).Text;
        string address2 = (edititem.FindControl("address2") as TextBox).Text;
    }
}

Thanks,
Princy
0
Helen
Top achievements
Rank 1
answered on 14 Nov 2013, 01:03 PM
Hi Princy,

Your code will work if I place my image button inside one of the GridTemplateColumn which includes another field like this :
<telerik:GridTemplateColumn DataField="bldgstreet1" FilterControlAltText="Filter address column" HeaderText="Address" SortExpression="address" UniqueName="addressTemplate">
    <EditItemTemplate>
        <asp:TextBox ID="address1" runat="server" Text='<%# Bind("bldgstreet1") %>'></asp:TextBox><br />
        <asp:TextBox ID="address2" runat="server" Text='<%# Bind("bldgstreet2") %>'></asp:TextBox>
        <asp:ImageButton ID="CertifyAddressImageButton" runat="server" ImageUrl="~/images/certificate32.png" OnClick="CertifyAddressImageButton_Click"/>
    </EditItemTemplate>
    <InsertItemTemplate>
        <asp:TextBox ID="address1" runat="server" Text='<%# Bind("bldgstreet1") %>'></asp:TextBox><br />
        <asp:TextBox ID="address2" runat="server" Text='<%# Bind("bldgstreet2") %>'></asp:TextBox>
        <asp:ImageButton ID="CertifyAddressImageButton" runat="server" ImageUrl="~/images/certificate32.png" OnClick="CertifyAddressImageButton_Click"/>
    </InsertItemTemplate>
    <ItemTemplate>
        <asp:Label ID="addressLabel" runat="server" Text='<%# Eval("bldgstreet1") + " " + Eval("bldgstreet2") %>'></asp:Label>
        <asp:ImageButton ID="CertifyAddressImageButton" runat="server" ImageUrl="~/images/certificate32.png" OnClick="CertifyAddressImageButton_Click"/>
    </ItemTemplate>
</telerik:GridTemplateColumn>

Due to the layout of the page, I like to place the image button by itself in it's own GridTemplateColumn like this so the button will show only when the user tries to edit or insert a record :
<telerik:GridTemplateColumn HeaderText="Certify Address" UniqueName="CertifyAddress" Display="False" Visible="False">
    <EditItemTemplate>
        <asp:ImageButton ID="CertifyAddressImageButton" runat="server" ImageUrl="~/images/certificate32.png" OnClick="CertifyAddressImageButton_Click"/>
    </EditItemTemplate>
    <InsertItemTemplate>
        <asp:ImageButton ID="CertifyAddressImageButton" ImageUrl="~/images/certificate32.png" OnClick="CertifyAddressImageButton_Click" runat="server" />
        <br />
        <asp:Label ID="Label3" runat="server" Text="placeholder"></asp:Label>
    </InsertItemTemplate>
</telerik:GridTemplateColumn>

I will get an error "Object reference not set to an instance of an object." on this line :
city = (edititem["bldgcity"].Controls[0] as TextBox).Text;

I got it partially working with this piece of code which I don't quite understand why. This piece of code only works when during "Edit" mode, not "Insert" mode.  In the insert mode, I get the same object reference error when I'm trying to get the value from the BoundColumn.  What did I do wrong....?
GridEditableItem edititem = (GridEditableItem)(sender as ImageButton).NamingContainer;
 
address1 = ((TextBox)(edititem["addressTemplate"].FindControl("address1"))).Text;
Debug.WriteLine("address=" + address1);
address2 = ((TextBox)(edititem["addressTemplate"].FindControl("address2"))).Text;
Debug.WriteLine("address=" + address2);
 
//int index = edititem.ItemIndex;
 
foreach (GridEditableItem item in gdvBuilding.EditItems)
{
    int ind = item.ItemIndex;
    city = ((TableCell)item["bldgcity"]).Text.Trim();
    //Debug.WriteLine("city=" + city);
    state = ((TableCell)item["bldgstate"]).Text.Trim();
    //Debug.WriteLine("state=" + state);
    zip = ((TableCell)item["bldgzip"]).Text.Trim();
    //Debug.WriteLine("zip=" + zip);
}

Any help is truly appreciated~~~~~

Helen
0
Princy
Top achievements
Rank 2
answered on 15 Nov 2013, 04:43 AM
Hi Helen,

Please try the following code snippet to access the controls in Edit or Insert mode.

ASPX:
<telerik:GridTemplateColumn HeaderText="Certify Address" UniqueName="CertifyAddress" Display="False" >
    <EditItemTemplate>
    <asp:ImageButton ID="CertifyAddressImageButton"  runat="server" ImageUrl="~/images/certificate32.png"
                OnClick="CertifyAddressImageButton_Click" />
    </EditItemTemplate>
    <InsertItemTemplate>
   <asp:ImageButton ID="CertifyAddressImageButton" runat="server" ImageUrl="~/images/certificate32.png"
        OnClick="CertifyAddressImageButton_Click" />
     <br />
   <asp:Label ID="Label3" runat="server" Text="placeholder"></asp:Label>
    </InsertItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void CertifyAddressImageButton_Click(object sender, ImageClickEventArgs e)
{
    ImageButton imgbtn = (ImageButton)sender;
    GridEditableItem editinsertitem = (GridEditableItem)imgbtn.NamingContainer;
    TextBox txt1 = (TextBox)editinsertitem ["bldgcity"].Controls[0];//Access BoundColumn
    string city= txt1.Text;
    TextBox txt2 = (TextBox)editinsertitem .FindControl("address1");//Access TemplateColumn
    string address1 = txt2.Text; 
}


Another approach is that you can add the CommandName property of the Image button, hence you can access the Controls via ItemCommand event of the RadGrid.
Please try the following code snippet.

ASPX:
<telerik:GridTemplateColumn HeaderText="Certify Address" UniqueName="CertifyAddress" Display="False" >
    <EditItemTemplate>
        <asp:ImageButton ID="CertifyAddressEditButton" CommandName="EditCertify" runat="server" ImageUrl="~/images/certificate32.png" />
    </EditItemTemplate>
    <InsertItemTemplate>
        <asp:ImageButton ID="CertifyAddressInsertButton" CommandName="InsertCertify" ImageUrl="~/images/certificate32.png" runat="server" />
        <br />
        <asp:Label ID="Label3" runat="server" Text="placeholder"></asp:Label>
    </InsertItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "EditCertify")
    {
        GridEditableItem edititem = (GridEditableItem)e.Item;
        //Access value of BoundColumn        
        TextBox txt1=(TextBox)((Telerik.Web.UI.GridDataItem)edititem).EditFormItem["bldgcity"].Controls[0];
        string city= txt1.Text;
         //Access value of TemplateColumn
        TextBox txt2 = (TextBox)((Telerik.Web.UI.GridDataItem)edititem).EditFormItem.FindControl("address1");
        string address1 = txt2.Text;
    }
    if (e.CommandName == "InsertCertify")
    {
        GridEditableItem insert = (GridEditableItem)e.Item;
         //Access value of BoundColumn
        TextBox txt1 = (TextBox)insert["bldgcity"].Controls[0];
        string city= txt1.Text;
        //Access value of TemplateColumn
        TextBox txt2 = (TextBox)insert.FindControl("address1");
        string address1 = txt2.Text;          
    }
}

Thanks,
Princy
0
Helen
Top achievements
Rank 1
answered on 19 Nov 2013, 12:34 PM
Hi Princy,

That'll work~ thank you for your help~

Regards,

Helen
Tags
Grid
Asked by
Helen
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Helen
Top achievements
Rank 1
Share this question
or