M
Morris Neuman
Hi,
I have a GridView1 which contains several fields including 2 template fields
where one edit template contains TextBox3 and the other contains
DropDownList1 with a CustomValidator. They are defined as follows:
<asp:TemplateField HeaderText="Notification Type"
SortExpression="TypeDescription">
<EditItemTemplate>
<aspropDownList ID="DropDownList1" runat="server"
DataSourceID='<%$ AppSettings:MyDataSource2 %>'
DataTextField="TypeDescription"
DataValueField="TypeNumber" SelectedValue='<%# Bind("Type") %>'>
</aspropDownList>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="DropDownList1"
ErrorMessage="CustomValidator"
OnServerValidate =
"CustomValidator1_ServerValidate"></asp:CustomValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("TypeDescription") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Record Number"
SortExpression="RecordNumber">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#
Bind("RecordNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%#
Bind("RecordNumber") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
In the CustomValidator function I want to check that if DropDowList1 has
value of "6" then the TextBox3.text must equal "0" and visa versa, i.e. If
TextBox3 = 0 then DropDownList1 must be set to "6" (Message Light). I tried
the following in the CustomValidator1_ServerValidate script but get null
exception error on TextBox3, though the field is set to 0.
protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
GridView gv1 = GridView1 as GridView;
TextBox tb3 = gv1.FindControl("TextBox3") as TextBox;
string tb3Text = tb3.Text as string;
string ddl1Value = args.Value;
if (ddl1Value == "6")
{
if (tb3.Text == "0")
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
if (tb3.Text == "0")
{
if (ddl1Value == "6")
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
Can you please tell me what I am doing wrong?
I have a GridView1 which contains several fields including 2 template fields
where one edit template contains TextBox3 and the other contains
DropDownList1 with a CustomValidator. They are defined as follows:
<asp:TemplateField HeaderText="Notification Type"
SortExpression="TypeDescription">
<EditItemTemplate>
<aspropDownList ID="DropDownList1" runat="server"
DataSourceID='<%$ AppSettings:MyDataSource2 %>'
DataTextField="TypeDescription"
DataValueField="TypeNumber" SelectedValue='<%# Bind("Type") %>'>
</aspropDownList>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="DropDownList1"
ErrorMessage="CustomValidator"
OnServerValidate =
"CustomValidator1_ServerValidate"></asp:CustomValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("TypeDescription") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Record Number"
SortExpression="RecordNumber">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#
Bind("RecordNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%#
Bind("RecordNumber") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
In the CustomValidator function I want to check that if DropDowList1 has
value of "6" then the TextBox3.text must equal "0" and visa versa, i.e. If
TextBox3 = 0 then DropDownList1 must be set to "6" (Message Light). I tried
the following in the CustomValidator1_ServerValidate script but get null
exception error on TextBox3, though the field is set to 0.
protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
GridView gv1 = GridView1 as GridView;
TextBox tb3 = gv1.FindControl("TextBox3") as TextBox;
string tb3Text = tb3.Text as string;
string ddl1Value = args.Value;
if (ddl1Value == "6")
{
if (tb3.Text == "0")
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
if (tb3.Text == "0")
{
if (ddl1Value == "6")
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
Can you please tell me what I am doing wrong?