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

Grid: Added Record with Template

2 Answers 59 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David Beck
Top achievements
Rank 1
David Beck asked on 02 Jun 2010, 08:00 PM
I am trying to us a FormTemplate using Pop-up to add/update records.

I get the Call to the Server but when i try to pull values from the Textbox they are coming back as Null that it cant find thought controls. I have search for hours on the forum and tried various thing and just cat seem to get it to work here is the Code.

NOTE: server side i get these 2 error for (THIS IS ON AN INSERT)

GridEditableItem object

EditManger Property: The current EditFormType does not support the requested editing capabilities.
Key Values Property: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


<telerik:RadGrid ID="TestimonialsGrid" runat="server" Skin="WebBlue" OnDataBound="TestimonialsGrid_DataBound" 
            OnNeedDataSource="TestimonialsGrid_NeedDataSource" AutoGenerateColumns="false" 
            ShowStatusBar="true" oninsertcommand="TestimonialsGrid_InsertCommand">  
            <MasterTableView Width="100%" CommandItemDisplay="TopAndBottom" PagerStyle-AlwaysVisible="true" 
                DataKeyNames="Testimonial_ID" EditMode="PopUp">  
                <CommandItemSettings ShowAddNewRecordButton="true" AddNewRecordText="Add A New Testimonial" /> 
                <RowIndicatorColumn Visible="True">  
                </RowIndicatorColumn> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="Testimonial_ID" Display="false" HeaderText="ID">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Testimonial_Name" Display="true" HeaderText="Name" 
                        Groupable="true">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Testimonial_Email" Display="true" HeaderText="Email" 
                        Groupable="true">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Testimonial_Text" Display="true" HeaderText="Text" 
                        Groupable="true">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Testimonial_Approved" Display="true" HeaderText="Approved" 
                        Groupable="true">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridDateTimeColumn DataField="Testimonial_CreateDate" Display="true" HeaderText="Create Date" 
                        Groupable="true">  
                    </telerik:GridDateTimeColumn> 
                </Columns> 
                <EditFormSettings EditFormType="Template">  
                    <PopUpSettings Modal="True"></PopUpSettings> 
                    <FormTemplate> 
                        <table id="Table1" cellspacing="1" cellpadding="1" width="250" border="0">  
                            <tr> 
                                <td align="right" valign="top">  
                                </td> 
                                <td> 
                                    <asp:ValidationSummary ID="valSumTestimonial" runat="server" DisplayMode="BulletList" 
                                        HeaderText="Please correct the following error(s): " ShowSummary="false" ShowMessageBox="true" /> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td align="right" valign="top">  
                                    <asp:RequiredFieldValidator ID="reqValName" runat="server" ErrorMessage="Display Name is required." 
                                        Text="*" ControlToValidate="txtDisplayName" InitialValue="" SetFocusOnError="false" 
                                        Display="Dynamic"></asp:RequiredFieldValidator>Display Name:  
                                </td> 
                                <td> 
                                    <telerik:RadTextBox ID="txtDisplayName" runat="server" Text='<%# Bind( "Testimonial_Name") %>' 
                                        TextMode="MultiLine" Rows="3" Columns="50" MaxLength="500" EmptyMessage="(What you place will be displayed as the name) \\r\\n \r\n  Suggestion: Name, company name and position, city, state.">  
                                    </telerik:RadTextBox> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td align="right" valign="top">  
                                    <asp:RequiredFieldValidator ID="reqValEmail" runat="server" ErrorMessage="Email is required." 
                                        Text="*" ControlToValidate="txtEmailAddress" InitialValue="" Display="Dynamic"></asp:RequiredFieldValidator><asp:RegularExpressionValidator  
                                            ID="regValEmail" runat="server" ErrorMessage="Email is in an incorrect format." 
                                            Text="*" ControlToValidate="txtEmailAddress" Display="dynamic" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>Email:  
                                </td> 
                                <td> 
                                    <telerik:RadTextBox ID="txtEmailAddress" runat="server" Text='<%# Bind( "Testimonial_Email") %>' 
                                        MaxLength="500" Width="315px" EmptyMessage="(Email is NOT shared or displayed on the website)">  
                                    </telerik:RadTextBox> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td align="right" valign="top">  
                                    <asp:RequiredFieldValidator ID="reqValTestimonial" runat="server" ErrorMessage="Testimonial Message is required." 
                                        Text="*" ControlToValidate="txtTestimonial" InitialValue="" SetFocusOnError="false" 
                                        Display="Dynamic"></asp:RequiredFieldValidator>Testimonial:  
                                </td> 
                                <td> 
                                    <telerik:RadTextBox ID="txtTestimonial" runat="server" Text='<%# Bind( "Testimonial_Text") %>' 
                                        TextMode="MultiLine" Rows="10" Columns="50" MaxLength="500" EmptyMessage="Type Your Testimonial Here!">  
                                    </telerik:RadTextBox> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td align="right" valign="top">  
                                </td> 
                                <td> 
                                    <telerik:RadCaptcha ID="RadCaptcha1" runat="server" Display="Dynamic" ProtectionMode="InvisibleTextBox">  
                                    </telerik:RadCaptcha> 
                                </td> 
                            </tr> 
                        </table> 
                        <table style="width: 100%">  
                            <tr> 
                                <td align="right" colspan="2">  
                                    <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' 
                                        runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>  
                                    </asp:Button>&nbsp;  
                                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" 
                                        CommandName="Cancel"></asp:Button> 
                                </td> 
                            </tr> 
                        </table> 
                    </FormTemplate> 
                </EditFormSettings> 
                <PagerStyle AlwaysVisible="True"></PagerStyle> 
            </MasterTableView> 
            <ClientSettings ReorderColumnsOnClient="True" AllowDragToGroup="True" AllowColumnsReorder="True">  
                <Selecting AllowRowSelect="True"></Selecting> 
                <Resizing AllowRowResize="True" AllowColumnResize="True" EnableRealTimeResize="True" 
                    ResizeGridOnColumnResize="False"></Resizing> 
            </ClientSettings> 
            <GroupingSettings ShowUnGroupButton="true" /> 
            <PagerStyle Mode="NextPrevAndNumeric" Position="TopAndBottom" /> 
        </telerik:RadGrid> 

 private SQL oSQL = new SQL();  
    private string gridMessage = null;  
 
    protected void Page_Load(object sender, EventArgs e)  
    {  
 
    }  
    protected void TestimonialsGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
    {  
        LoadTestimonialsGrid();  
    }  
    private void LoadTestimonialsGrid()  
    {  
        oSQL = Testimonial.GetAllTestimonials();  
        TestimonialsGrid.DataSource = oSQL.SQL_DataReader;  
    }  
    protected void TestimonialsGrid_DataBound(object sender, EventArgs e)  
    {  
        oSQL.CloseConnection();  
        //if (!string.IsNullOrEmpty(gridMessage))  
        //{  
            DisplayMessage(gridMessage);  
        //}  
 
    }  
    private void SetMessage(string message)  
    {  
        gridMessage = message;  
    }  
    private void DisplayMessage(string text)  
    {  
        TestimonialsGrid.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));  
    }  
 
    protected void TestimonialsGrid_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
    {  
        if (e.CommandName == RadGrid.PerformInsertCommandName)  
        {  
            SetMessage("New Testimonial Added!");  
 
            GridEditableItem editedItem = e.Item as GridEditableItem;  
            GridEditFormInsertItem e2 = e.Item as GridEditFormInsertItem;  
            GridEditFormItem e3 = e.Item as GridEditFormItem;  
 
            //Insert new values  
            Testimonial oTestimonial = new Testimonial();  
 
            TextBox tb = e2.FindControl("txtDisplayName"as TextBox;  
            TextBox tb2 = e3.FindControl("txtDisplayName"as TextBox;  
            TextBox tb3 = e.Item.FindControl("txtDisplayName"as TextBox;  
            //TextBox tb4 = e2["Testimonial_Name"].Controls[0] as TextBox;  
            oTestimonial.Display_Name = (editedItem.FindControl("txtDisplayName"as TextBox).Text;  
            //oTestimonial.Email_Address = (editedItem.FindControl("txtEmailAddress") as TextBox).Text;  
            //oTestimonial.Message = (editedItem.FindControl("txtTestimonial") as TextBox).Text;  
            //oTestimonial.Approved = "N";  
            //oTestimonial.Create_Date = DateTime.Now;  
            //oTestimonial.InsertTestimonial();  
        }  
    } 

2 Answers, 1 is accepted

Sort by
0
David Beck
Top achievements
Rank 1
answered on 02 Jun 2010, 09:37 PM
Sometimes stepping away for a Few Hours and doing someting fun and coming back Help. I found the Issue Althought it was strange

I was trying to cast a RADTextBox to a TextBox and it kept coming back as null rather than an invalid cast. But it works now YAY!!
0
Jeff Reinhardt
Top achievements
Rank 1
answered on 02 Jun 2010, 09:38 PM
Not sure if this will help but I am reading some values from a grid after Insert using the following:

protected

 

void radgrdNotes_ItemInserted(object source, GridInsertedEventArgs e)
{
    

 

if

 

(e.Exception != null)

 

{

e.ExceptionHandled =

true;

 

e.KeepInInsertMode =

true;

 

DisplayMessage(

true, "Note cannot be inserted. Reason: " + e.Exception.Message);

 

}

 

else

 

 

 

 

{

 

DisplayMessage(

false, "Note inserted.");

 

 

RadTextBox EnteredBy = (RadTextBox)e.Item.FindControl("EnteredBy");

 

if (EnteredBy != null)

 

 

    string enteredBy = EnteredBy.Text;


This seems to work just fine for my scenario, hope it helps for yours.

Tags
Grid
Asked by
David Beck
Top achievements
Rank 1
Answers by
David Beck
Top achievements
Rank 1
Jeff Reinhardt
Top achievements
Rank 1
Share this question
or