In gridview i am using checkbox and textbox in itemtemplate . Now i want
to validate the textbox (Required field validator)when the checkbox is checked .
give some sample codes in asp.net not in javascript...
add requierd field validator like this..
<EditItemTemplate>
<asp:TextBoxID="TextBox2"runat="server"Text='<%# Bind("sal") %>'></asp:TextBox>
<asp:RequiredFieldValidatorControlToValidate="TextBox2"ID="RequiredFieldValidator1"runat="server"ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
</EditItemTemplate>
hope this will help u..
I am using Textbox&checkbox in Itemtemplate and i have try the above code in itemtemplate also
but its not working please check and send the code.............
see this code now
<ItemTemplate>
<asp:PlaceHolderID="placeholder1"runat="server">
</asp:PlaceHolder>
<asp:CheckBoxID="CheckBox1"Text='<%# Bind("id") %>'runat="server"AutoPostBack="true"OnCheckedChanged="CheckBox2_CheckedChanged"/>
<asp:TextBoxID="TextBox3"runat="server"Text='<%# Bind("sal") %>'></asp:TextBox>
</ItemTemplate>
and i added the requierd filed validator on the chk box chk event as
protectedvoid CheckBox2_CheckedChanged(object sender,EventArgs e)
{
CheckBox chk = (CheckBox)sender;if (chk.Checked){
int i=Convert.ToInt32(chk.Text);
TextBox tt = (TextBox)GridView1.Rows[i].FindControl("TextBox3");PlaceHolder pp = (PlaceHolder)GridView1.Rows[i].FindControl("placeholder1");RequiredFieldValidator re =newRequiredFieldValidator();
re.ControlToValidate = tt.ID.ToString();
re.Text ="*";pp.Controls.Add(re);
}
}
'>'>
'>'>try this and let me know if any problem..
I am using checkbox and textbox in seperate itemtemplates.
The above code is not working..................
<asp:CheckBox ID="CHK_T_SET_CONTROL_PERMISSION" runat="server" AutoPostBack="True"
OnCheckedChanged="CHK_T_SET_CONTROL_PERMISSION_CheckedChanged" />
</ItemTemplate>
<HeaderTemplate>
<asp:Label ID="Label1" runat="server" Text="Select"></asp:Label>
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ControlName" HeaderText="Control Name" SortExpression="ControlName">
<ItemStyle Font-Size="Small" />
</asp:BoundField>
<asp:TemplateField HeaderText="Display Control Name">
<ItemTemplate>
<asp:TextBox ID="TXT_T_SET_CTRL_SCR_DISPLAY_SCREEN_NAME" runat="server" Enabled="False"></asp:TextBox>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Control Description">
<ItemTemplate>
<asp:TextBox ID="TXT_T_SET_CTRL_SCR_SCREEN_DESCRIPTION" runat="server" Enabled="False"></asp:TextBox>
</ItemTemplate>
this is my aspx code.........
This is my code..
HTML
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False">
<Columns><asp:TemplateFieldHeaderText="Isvalidate">
<ItemTemplate>
<asp:CheckBoxID="CheckBox1"Text='<%# Bind("id") %>'runat="server"AutoPostBack="true"OnCheckedChanged="CheckBox2_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldHeaderText="sal">
<EditItemTemplate>
<asp:TextBoxID="TextBox2"runat="server"Text='<%# Bind("sal") %>'></asp:TextBox>
<asp:RequiredFieldValidatorControlToValidate="TextBox2"ID="RequiredFieldValidator1"runat="server"ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:PlaceHolderID="placeholder1"runat="server">
</asp:PlaceHolder><asp:TextBoxID="TextBox3"runat="server"Text='<%# Bind("sal") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField></Columns>
</asp:GridView>
and code behind page is
protectedvoid Page_Load(object sender,EventArgs e){
if (!IsPostBack){
GridView1.DataSource = binddate();
GridView1.DataBind();
}
}
privateDataTable binddate(){
DataTable dt =newDataTable();DataColumn dc =newDataColumn("id");
DataColumn dc1 =newDataColumn("sal");dt.Columns.Add(dc);
dt.Columns.Add(dc1);
for (int i = 0; i < 5; i++){
DataRow dr = dt.NewRow();dr["id"] = i;
dr["sal"] = i * 100;dt.Rows.Add(dr);
}
return dt;}
protectedvoid CheckBox2_CheckedChanged(object sender,EventArgs e){
CheckBox chk = (CheckBox)sender;if (chk.Checked){
int i =Convert.ToInt32(chk.Text);
TextBox tt = (TextBox)GridView1.Rows[i].FindControl("TextBox3");PlaceHolder pp = (PlaceHolder)GridView1.Rows[i].FindControl("placeholder1");
RequiredFieldValidator re =newRequiredFieldValidator();re.ControlToValidate = tt.ID.ToString();
re.Text ="*";pp.Controls.Add(re);
}
}
This code will work, but not that sufficent
when you will select the chk box and clear the textbox then it will shows error,
but this is working for only one row at a time, if you are selected two chkboxes, then it is not validating both the textboxes.
but i think this will give u an idea, to validate the textbox..
<asp:CheckBoxID="CheckBox1"Text='<%# Bind("id") %>'runat="server"AutoPostBack="true"OnCheckedChanged="CheckBox2_CheckedChanged"/>
<asp:TextBoxID="TextBox2"runat="server"Text='<%# Bind("sal") %>'></asp:TextBox>
I am not binding text value to checkbox and textbox ..
I try the above code but its not working...............
No comments:
Post a Comment