vfuwhite
asked on
Custom validator in edititemtemplate of a datagrid
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</head ertemplate >
<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="ddlOutp utUnit" Display="Static" OnServerValidate="Test_Val idate"/>
</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="ddlOutp utUnit_add " Display="Dynamic" OnServerValidate="cvdCompU nit_Valida te"/>
</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
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</head
<headerstyle Font-Bold="true" Width="100"></headerstyle>
<itemtemplate><%# DataBinder.Eval(Container.
<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="ddlOutp
</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="ddlOutp
</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
ASKER
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>dgOutp uts_Update ")
Page.Validate
if page.isvalid then
Response.Write("<BR> Page IS VALID")
Dim intOutcomeID as int32 = ddloutcome.Selecteditem.Va lue
Dim intID as Int32 = int32.Parse(dgOutputs.Data Keys(e.Ite m.ItemInde x))
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(intOutputI D) 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(" ddlOutputU nit"), 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",ole dbtype.var char)
.Parameters.add("@ACTIVITY _ID",oledb type.integ er)
.Parameters.add("@QTY",ole dbtype.dou ble)
.Parameters.add("@UNIT_ID" ,oledbtype .integer)
.Parameters.add("@WEED_ID" ,oledbtype .integer)
.Parameters("@SMU_LVL2_COD E").value = strSMU
.Parameters("@ACTIVITY_ID" ).value = intOutputID
.Parameters("@QTY").value = dblQty
.Parameters("@UNIT_ID").va lue = intOutputUnitID
If intWeed = nothing then
.Parameters("@WEED_ID").va lue = DBNull.value
else
.Parameters("@WEED_ID").va lue = 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
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>dgOutp
Page.Validate
if page.isvalid then
Response.Write("<BR> Page IS VALID")
Dim intOutcomeID as int32 = ddloutcome.Selecteditem.Va
Dim intID as Int32 = int32.Parse(dgOutputs.Data
Dim strSMU as String = e.Item.Cells(1).text
Dim intOutputID as int32 = Ctype(e.Item.FindControl("
Dim intWeed as int32
'Only Save the Weed if the Output is Weeding
if IsWeedingOutput(intOutputI
intWeed = Ctype(e.Item.FindControl("
end if
Dim dblQty as Double = Ctype(e.Item.FindControl("
Dim intOutputUnitID as int32 = Ctype(e.Item.FindControl("
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
.Parameters.add("@ACTIVITY
.Parameters.add("@QTY",ole
.Parameters.add("@UNIT_ID"
.Parameters.add("@WEED_ID"
.Parameters("@SMU_LVL2_COD
.Parameters("@ACTIVITY_ID"
.Parameters("@QTY").value = dblQty
.Parameters("@UNIT_ID").va
If intWeed = nothing then
.Parameters("@WEED_ID").va
else
.Parameters("@WEED_ID").va
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
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
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.Va lue
Dim intID as Int32 = int32.Parse(dgOutputs.Data Keys(e.Ite m.ItemInde x))
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(intOutputI D) 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(" ddlOutputU nit"), DropDownList).SelectedItem .Value
'Check the Compulsory Unit
If CompUnitUpdate_Validate(in tOutcomeID , 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",ole dbtype.var char)
.Parameters.add("@ACTIVITY _ID",oledb type.integ er)
.Parameters.add("@QTY",ole dbtype.dou ble)
.Parameters.add("@UNIT_ID" ,oledbtype .integer)
.Parameters.add("@WEED_ID" ,oledbtype .integer)
.Parameters("@SMU_LVL2_COD E").value = strSMU
.Parameters("@ACTIVITY_ID" ).value = intOutputID
.Parameters("@QTY").value = dblQty
.Parameters("@UNIT_ID").va lue = intOutputUnitID
If intWeed = nothing then
.Parameters("@WEED_ID").va lue = DBNull.value
else
.Parameters("@WEED_ID").va lue = 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
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.Va
Dim intID as Int32 = int32.Parse(dgOutputs.Data
Dim strSMU as String = e.Item.Cells(1).text
Dim intOutputID as int32 = Ctype(e.Item.FindControl("
Dim intWeed as int32
'Only Save the Weed if the Output is Weeding
if IsWeedingOutput(intOutputI
intWeed = Ctype(e.Item.FindControl("
end if
Dim dblQty as Double = Ctype(e.Item.FindControl("
Dim intOutputUnitID as int32 = Ctype(e.Item.FindControl("
'Check the Compulsory Unit
If CompUnitUpdate_Validate(in
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
.Parameters.add("@ACTIVITY
.Parameters.add("@QTY",ole
.Parameters.add("@UNIT_ID"
.Parameters.add("@WEED_ID"
.Parameters("@SMU_LVL2_COD
.Parameters("@ACTIVITY_ID"
.Parameters("@QTY").value = dblQty
.Parameters("@UNIT_ID").va
If intWeed = nothing then
.Parameters("@WEED_ID").va
else
.Parameters("@WEED_ID").va
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
ASKER
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
Still doesn't answer my original question. But it will have to do.
Thanks
Vicky
In your event for update
do this
CType(e.Item.FindControl("
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.