I watched the video training about putting some asp.net validator's in an UltraWebGrid and my question is that i can't find a way how to make them work when i use RowEditTemplate to insert/update data in the grid.
Hello,
When you select Edit Template for the first time, UltraWebGrid asks "Do you want to add a textbox and label for each visible column of the row?" if you select Yes then in RowEditTemplate HTML inputs are added but they are not in server mode (no runat="server" attribute is set) - hence they cannot be validated by server controls.
You can just set in <RowEditTemplate > tag of UltraWebGrid some validation and then set in all Textbox runat="server" - this will solve the problem. For example:
//Demo.aspx
....
<RowEditTemplate>
AddressesID
<input runat="server" id="igtbl_TextBox_0_0" columnkey="AddressesID" style="width: 150px;"
type="text">
<br>Address
<input runat="server" id="igtbl_TextBox_0_1" columnkey="Address" style="width: 150px;"
<br>PersonID
<input id="igtbl_TextBox_0_2" runat="server" columnkey="PersonID" style="width: 150px;"
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="igtbl_TextBox_0_1"
ErrorMessage="FieldRequiredValidator"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="igtbl_TextBox_0_2"
ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="igtbl_TextBox_0_0"
</input>
<br></br>
</br>
<br>
<p align="center">
<input id="igtbl_reOkBtn" onclick="igtbl_gRowEditButtonClick(event);" style="width: 50px;"
type="button" value="OK">
<input id="igtbl_reCancelBtn" onclick="igtbl_gRowEditButtonClick(event);" style="width: 50px;"
type="button" value="Cancel"> </input>
</p>
</RowEditTemplate>
//Demo.aspx.cs
...
using Infragistics.WebUI.UltraWebGrid;using System.Drawing;
protected void Page_Load(object sender, EventArgs e) {
UltraWebGrid1.Columns.Insert(0, "Edit"); UltraWebGrid1.Columns[0].Type = ColumnType.Button; UltraWebGrid1.Columns[0].Width = 50; UltraWebGrid1.Columns[0].CellButtonStyle.BackColor = Color.FromArgb(204, 0, 0); UltraWebGrid1.Columns[0].CellButtonStyle.BorderWidth = 1; UltraWebGrid1.Columns[0].CellStyle.BackColor = Color.FromArgb(51, 51, 51); UltraWebGrid1.Columns[0].CellButtonStyle.BorderColor = Color.FromArgb(204, 0, 0); UltraWebGrid1.DisplayLayout.Bands[0].Columns[0].Header.Caption = "Edit";
}
You are correct, what I posted in my previous answer shows only how to enable validation, but the OK button still fires postback, even if the page is not valid. You can use the IsValid property (Indicates whether or not the validation check made by the individual validating object has passed. You can manually alter this value after validation has taken place.) in JavaScript function postBackIfValid.
<input id="igtbl_reOkBtn" onclick="postBackIfValid(event)" style="width: 50px;" type="button" value="OK">
<script type="text/javascript">
function postBackIfValid(e)
{
if (Page_IsValid)
igtbl_gRowEditButtonClick(e);
</script>
Here you can see more information about ASP.NET Validation:
http://msdn.microsoft.com/en-us/library/aa479045.aspx
Hope this helps.
Hi Ivan
I have been trying to do this solution with setting the runat="server" on text boxes inside the editrowtemplate. However I cannot access these fields from the codebehind? Do you know why, is this not possible?
if you want to validate something custom, use javascript methods.
If you want to access your text boxes, in case if their are <input .... type="text" /> you can take the values from "Request.Form[Control Id]".
In case if their are runat="server" you can access with this:
TextBox myTextBox = (TextBox)UltraWebGrid1.Bands[0].RowEditItem.FindControl(Control Id);
or generally
Control myControl= (Control )UltraWebGrid1.Bands[0].RowEditItem.FindControl(Control Id);
thank you very much. it works!