WebUserControl CustomValidator

6 posts, 1 answers
  1. Scott Marx
    Scott Marx avatar
    38 posts
    Member since:
    Apr 2010

    Posted 13 Dec 2011 Link to this post

    I have a RadGrid with the EditFormSettings set to WebUserControl. Within the WebUserControl I have a CustomValidator, when I run the update command from the WebUserControl page the CustomValidator is run, but does not stop the page from processing. I am checking for Page.IsValid on my RadGrid_ItemCommand.

    Page.aspx
    <telerik:RadGrid ID="rgSiteContent" runat="server" CellSpacing="0" GridLines="None" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" onneeddatasource="rgSiteContent_NeedDataSource" onitemcommand="rgSiteContent_ItemCommand">
        <MasterTableView CommandItemDisplay="Top" DataKeyNames="pageid">
            <Columns>
                <telerik:GridTemplateColumn FilterControlAltText="Filter EditCommandColumn column" Groupable="False" UniqueName="EditCommandColumn" HeaderText="Actions">
                    <EditItemTemplate>
                        <telerik:RadButton runat="server" CommandName="Update" Text="Update" /> 
                        <telerik:RadButton runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <telerik:RadButton runat="server" CausesValidation="false" CommandName="Edit" Text="Edit" /> 
                        <telerik:RadButton runat="server" CausesValidation="false" CommandName="Delete" Text="Delete" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn DataField="title" FilterControlAltText="Filter title column" HeaderText="Page Title" ReadOnly="True" SortExpression="title" UniqueName="title" />
                <telerik:GridBoundColumn DataField="metadesc" FilterControlAltText="Filter metadesc column" HeaderText="Meta Description" ReadOnly="True" SortExpression="metadesc" UniqueName="metadesc" />
                <telerik:GridBoundColumn DataField="metaword" FilterControlAltText="Filter metaword column" HeaderText="Meta Keywords" ReadOnly="True" SortExpression="metaword" UniqueName="metaword" />
                <telerik:GridHyperLinkColumn FilterControlAltText="Filter pagelink column" HeaderText="Page Link" SortExpression="pagelink" UniqueName="pagelink" DataTextField="pagelink" Target="_blank" DataNavigateUrlFields="pagelink" />
                <telerik:GridBoundColumn DataField="created" DataType="System.DateTime" FilterControlAltText="Filter created column" HeaderText="Created" ReadOnly="True" SortExpression="created" UniqueName="created" />
                <telerik:GridBoundColumn DataField="lastedit" DataType="System.DateTime" FilterControlAltText="Filter lastedit column" HeaderText="Last Edit" ReadOnly="True" SortExpression="lastedit" UniqueName="lastedit" />
                <telerik:GridBoundColumn DataField="fullname" DataType="System.Int32" FilterControlAltText="Filter fullname column" HeaderText="Edited By" ReadOnly="True" SortExpression="fullname" UniqueName="fullname" />
            </Columns>
            <EditFormSettings EditFormType="WebUserControl" UserControlName="~/managers/controls/admin_sitecontent_editor.ascx">
                <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
            </EditFormSettings>
        </MasterTableView>
    </telerik:RadGrid>

    Page .aspx.cs
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    protected void rgSiteContent_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        getSiteContent();
    }
    protected void getSiteContent()
    {
        using (SiteDataContext DB = new SiteDataContext())
        {
            var rsSiteContent = from rs in DB.contents
                                where rs.parentid == 0
                                select new
                                {
                                    pageid = rs.pageid,
                                    pagelink = String.Format("http://www.mysite.com/site/{0}", rs.pagename),
                                    pagename = rs.pagename,
                                    title = rs.title,
                                    metadesc = rs.metadesc,
                                    metaword = rs.metaword,
                                    created = rs.created,
                                    lastedit = rs.lastedit,
                                    fullname = String.Format("{0} {1}", rs.tbl_user.firstname, rs.tbl_user.lastname),
                                    pagecontent = rs.pagecontent
                                };
            rgSiteContent.DataSource = rsSiteContent.ToList();
        }
    }
    protected void rgSiteContent_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            GridEditFormItem editForm = (GridEditFormItem)e.Item;
            UserControl userControl = (UserControl)editForm.FindControl(GridEditFormItem.EditFormUserControlID);
            
            if (Page.IsValid)
            {
                using (SiteDataContext DB = new SiteDataContext())
                {
                    var rsSiteContent = (from rs in DB.contents where rs.pageid == Convert.ToInt32(editForm.GetDataKeyValue("pageid")) select rs).SingleOrDefault();
                    rsSiteContent.pagename = ((RadTextBox)userControl.FindControl("pagelinkTextBox")).Text;
                    rsSiteContent.title = ((RadTextBox)userControl.FindControl("pagetitleTextBox")).Text;
                    rsSiteContent.pagecontent = (((RadEditor)userControl.FindControl("contentEditor"))).Content;
                    rsSiteContent.metadesc = ((RadTextBox)userControl.FindControl("metadescTextBox")).Text;
                    rsSiteContent.metaword = ((RadTextBox)userControl.FindControl("metawordTextBox")).Text;
                    rsSiteContent.lastedit = DateTime.Now;
                    rsSiteContent.editby = Profile.UserID;
                    DB.SubmitChanges();
                }
            }
        }
    }

    WebUserControl ascx
    <asp:CustomValidator ID="editcontentCustomValidator" runat="server" ControlToValidate="pagetitleTextBox" ValidationGroup="editcontentValidationGroup" onservervalidate="editcontentCustomValidator_ServerValidate" Font-Bold="true" ForeColor="Red" ValidateEmptyText="true" /> 
    <table style="width:100%;">
        <tr>
            <td colspan="2"
                <telerik:RadButton runat="server" CommandName="Update" CausesValidation="true" Text="Save" ValidationGroup="editcontentValidationGroup" /> 
                <telerik:RadButton runat="server" CommandName="Cancel" CausesValidation="false" Text="Cancel" />
            </td>
        </tr>
        <tr>
            <td style="width:110px;">Page Link:</td>
            <td><telerik:RadTextBox ID="pagelinkTextBox" Runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.pagename") %>' Width="800px" /></td>
        </tr>
        <tr>
            <td style="width:110px;">Page Title:</td>
            <td><telerik:RadTextBox ID="pagetitleTextBox" Runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.title") %>' Width="800px" /></td>
        </tr>
        <tr>
            <td style="width:110px;">Meta Description:</td>
            <td><telerik:RadTextBox ID="metadescTextBox" Runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.metadesc") %>' Width="800px" /></td>
        </tr>
        <tr>
            <td style="width:110px;">Meta Keywords:</td>
            <td><telerik:RadTextBox ID="metawordTextBox" Runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.metaword") %>' Width="800px" /></td>
        </tr>
        <tr>
            <td colspan="2">Page Content:</td>
        </tr>
        <tr>
            <td colspan="2">
                <telerik:RadEditor ID="contentEditor" Runat="server" Width="940px"  Content='<%# DataBinder.Eval(Container, "DataItem.pagecontent") %>' ContentAreaCssFile="~/Styles/RadEditor.css" ContentAreaMode="Div" Height="800px"  NewLineMode="Br" />
            </td>
        </tr>
    </table>

    WebUserControl ascx.cs
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    protected void editcontentCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        List<string> strErrors = new List<string>();
        if (pagelinkTextBox.Text.Count() > 0)
        {
            using (SiteDataContext DB = new SiteDataContext())
            {
                var rsCheckDupPageName = from rs in DB.contents where rs.pagename == pagelinkTextBox.Text select rs;
                if (rsCheckDupPageName.Count() > 1)
                {
                    strErrors.Add("Duplicate Page Links are not allowed.");
                }
            }
        }
        else
        {
            strErrors.Add("Page Link is required.");
        }
        if (pagetitleTextBox.Text.Count() <= 0)
        {
            strErrors.Add("Page Title is required.");
        }
        if (metadescTextBox.Text.Count() <= 0)
        {
            strErrors.Add("Meta Description is required.");
        }
        if (metawordTextBox.Text.Count() <= 0)
        {
            strErrors.Add("Meta Keywords are required.");
        }
        if (contentEditor.Text.Count() <= 0)
        {
            strErrors.Add("Page Content is required.");
        }
        if (strErrors.Count() > 0)
        {
            string strErrorMessage = String.Empty;
            foreach (string errors in strErrors)
            {
                strErrorMessage = String.Format("{0}<br />", errors);
            }
            editcontentCustomValidator.IsValid = false;
            editcontentCustomValidator.ErrorMessage = strErrorMessage;
        }
    }


  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 13 Dec 2011 Link to this post

    Hello Scott,

    Check the following help documentation which explains about validation.
    Validation

    -Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Scott Marx
    Scott Marx avatar
    38 posts
    Member since:
    Apr 2010

    Posted 14 Dec 2011 Link to this post

    Not sure why you sent me there... My code already followed the examples there. The problem is passing the failed validation to the page from the WebUserControl.


    Opps... My bad. I see I needed to change editcontentCustomValidator.IsValid to args.IsValid.
  5. Balakrishna Reddy
    Balakrishna Reddy avatar
    37 posts
    Member since:
    Oct 2012

    Posted 29 Oct 2012 Link to this post

    Hi,
       Please anybody Help me.
    I am using Rad Grid with web user control in Editformsettings mode as web control..I am using radasyuploader when i am clicking on upload button for updating  i am getting an error saying that 

    Insert item is available only when grid is in insert mode.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: Telerik.Web.UI.GridException: Insert item is available only when grid is in insert mode.

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace: 



    The following is my MyUserControl.ascx file...

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyUserControl.ascx.cs" Inherits="MyUserControl" %>


    <%@ Register  Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>


    <style type="text/css">


        .style2


        {


            width: 270px;


        }


        .style3


        {


            width: 272px;


        }


    </style>



    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


    <table width="100%" >


    <tr>


    <td>


    <table id="Table3" cellspacing="1" cellpadding="1" width="100%" border="0">


        <tr>


            <td>


                <h3>Web User Control</h3>


            </td>


        </tr>


        <tr>


            <td class="style3">


                Person ID &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


            


                <telerik:RadTextBox ID="txtPID" runat="server" ReadOnly="true" Text='<%# DataBinder.Eval( Container, "DataItem.PID" ) %>' />


                <br />


                <br />


            </td>


        </tr>


        <tr>


            <td class="style3">


                Person Name &nbsp; &nbsp;&nbsp;


                <telerik:RadTextBox ID="txtPName" runat="server" Text='<%# DataBinder.Eval( Container, "DataItem.PName" ) %>' />


                <br />


                <br />


            </td>


        </tr>


        <tr>


            <td class="style3">


                Country Name &nbsp;&nbsp;


                <telerik:RadComboBox ID="rcbCountryName" runat="server" AutoPostBack="true"


                    DataMember='<%# DataBinder.Eval( Container, "DataItem.COU_Name" ) %>' DataValueField="COU_Name"


                     DataTextField="COU_Name" Text='<%# DataBinder.Eval( Container, "DataItem.COU_Name" ) %>'


                    onselectedindexchanged="rcbCountryName_SelectedIndexChanged" />


                &nbsp;&nbsp;


                <br />


                <br />


            </td>


        </tr>


        <tr>


            <td class="style3">


                State Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


                <telerik:RadComboBox ID="rcbStateName" runat="server"  AutoPostBack="true"


                    DataMember='<%# DataBinder.Eval( Container, "DataItem.STE_Name" ) %>' 


                    DataValueField="STE_Name" Text='<%# DataBinder.Eval( Container, "DataItem.STE_Name" ) %>'


                    onselectedindexchanged="rcbStateName_SelectedIndexChanged" />


                &nbsp;&nbsp;


                <br />


                <br />


            </td>


        </tr>


        <tr>


            <td class="style3">


                City Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


                <telerik:RadComboBox ID="rcbCityName" runat="server"  AutoPostBack="true"


                    DataMember='<%# DataBinder.Eval( Container, "DataItem.CIT_Name" ) %>' 


                    DataValueField="CIT_Name" 


                    Text='<%# DataBinder.Eval( Container, "DataItem.CIT_Name" ) %>' />


                &nbsp;&nbsp;


                <br />


                <br />


            </td>


       </tr>


     </table>


     </td>


     <td>


    <table id="Tabl2" cellspacing="1" cellpadding="1" width="100%" border="0">


        <tr>


            <td>


                <telerik:RadAsyncUpload ID="Uplader" runat="server" TargetFolder="Photos"


                        Height="25px" onfileuploaded="Uplader_FileUploaded" Skin="Sunset" 


                    Width="243px"/>


                <br />


                <br />


                &nbsp;&nbsp;&nbsp;&nbsp;


                <asp:Button ID="btnUpload" runat="server"  Text="UploadFile"


                        CommandName="PerformInsert" Visible="true" Width="96px" onclick="btnUpload_Click"/>


                <br />


            </td> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;


            <td>


                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


                <telerik:RadBinaryImage ID="radbinaryimg" runat="server" Height="80px" ImageUrl='<%#"~/Photos/" + Eval("IMG_Name") %>' />


            </td>


        </tr>


       <tr>


        <td class="style2">


            &nbsp;&nbsp;&nbsp;&nbsp;


            Width="89px"/>


            &nbsp;&nbsp;&nbsp;--%>


            <asp:Button ID="btnSubmit" runat="server" CommandName="PerformInsert" 


                Visible="true" Width="96px" onclick="btnSubmit_Click"></asp:Button>


                    &nbsp;&nbsp;&nbsp;  


            <asp:Button ID="btnCancel" Text="Cancel" runat="server"  CausesValidation="False" CommandName="Cancel" Width="96px" 


                onclick="btnCancel_Click"></asp:Button>


                         </asp:Button>


                <asp:Button ID="btnInsert" Text="Insert" runat="server" CommandName="PerformInsert"


                    Visible='<%# "DataItem" is Telerik.Web.UI.GridInsertionObject %>'></asp:Button>


                &nbsp;


    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"


    CommandName="Cancel"></asp:Button>--%>


        </td>


       </tr>


    </table>


    </td>


    </tr>


    </table>
    *************************************
    and MyUserControlPage.aspx is as follows:
    *************************************************

    <body>
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>




        <div>
            <telerik:RadGrid ID="RadGrid1" runat="server" 
                 AutoGenerateColumns="False" AllowPaging="True" PageSize="5" Skin="Vista" 
                onitemcommand="RadGrid1_ItemCommand" onitemdatabound="RadGrid1_ItemDataBound" onupdatecommand="RadGrid1_UpdateCommand" 
                >
                <PagerStyle Mode="NextPrevAndNumeric" />
                <MasterTableView DataKeyNames="PID" CommandItemDisplay="TopAndBottom">
                  <Columns>
                        <telerik:GridEditCommandColumn UniqueName="Editcolumn"></telerik:GridEditCommandColumn>
                        <telerik:GridBoundColumn  DataField="PID" ReadOnly ="true" UniqueName="Pid" Visible="false" >
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="PName" HeaderText="Person Name" UniqueName="PName">
                        </telerik:GridBoundColumn>
                        <telerik:GridDropDownColumn DataField="COU_Name" UniqueName="COU_Name" HeaderText="Country Name" ListDataMember="Country_Master" ListValueField="COU_Name" >
                        </telerik:GridDropDownColumn>
                        <telerik:GridDropDownColumn DataField="STE_Name" UniqueName="STE_Name" HeaderText="State Name" ListDataMember="State_Master" ListValueField="STE_Name" >
                        </telerik:GridDropDownColumn>
                        <telerik:GridDropDownColumn DataField="CIT_Name" UniqueName="CIT_Name" HeaderText="City Name" ListDataMember="City_Master" ListValueField="CIT_Name" >
                        </telerik:GridDropDownColumn>
                        <telerik:GridTemplateColumn HeaderText="Image" UniqueName="IMAGE">
                            <ItemTemplate>
                                <telerik:RadBinaryImage ID="radbinaryimg" runat="server" Height="80px" ImageUrl='<%# "~/Photos/" + Eval("IMG_Name") %>' />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                  </Columns>
                    <EditFormSettings UserControlName="~/MyUserControl.ascx" EditFormType="WebUserControl">
                        <EditColumn UniqueName="EditColumn1" ButtonType="ImageButton"></EditColumn>
                    </EditFormSettings>
                </MasterTableView>
            </telerik:RadGrid>
        </div>
        </form>
    </body>
    </html>
    ***********************************************************
    My problem is that i want insert and update values.So Please help me.
    please help me what i want to write in MyUserControl.ascx.csfile and in ASPX.CS file.
    Please.
    please.
    please.
  6. Balakrishna Reddy
    Balakrishna Reddy avatar
    37 posts
    Member since:
    Oct 2012

    Posted 29 Oct 2012 Link to this post

    hi .
    I am using web user control in rad grid editformsettings..In user control i am using RadAsyUploader for uploading files.my problem is when i am trying to update the values of grid when i am clicking on the upload button the control will automatically executes the "RadGrid1_InsertCommand()" method.and it throws an error as "Insert item is available only when grid is in insert mode."

    So please he me in getting off this problem........
    please.
    please.


    Thanks in Advance...........

  7. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 01 Nov 2012 Link to this post

    Hello Balakrishna,

    I would suggest you to review the following online demo which presents the same functionality. Test it on your end and verify of this helps:
    http://demos.telerik.com/aspnet-ajax/controls/examples/integration/gridandradasyncupload/defaultcs.aspx?product=asyncupload


    Kind regards,
    Maria Ilieva
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017