473,382 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

CustomValidator against two controls in EditItemTemplate on a DataGrid?

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
Nov 18 '05 #1
5 3143
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.

Nov 18 '05 #2
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


"Yan-Hong Huang[MSFT]" <yh*****@online.microsoft.com> wrote in message
news:e6**************@cpmsftngxa07.phx.gbl...
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.

Nov 18 '05 #3
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.

Nov 18 '05 #4
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

"Yan-Hong Huang[MSFT]" <yh*****@online.microsoft.com> wrote in message
news:kX**************@cpmsftngxa07.phx.gbl...
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.

Nov 18 '05 #5
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("MyCon trol"). 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.

Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave | last post by:
I have added a custom validator to the EditItemTemplate of a DataGrid as follows: <EditItemTemplate> <asp:TextBox id=txtWorkDate CssClass="ResultsDim" runat="server" Width="100%" Text='<%#...
3
by: philipl | last post by:
hi, i have a textbox in my edititemtemplate in my datagrid. I have created a customvalidator for it, when i update the row the appropiate function gets called to customvalidate but the problem...
4
by: starwiz | last post by:
I'm trying to use the DataGrid's editing with a DropDownList. I've tried using every code sample I've seen and none of them seem to be able to solve my problem. When I call...
1
by: Vi | last post by:
Hi, I have two pairs of a DropDownList and TextBox Controls and for each pair I have a CustomValidator that makes sure that at least one of the two (DropDownList or TextList) has a value. When I...
2
by: 2obvious | last post by:
Below is a cut-and-paste code example that runs. It demonstrates some results which confuse me. It uses a DataGrid to make a table with 12 rows, each containing a TextBox and a CustomValidator....
0
by: ghafranabbas | last post by:
This is how you use the customvalidator control in any INamingContainer interface control (Datagrid, DataList, DataRepeater, etc). 1. In the ItemTemplate, place your customvalidator 2. Set the...
3
by: jmclej | last post by:
Here is what I have : <asp:templatecolumn HeaderText="HrBegin"> <itemtemplate> <asp:Label id="lblHrBegin" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "HrBegin") %>'...
0
by: rn5a | last post by:
Suppose I have the following DataGrid: <asp:DataGrid ID="dgUsers" OnUpdateCommand="UpdateDG" runat="server"> <Columns> <TemplateColumn HeaderText="NAME"> <ItemTemplate> <asp:Label...
2
by: Steve Hershoff | last post by:
Hi everyone, I have a DataGrid with several TemplateColumns. One of these columns has an EditItemTemplate that contains an ASP.Net DropDownList. I'm catching this DropDownList's...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.