I have one drop down and it fills the value automatically from database. For that I'm using the following code in the front-end:
public void des_fill()
{
string q = "SELECT -1 AS pick_det_id,'' AS pick_det_name UNION select pick_det_id,pick_det_name from tbl_pick_list_detail where pick_hdr_id=(select pick_hdr_id from tbl_pick_list_header where pick_hdr_name='Designation')";
isopen();
conn.Open();
code cd = new code();
SqlDataReader dr = cd.Reader(q);
ddlDes.DataSource = dr;
ddlDes.DataValueField = "pick_det_id";
ddlDes.DataTextField = "pick_det_name";
ddlDes.DataBind();
conn.Close();
}
I am using this:
SELECT -1 AS pick_det_id,'' AS pick_det_name UNION
for the starting dropdown value as empty.
But by using this required field is not working. When I click the Save button it doesn't asks for required field, and saves the record.
@thiru for required field validator place InitialValue=0 <asp:RequiredFieldValidator ID="RFVQualification" runat="server" ControlToValidate="ddlQualification" CssClass="requiredfieldvalidatorstyle" Display="Static" ErrorMessage="*" InitialValue="0"></asp:RequiredFieldValidator>
here InitalValue refers to the the starting Index of the DropDown
do the following, add text box with ValidationGroup and validate that group on c# level as the following:
for Textbox
<asp:TextBox ID="FirstName" runat="server"
AutoPostBack="false" ValidationGroup="UserInfor"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="FirstName"
ErrorMessage="First Name is requried" ForeColor="Red"
ValidationGroup="UserInfor"> </asp:RequiredFieldValidator>
for dropdownlist, you should add value InitialValue as the following:
<asp:DropDownList runat="server" id="ddAge" ValidationGroup="UserInfor">
<asp:ListItem Value="-1" text="Select a Value">
<asp:ListItem Value="15" text="15 years">
<asp:ListItem Value="20" text="20 years">
<asp:ListItem Value="30" text="30 years">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
runat="server" ControlToValidate="ddAge" InitialValue="-1"
ErrorMessage="Age requried" ForeColor="Red"
ValidationGroup="UserInfor"> </asp:RequiredFieldValidator>
then in the c# level, add the following:
Validate("UserInfor");
if (Page.IsValid) {
lblOutput.Text = "Required field is filled!";
}
else {
lblOutput.Text = "Required field is empty!";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.