CustomValidator against two controls in EditItemTemplate on a DataGrid?

G

Gene McCrory

Is it possible to have a CustomValidator against two controls in an
EditItemTemplate on a DataGrid?

Case: Have a business rule that states if ATextBox starts with
"ACertainValue" then BTextBox value is required. I added a CustomValidator
to the EditItemTemplate for the column containing BTextBox. I did not set
the ControlToValidate, but added a Method for the OnServerValidate.

The OnServerValidate is called correctly, but I don't know how to reference
ATextBox.Text and BTextBox.Text from the method assigned to the
OnServerValidate event. Is there anyway to obtain a reference to these
controls that are embedded in the EditItemTemplate of a DataGrid from within
the event handler method of OnServerValidate.

Best regards,

Gene McCrory
 
Y

Yan-Hong Huang[MSFT]

Hello Gene,

Thanks for posting in the group.

Based on my understand, now the question is: In your datagrid, there are
two controls in an EditItem Template. Is there any way for a method in code
behind file to access these type of controls? Please feel free to post here
if I have any misunderstandings.

Generally speaking, we could use the FindControl method to create a
reference to the control, whenever we know the ID of the control whose
value we want.

The following example shows the HTML syntax for a template column that
displays Boolean data. Both the ItemTemplate and EditItemTemplate use a
check box to display the value. In the ItemTemplate, the check box is
disabled so that users do not think they can check it. In the
EditItemTemplate, the check box is enabled.

<Columns>
<asp:TemplateColumn HeaderText="Discontinued">
<ItemTemplate>
<asp:Checkbox runat="server" enabled= false name ="Checkbox2"
ID="Checkbox2"
Checked = '<%# DataBinder.Eval(Container,
"DataItem.Discontinued") %>' >
</asp:Checkbox>
</ItemTemplate>
<EditItemTemplate>
<asp:Checkbox
runat="server" name ="Checkbox2" ID="Checkbox2"
Checked = '<%# DataBinder.Eval(Container,
"DataItem.Discontinued") %>' >
</asp:Checkbox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>

// C#
CheckBox cb;
cb = (CheckBox) e.Item.FindControl("CheckBox2");

In Visual Studio, you can use the grid's Property builder to create the
template column and use the template editor to specify the layout. In the
Columns tab of the Properties window page for the grid, select the column
and at the bottom, click Convert this column into a Template Column. Close
the Properties window, right-click the grid, and choose Edit Template. You
can then drag controls from the Toolbox into the template and add static
text.

Does that answer your question?

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Gene McCrory

Yan-Hong,

Thanks for your response but this does not answer my question.

I should've included the fact that the two textbox controls are in different
columns (separate EditItemTemplates).

Also, you cannot use e.Item.FindControl("ATextBox) and etc... within the
OnServerValidate event handler of the CustomValidator. The OnServerValidate
does not contain a parameter that contains this method.

Any additional insight into this issue is greatly appreciated.

Best regards,

Gene McCrory
 
Y

Yan-Hong Huang[MSFT]

Hello Gene,

Currently I am researching it and will back here with more information as
soon as possible. If you have any more concerns on it, please feel free to
post here.

Thanks very much.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Gene McCrory

I've written a solution that works for my requirement. But It does not
answer question in regards to getting reference to controls in two different
EditItemTemplates from the OnServerValidate of a CustomValidator.

Following is a description of a solution to the original requirement in case
someone else is having a issue similar to this one:
Handle requirement in datagrid's UpdateCommand event handler by
obtaining a reference to controls in EditItemTemplate via
e.Items.FindControl["ATextBox"]. Get a reference to both. Perform business
logic with if condition. If error call DisplayError().


private void DisplayError(string errorMessage)
{
CustomValidator cv = new CustomValidator();
cv.ErrorMessage = errorMessage;
cv.Text = string.Empty;
cv.ServerValidate += new ServerValidateEventHandler(cv_ServerValidate);
cv.Enabled = true;
cv.Display = ValidatorDisplay.None;
cvPlaceHolder.Controls.Add(cv);
Page.Validate();
}

private void cv_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
}


cvPlaceHolder is a placeholder that I placed next to the ValidationSummary
for the page.

This handles my requirement, but doesn't place an "*" by any control.

Best regards,

Gene
 
Y

Yan-Hong Huang[MSFT]

Hello Gene,

Thanks for sharing your experience in the community. Sorry for the late
response here.

In the OnServerValidate event handler, since we know the ID of datagrid, I
think we could use its reference directly, for an example,
DataGrid1.SelectedItem.Cells(0).FindControl("MyControl"). In this way, we
could find the correct control and get its value. Could you please test it
on your side and let me know if it works?

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top