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

Find Control - RadGrid edit template text box

7 Answers 714 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Ben asked on 23 Nov 2010, 05:38 PM
Hi Guys,
Getting an error when I try to get control of radgrid edit template text box in code behind. I want to make edit template textbox  "visible = false"  when insert click(Add new record). Anybody can help me?

protected void ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                (this.Page.FindControl("txtOpcode") as RadTextBox).Visible = false;
                //(RadGrid1.MasterTableView.FindControl("txtOpcode") as RadTextBox).Visible = false;                
            }

Error: Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.

7 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 24 Nov 2010, 06:04 AM
Hello Ben,
You can find the TextBox in ItemDataBound Event.Try the following code snippet. 

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
   {
       if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
       {
           GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;
           TextBox txt = (TextBox)item.FindControl("TextBox1");
           txt.Visible = false;
 
       }     
   }

Please go through the following documentation for more information .
http://www.telerik.com/help/aspnet-ajax/grddistinguisheditinsertmodeonitemcreateditemdatabound.html

Thanks,
Princy.
0
Ben
Top achievements
Rank 1
answered on 24 Nov 2010, 04:46 PM
Thank you Princy
Its working now
I have some other issues related to this post. I posted it in my other thread. We can communicate there. Please answer for that thread.
Thank you again
Ben
0
Mark
Top achievements
Rank 1
answered on 28 Feb 2011, 05:52 PM
Hi,

I'm receiving the exact same error that Ben received (Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.) but my situation is slightly different.

I have a custom edit form, and within the edit form I have RadAsyncUpload and RadBinaryImage conrols. Where I'm receiving the error is when I try to upload a new image. When I debug, the line of code that's creating the error is where I'm trying to find the RadBinaryImage control in my edit form. See code below:

RadGrid:

<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" AllowPaging="True" AllowFilteringByColumn="true"
        CssClass="RadGrid" AllowSorting="True" AutoGenerateColumns="False" ShowStatusBar="true"
        OnNeedDataSource="RadGrid1_NeedDataSource" Skin="Office2007">
        <MasterTableView Width="100%" DataKeyNames="AssetID" EditMode="PopUp">
            <Columns>
                <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn UniqueName="AssetID" HeaderText="Asset ID" DataField="AssetID" ShowFilterIcon="true">
                    <HeaderStyle Width="60px"></HeaderStyle>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="AssetCode" HeaderText="Asset Code" DataField="AssetCode" ShowFilterIcon="true">
                    <HeaderStyle Width="80px"></HeaderStyle>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="AssetName" HeaderText="AssetName" DataField="AssetName" ShowFilterIcon="true" FilterControlWidth="200">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="AssetDescription" HeaderText="Asset Description" DataField="AssetDescription" ShowFilterIcon="true" FilterControlWidth="250">
                    <HeaderStyle Width="350px"></HeaderStyle>
                </telerik:GridBoundColumn>
                 
            </Columns>
            <EditFormSettings CaptionFormatString="Editing Asset: {0}" CaptionDataField="AssetName"
                PopUpSettings-Modal="true" EditFormType="Template">
                <PopUpSettings Width="850" />
                <FormTemplate>
                    <table width="800">
                        <tr>
                            <td>
                                <table cellspacing="10">
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblAssetID" runat="server"><b>Asset ID:</b></asp:Label><br />
                                            <asp:TextBox ID="txtAssetID" runat="server" Enabled="false" BackColor="AntiqueWhite"
                                                CssClass="txt" Text='<%# Eval("AssetID") %>' />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblAssetCode" runat="server"><b>Asset Code:</b></asp:Label><br />
                                            <asp:TextBox ID="txtAssetCode" runat="server" Enabled="false" BackColor="AntiqueWhite"
                                                CssClass="txt" Text='<%# Eval("AssetCode") %>' />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblAssetName" runat="server"><b>Asset Name:<span class="ErrorMessage"> *</span></b></asp:Label><br />
                                            <asp:TextBox ID="txtAssetName" runat="server" CssClass="txt" Text='<%# Eval("AssetName") %>' />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblAssetDescription" runat="server"><b>Asset Description:<span class="ErrorMessage"> *</span></b></asp:Label><br />
                                            <asp:TextBox ID="txtAssetDescription" runat="server" TextMode="MultiLine" Rows="2"
                                                CssClass="txt" Text='<%# Eval("AssetDescription") %>' />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblAssetType" runat="server"><b>Asset Type:<span class="ErrorMessage"> *</span></b></asp:Label><br />
                                            <asp:DropDownList ID="ddlAssetType" runat="server" CssClass="txt" AppendDataBoundItems="true"
                                                DataSource="<%# GetAssetTypes() %>" DataValueField="AssetTypeID" DataTextField="AssetTypeName"
                                                SelectedValue='<%# Eval("AssetTypeID") %>'>
                                                <asp:ListItem Value="0" Text="--- Select an Asset Type ---" />
                                            </asp:DropDownList>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblPhysicalAssetType" runat="server"><b>Physical Asset Type:<span class="ErrorMessage"> *</span></b></asp:Label><br />
                                            <asp:DropDownList ID="ddlPhysicalAssetType" runat="server" CssClass="txt" AppendDataBoundItems="true"
                                                DataSource="<%# GetPhysicalAssetTypes() %>" DataValueField="PhysicalAssetTypeID"
                                                DataTextField="PhysicalAssetTypeName" SelectedValue='<%# (Eval("PhysicalAssetTypeID") == DBNull.Value) ? "0" : Eval("PhysicalAssetTypeID") %>'>
                                                <asp:ListItem Value="0" Text="--- Select a Physical Asset Type ---" />
                                            </asp:DropDownList>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblNextStep" runat="server"><b>Next Step:<span class="ErrorMessage"> *</span></b></asp:Label><br />
                                            <asp:DropDownList ID="ddlNextStep" runat="server" CssClass="txt" AppendDataBoundItems="true"
                                                DataSource="<%# GetNextSteps() %>" DataValueField="NextStepID" DataTextField="NextStepName"
                                                SelectedValue='<%# (Eval("NextStepID") == DBNull.Value) ? "0" : Eval("NextStepID") %>'>
                                                <asp:ListItem Value="0" Text="--- Select The Next Step ---" />
                                            </asp:DropDownList>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblCategory" runat="server"><b>Category:<span class="ErrorMessage"> *</span></b></asp:Label><br />
                                            <asp:RadioButtonList ID="rblCategory" runat="server" RepeatColumns="3" RepeatDirection="Vertical"
                                                DataSource="<%# GetCategories() %>" DataValueField="CategoryID" DataTextField="CategoryName"
                                                SelectedValue='<%# (Eval("CategoryID") == DBNull.Value) ? "0" : Eval("CategoryID") %>'>
                                            </asp:RadioButtonList>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblTatic" runat="server"><b>Tatic:<span class="ErrorMessage"> *</span></b></asp:Label><br />
                                            <asp:RadioButtonList ID="rblTatic" runat="server" RepeatColumns="4" RepeatDirection="Horizontal"
                                                DataSource="<%# GetTatics() %>" DataValueField="TaticID" DataTextField="TaticName"
                                                SelectedIndex='<%# CheckIfValueExists(Eval("TaticID")) %>'>
                                            </asp:RadioButtonList>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblFunctionalPreviewType" runat="server"><b>Functional Preview Type:<span class="ErrorMessage"> *</span></b></asp:Label><br />
                                            <asp:RadioButtonList ID="rblFunctionalPreviewType" runat="server" RepeatColumns="4"
                                                RepeatDirection="Horizontal" DataSource="<%# GetFunctionPreviewTypes() %>" DataValueField="FunctionPreviewTypeID"
                                                DataTextField="FunctionPreviewTypeName" SelectedValue='<%# (Eval("FunctionPreviewTypeID") == DBNull.Value) ? "0" : Eval("FunctionPreviewTypeID") %>'>
                                            </asp:RadioButtonList>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblProgramType" runat="server"><b>Program Type:<span class="ErrorMessage"> *</span></b></asp:Label><br />
                                            <asp:CheckBoxList ID="cblProgramType" runat="server" RepeatColumns="3" RepeatDirection="Vertical"
                                                DataSource="<%# GetProgramTypes() %>" DataValueField="ProgramTypeID" DataTextField="ProgramTypeName">
                                            </asp:CheckBoxList>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <br />
                                            <asp:Button ID="btnUpdate" Text="Update" runat="server" CssClass="workflowbutton2"
                                                CommandName="Update"></asp:Button>
                                              
                                            <asp:Button ID="btnCancel" Text="Cancel" runat="server" CssClass="workflowbutton2"
                                                CausesValidation="False" CommandName="Cancel"></asp:Button>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td valign="top">
                                <table>
                                    <tr>
                                        <td>
                                              
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <%--<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("LargePreviewImage") %>' />--%>
                                            <telerik:RadBinaryImage runat="server" ResizeMode="None" ID="Thumbnail" ImageUrl='<%# Eval("LargePreviewImage") %>'
                                                AlternateText="Thumbnail" CssClass="binary-image" /><br />
                                            <br />
                                            <span class="invalid"></span>
                                            <telerik:RadAsyncUpload runat="server" ID="AsyncUpload1" MaxFileInputsCount="1" OnClientFileUploaded="fileUploaded"
                                                OnFileUploaded="AsyncUpload1_FileUploaded" AllowedFileExtensions="jpeg,jpg,gif,png,bmp"
                                                OnClientValidationFailed="validationFailed">
                                                <Localization Select="Choose New Image" />
                                            </telerik:RadAsyncUpload>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </FormTemplate>
            </EditFormSettings>
        </MasterTableView>
        <ClientSettings>
            <ClientEvents OnRowDblClick="RowDblClick" />
        </ClientSettings>
    </telerik:RadGrid>


File Upload Method (bold = where error is thrown):
protected void AsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
    {
        //Thumbnail.Width = Unit.Pixel(200);
        //Thumbnail.Height = Unit.Pixel(150); 
 
        using (Stream stream = e.File.InputStream)
        {
            byte[] imageData = new byte[stream.Length];
            stream.Read(imageData, 0, (int)stream.Length);
            GridEditFormInsertItem
            RadBinaryImage thumb = (RadBinaryImage)RadGrid1.MasterTableView.FindControl("Thumbnail");
            thumb.DataValue = imageData;
        } 
    }


My RadGrid is contained in a Master Page. How can I get at my "Thumbnail" control?

Thanks,
Mark
0
Princy
Top achievements
Rank 1
answered on 01 Mar 2011, 06:07 AM
Hello Mark,

You can try the following code snippet to access the RadBinaryImage in edit form.

C#:
protected void AsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
   {
        .   .    .    .    .    .
       RadAsyncUpload upload = (RadAsyncUpload)sender;
       GridEditFormItem editItem = (GridEditFormItem)upload.NamingContainer;
       RadBinaryImage thumb = (RadBinaryImage)editItem.FindControl("Thumbnail");
       .   .    .    .    .    .
   }

Thanks,
Princy.
0
Mark
Top achievements
Rank 1
answered on 01 Mar 2011, 05:41 PM
Thank You Princy! That worked wonderfully.

Mark
0
Suderson
Top achievements
Rank 1
answered on 09 May 2011, 07:06 AM
Hi All,

I'm using Telerik Grid and binded an XML through dataset. I've a autyo generate edit coulmn.

 When I press edit, I get a text box with exisitng value and update, cancel button. After typing new value whren i press am not getting the new value in the grid. Can any one help on this. I'm using updatecommand event,but no use so far. Here is my code

 

 Hashtable newValues = new Hashtable();
            ((GridEditableItem)e.Item).ExtractValues(newValues);

            GridEditableItem editItem = (GridEditableItem)e.Item;
            GridEditableItem editedItem = e.Item as GridEditableItem;
            GridEditManager editMan = editedItem.EditManager;

            var obj = editItem.GetDataKeyValue("ConnectionString");

            //TextBox txt = e.Item.FindControl("txtValue") as TextBox;
            //if (txt != null)
            //{
            //    string s = txt.Text;
            //}
            ds.Tables["add"].Rows[editItem.DataSetIndex][1] = "XXXXX";
            //radDisplay.DataSource = ds.Tables["add"];
            radDisplay.Rebind();
            StringWriter sw = new StringWriter();
            ds.WriteXml(sw);
            Session["decryptesXMLString"] = sw.ToString();

0
Shinu
Top achievements
Rank 1
answered on 09 May 2011, 07:53 AM
Hello Suderson,

I have tried the same and that worked as expected. One suggestion is to try the following approach to achieve the same.
C#:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
 {
   if ((e.Item is  GridEditFormItem) && (e.Item.IsInEditMode))//editmode=EditForm/InPlace
       {
           GridEditFormItem edititem = (GridEditFormItem)e.Item;
           TextBox txtbx = (TextBox)edititem.FindControl("txtValue");
           string City= txtbx.Text;
       }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Ben
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Ben
Top achievements
Rank 1
Mark
Top achievements
Rank 1
Suderson
Top achievements
Rank 1
Shinu
Top achievements
Rank 1
Share this question
or