?
Solved

Custom validator in edititemtemplate of a datagrid

Posted on 2006-04-10
10
Medium Priority
?
411 Views
Last Modified: 2012-06-27
Hello,

I have an editable datagrid and wish to use a custom vaidator on one of the fields as it needs to check back with the database to determine if the entry is valid. Hence it need to use a server side validation function.

However I cannot get the error message to display. Even if I just put in a test procedure in that sets IsValid = False the error message will not display. It always thinks that the page is valid. A similar custom validator in the footer of the datagrid works fine.

Is it something to do with running a server-side validation?

here is the column

<asp:templatecolumn>                                          
<headertemplate>Unit</headertemplate>
            <headerstyle Font-Bold="true" Width="100"></headerstyle>
      <itemtemplate><%# DataBinder.Eval(Container.DataItem, "UNIT") %></itemtemplate>
            <itemstyle Width="100"></itemstyle>
      <edititemtemplate>
            <asp:dropdownlist runat="server" id="ddlOutputUnit" CssClass="DDL_STYLE" Width="100" DataValueField="UNIT_ID" DataTextField="UNIT" />
            <asp:customvalidator CssClass="ERR_STYLE" id="cvdCompUnitUpdate" runat="server" ErrorMessage="Must use the compulsory unit" ControlToValidate="ddlOutputUnit" Display="Static" OnServerValidate="Test_Validate"/>            
      </edititemtemplate>                              
      <footertemplate>
            <asp:dropdownlist runat="server" id="ddlOutputUnit_add" CssClass="DDL_STYLE" Width="100" DataValueField="UNIT_ID" DataTextField="UNIT"/>
            <asp:customvalidator CssClass="ERR_STYLE" id="cvdCompUnit" runat="server" ErrorMessage="Must use the compulsory unit" ControlToValidate="ddlOutputUnit_add" Display="Dynamic" OnServerValidate="cvdCompUnit_Validate"/>            
      </footertemplate>
</asp:templatecolumn>


cvdCompUnit_Validate works fine

but even if I set

      Private Sub test_Valid(source as object, args as ServerValidateEventArgs)
            args.IsValid = False
      End Sub


the error message is never displayed and the page is always valid

0
Comment
Question by:vfuwhite
  • 5
  • 2
7 Comments
 
LVL 5

Expert Comment

by:Jojo1771
ID: 16423075
This is simpe enoguh.  


In your event for update

do this


CType(e.Item.FindControl("cvdCompUnitUpdate"), customvalidator ).IsValid = False

Datagirds have lots of items, so there is no way to call it by ID.

If this not work please post your code behind. So I can take a look.
0
 

Author Comment

by:vfuwhite
ID: 16423274
I don't see the point in setting the validator to false in the updatecommand

Anyway below if the update command

the page is definitely not valid but the error message doesn't display and the datagrid returns to the non-edit mode

Sub dgOutputs_Update(sender as Object, e as DataGridCommandEventArgs)
      
      Response.Write("<BR>dgOutputs_Update")
      Page.Validate
      if page.isvalid then
            Response.Write("<BR> Page IS VALID")
            Dim intOutcomeID as int32 = ddloutcome.Selecteditem.Value
            
            Dim intID as Int32 = int32.Parse(dgOutputs.DataKeys(e.Item.ItemIndex))
            Dim strSMU as String = e.Item.Cells(1).text
            Dim intOutputID as int32 = Ctype(e.Item.FindControl("ddlOutput"), DropDownList).SelectedItem.Value
            Dim intWeed as int32
            
            'Only Save the Weed if the Output is Weeding
            if IsWeedingOutput(intOutputID) then
                  intWeed = Ctype(e.Item.FindControl("ddlWeed"), DropDownList).SelectedItem.Value
            end if
      
            Dim dblQty as Double = Ctype(e.Item.FindControl("txtQty"), textbox).text
            Dim intOutputUnitID as int32 = Ctype(e.Item.FindControl("ddlOutputUnit"), DropDownList).SelectedItem.Value
            
            Dim strSQL as string = "UPDATE SMU_LVL2_ACTIVITIES_TBL " & _
                  " SET SMU_LVL2_CODE = @SMU_LVL2_CODE, ACTIVITY_ID = @ACTIVITY_ID, QTY = @QTY, " & _
                  " UNIT_ID = @UNIT_ID, WEED_ID = @WEED_ID " & _
                  " WHERE [SMU_LVL2_ACTIVITY_ID] = " & intID                  
            ConnectDB()
            dim cmd as oledbCommand = New oledbCommand(strSQL, m_objConn)
            cmd.Commandtype = Commandtype.text
            With  cmd
                  .Parameters.add("@SMU_LVL2_CODE",oledbtype.varchar)
                  .Parameters.add("@ACTIVITY_ID",oledbtype.integer)
                  .Parameters.add("@QTY",oledbtype.double)
                  .Parameters.add("@UNIT_ID",oledbtype.integer)
                  .Parameters.add("@WEED_ID",oledbtype.integer)
                  .Parameters("@SMU_LVL2_CODE").value = strSMU
                  .Parameters("@ACTIVITY_ID").value = intOutputID
                  .Parameters("@QTY").value = dblQty
                  .Parameters("@UNIT_ID").value = intOutputUnitID
                  If intWeed = nothing then
                        .Parameters("@WEED_ID").value = DBNull.value
                  else
                        .Parameters("@WEED_ID").value = intWeed                  
                  end if
            End With
            Dim lRecordsAffected = cmd.ExecuteNonQuery()
            DisconnectDB()                  
            dgOutputs.showfooter=false
            dgOutputs.EditItemIndex = -1
            SetUpOutputData()
            SetUpOutputUnitAdd()
                Else
                      Response.Write("<BR> PAGE IS NOT VALID")
               End if
      
End sub      
0
 

Author Comment

by:vfuwhite
ID: 16423478
It seems that if I put the Page.Validate at the start of the Sub then the page is always invalid. If I remove it the page is always valid.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Accepted Solution

by:
Jojo1771 earned 2000 total points
ID: 16428182
In your code

where you have

  Else
                     Response.Write("<BR> PAGE IS NOT VALID")
               End if

could you not do

  Else
                Response.Write("<Script> alert('Page not valid') </Script>)
                'or
                label1.text ="Not valid"
                ' or about anything you  want.  Then
                dgOutputs.EditItemIndex = e.Item.ItemIndex
 End if
0
 

Author Comment

by:vfuwhite
ID: 16431253
I am only putting  Response.Write("<BR> PAGE IS NOT VALID") in the code to debug. I don't want the update to do anything if the page is not valid except dgOutputs.EditItemIndex = e.Item.ItemIndex like you have noted above, and display the custom error message of course.

The trouble is that it does not get there even if the custom validator returns false. The page is always valid. However if I put a page.validate in there the page is always invalid and it will display the above error message. However it still does not display the custom error message.

I have also tried using a error message in an asp:label and setting it to visisble if the page the page is invalid but it will also not display.

I have tested the custom validator works with client side validation and it works fine.

I also have a custom validator in the footer of the datagrid and it also works fine.

So I assume it is something that happens in the update command.

0
 

Author Comment

by:vfuwhite
ID: 16432297
The only thng I can get working is a label outside of the datagrid with an an error message. I now have function that runs the valdation check within the Update Command and displays the label error message if it returns false.

The only trouble is that the dgOutputs.EditItemIndex = e.Item.ItemIndex does not leave the record in the editing state. The update does not run because of the validation check but it returns to the non-editing state. Here is the current code :

Sub dgOutputs_Update(sender as Object, e as DataGridCommandEventArgs)

      Dim intOutcomeID as int32 = ddloutcome.Selecteditem.Value
            
               Dim intID as Int32 = int32.Parse(dgOutputs.DataKeys(e.Item.ItemIndex))
      Dim strSMU as String = e.Item.Cells(1).text
      Dim intOutputID as int32 = Ctype(e.Item.FindControl("ddlOutput"), DropDownList).SelectedItem.Value
      Dim intWeed as int32
            
      'Only Save the Weed if the Output is Weeding
      if IsWeedingOutput(intOutputID) then
            intWeed = Ctype(e.Item.FindControl("ddlWeed"), DropDownList).SelectedItem.Value
      end if
      
      Dim dblQty as Double = Ctype(e.Item.FindControl("txtQty"), textbox).text
      Dim intOutputUnitID as int32 = Ctype(e.Item.FindControl("ddlOutputUnit"), DropDownList).SelectedItem.Value
            
      'Check the Compulsory Unit
      If CompUnitUpdate_Validate(intOutcomeID,  intOutputID, intOutputUnitID, strSMU, intID) then
                  
            Dim strSQL as string = "UPDATE SMU_LVL2_ACTIVITIES_TBL " & _
                        " SET SMU_LVL2_CODE = @SMU_LVL2_CODE, ACTIVITY_ID = @ACTIVITY_ID, QTY = @QTY, " & _
                        " UNIT_ID = @UNIT_ID, WEED_ID = @WEED_ID " & _
                              " WHERE [SMU_LVL2_ACTIVITY_ID] = " & intID                  
            Try
                  ConnectDB()
                  dim cmd as oledbCommand = New oledbCommand(strSQL, m_objConn)
                  cmd.Commandtype = Commandtype.text
                  With  cmd
                        .Parameters.add("@SMU_LVL2_CODE",oledbtype.varchar)
                        .Parameters.add("@ACTIVITY_ID",oledbtype.integer)
                        .Parameters.add("@QTY",oledbtype.double)
                        .Parameters.add("@UNIT_ID",oledbtype.integer)
                        .Parameters.add("@WEED_ID",oledbtype.integer)
                        .Parameters("@SMU_LVL2_CODE").value = strSMU
                        .Parameters("@ACTIVITY_ID").value = intOutputID
                        .Parameters("@QTY").value = dblQty
                        .Parameters("@UNIT_ID").value = intOutputUnitID
                        If intWeed = nothing then
                              .Parameters("@WEED_ID").value = DBNull.value
                        else
                              .Parameters("@WEED_ID").value = intWeed                  
                        end if
                  End With
                  Dim lRecordsAffected = cmd.ExecuteNonQuery()
            Catch ex as exception            
                  DisconnectDB()
                  Response.Write(ex.Message)
                  Response.End
            Finally
                           DisconnectDB()                  
            End Try
            dgOutputs.showfooter=false
            dgOutputs.EditItemIndex = -1
            SetUpOutputData()
            SetUpOutputUnitAdd()
      Else
            Response.Write("<BR> <BR> Page IS NOT VALID")
            lblTest.Visible = True
            dgOutputs.EditItemIndex = e.Item.ItemIndex
      End if
      
End sub      
0
 

Author Comment

by:vfuwhite
ID: 16432315
OK I have worked out that I need a dgOutputs.DataBind after the last line and it remains in the Edit Mode.

Still doesn't answer my original question. But it will have to do.

Thanks
Vicky
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question