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.
Yep, that is a great idea too, CustomValidators may be needed for more complex validation rules. The EnableClientSCript property may also be used to customize the server-side / client-side functionality.
Thanks for sharing this idea with the community - certainly will be appreciated by other people as well.
Do not forget that you can use also CusomValidators control.CustomValidators have two validate methods:
OnClient method runs first.With OnClient validate event u can take the control that you want using document.getElementById() if its a normal html control.If it runat="server" use document.getElementById("<%= controlId.ClientID %>") or document.getElementById("<%= controlId.UniqueID%>").This is because it's inside the grid and the control when reders probably with take a client id such as "WebGrid_RowEditTemplate_controlId"
There is no standalone textarea control in the ASP.NET framework - but you can use TextBox for that - just make sure you set TextMode to MultiLine, e.g.
<asp:TextBox TextMode="MultiLine" Columns="40" Rows="40" />
Using RangeValidator is possible, but it really depends on the types of values entered in the textbox and could be tricky to achieve. I suggest going through the following MSDN article for details:
http://msdn.microsoft.com/en-us/library/f70d09xt(VS.71).aspx
Can you only do this with text inputs? How about textarea?
Can you only validate using RequiredFieldValidator? I can't seem to get RangeValidator to work.
thank you very much. it works!