0

hi, i m asha bhatt

i have create check box list dynamically and i want to do the validation for that its checked or not

this is my .aspx.cs code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Diagnostics;

public partial class Default2 : System.Web.UI.Page
{
    string scon = "Data Source=RBWORKSTATION2\\SQLEXPRESS;Initial Catalog=onlineexamdb;Integrated Security=True;MultipleActiveResultSets=true";
    SqlConnection con;
    SqlDataAdapter da;
    SqlCommand cmd;
    DataSet ds;
    static DataTable dt;
    SqlDataReader readsid, readalltest, readallinterview, readallquestion;
    IDataReader readqus;
    public string qus, sid, alltest, allinterview , score;
    int[] allquestion = new int[3];
    static int count = 0;
    RadioButtonList rdlist = new RadioButtonList();
    CheckBoxList chklist = new CheckBoxList();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            con = new SqlConnection(scon);
            con.Open();
            cmd = new SqlCommand("select SubID from TestInfo where TestID = (select MAX(TestID) from TestInfo);", con);
            readsid = cmd.ExecuteReader();
            while (readsid.Read())
            {
                sid = readsid[0].ToString();
                //Response.Write(sid);
            }
            getquestion();
            NextQuestionButton_Click(sender, e);
            rdlist_TextChanged(sender, e);
            chklist_TextChanged(sender, e);

        }
     }

    public string getquestion()
    {
        con = new SqlConnection(scon);
        con.Open();
        if (!IsPostBack)
        {
            cmd = new SqlCommand("SELECT  QusID ,Question,Option1 ,Option2,Option3,Option4 ,NumberofCorrectAnswers ,CorrectOptions from Questions INNER JOIN SubjectInfo ON  Questions.SubID = SubjectInfo.SubID  where Questions.SubID=" + sid + "", con);
            dt = new DataTable();
            readqus = cmd.ExecuteReader();
            if (readqus != null)
            {
                dt.Load(readqus);
            }
        }
        con.Close();
        return "true";
    }

    public string nextquestion()
    {
        if (count < dt.Rows.Count)
        {
            QuestionLabel.Text = dt.Rows[count][1].ToString();
            createcheckbox();
            count = count + 1;

        }
        else
        {
            Response.Write("<script>alert('Your Test is Finished')</script>");
        }
        return "true";
    }


    public void createcheckbox()
    {
        if (Convert.ToInt32(dt.Rows[count][6].ToString()) > 1)
        {
            CheckBoxList chklist = new CheckBoxList();
            chklist.ID = "chklist";
            chklist.Items.Add(new ListItem(dt.Rows[count][2].ToString()));
            chklist.Items.Add(new ListItem(dt.Rows[count][3].ToString()));
            chklist.Items.Add(new ListItem(dt.Rows[count][4].ToString()));
            if (dt.Rows[count][5].ToString() == "")
            {
                //Response.Write("check box 5 blank");
                Label4.Visible = false;
            }
            else
            {
                chklist.Items.Add(new ListItem(dt.Rows[count][5].ToString()));
                Label4.Visible = true;
            }

            chklist.RepeatDirection = RepeatDirection.Vertical;
            chklist.TextChanged += new EventHandler(chklist_TextChanged);
            Panel1.Controls.Add(chklist);
        }
        else
        {
            
            rdlist.Items.Add(new ListItem(dt.Rows[count][2].ToString()));
            rdlist.Items.Add(new ListItem(dt.Rows[count][3].ToString()));
            rdlist.Items.Add(new ListItem(dt.Rows[count][4].ToString()));
            if (dt.Rows[count][5].ToString() == "")
            {
               // Response.Write("radio button 5 is blaink");
                Label4.Visible = false;
            }
            else
            {
                rdlist.Items.Add(new ListItem(dt.Rows[count][5].ToString()));
                Label4.Visible = true;
            }
            rdlist.RepeatDirection = RepeatDirection.Vertical;
            rdlist.TextChanged += new EventHandler(rdlist_TextChanged);
            Panel1.Controls.Add(rdlist);
        }
    }

  
    void rdlist_TextChanged(object sender, EventArgs e)
    {
       // Response.Write("Radiobutton create");
    }

    void chklist_TextChanged(object sender, EventArgs e)
    {
       // Response.Write("CheckBox create");
    }

    protected void NextQuestionButton_Click(object sender, EventArgs e)
    {
        getquestion();
        nextquestion();

        
      
    }
    protected void PreButton_Click(object sender, EventArgs e)
    {
        try
        {
            if (count < dt.Rows.Count)
            {
                QuestionLabel.Text = dt.Rows[--count][1].ToString();
                //  getquestion();
                createcheckbox();
                // count = count - 1;
            }
            else
            {
                Response.Write("<script>alert('Your Test is Finished')</script>");
            }
        }
        catch (IndexOutOfRangeException ex)
        {

            Response.Write(ex.Message);
        }
      
    }
    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(scon);
        con.Open();
        nextquestion();
       
        cmd = new SqlCommand("select * from TestInfo", con);
        readalltest = cmd.ExecuteReader();
        while (readalltest.Read())
        {
            alltest = readalltest[0].ToString();
        }
        cmd = new SqlCommand("select * from InterviewSet where InterviewID = (select MAX(InterviewID) from InterviewSet)", con);
        readallinterview = cmd.ExecuteReader();
        while (readallinterview.Read())
        {
            allinterview = readallinterview[0].ToString();
        }

        cmd = new SqlCommand("select QusID , CorrectOptions from Questions where Question = '" + QuestionLabel.Text + "'", con);
        readallquestion = cmd.ExecuteReader();
        while (readallquestion.Read())
        {
            allquestion[0] = Convert.ToInt32(readallquestion[0]);
        }
}

and this is my .aspx code

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<html>
    <head>
    <title></title>
    <script type="text/javascript" language="javascript">
        
        function validate()
        {
//            for(int i = 0; i< rdchk.length;i++)
//            {
             if (!document.getElementById("<%=rdchk.ClientID%>").checked)
             {
                alert("Select the Time Test");
                document.getElementById("<%=rdchk.ClientID %>").focus();
                return false;
             }
             else
             {
                alert("nothing");
             }
//           }
            //return false;
        }
                        
          
//        allInputs = document.getElementsByTagName("input");
//        //var last = "NameUnlikelyToBeUsedAsAnElementName";

//        // walk through the array
//        for (i = 0; i< allInputs.length; i++)
//        {
//            var input = allInputs[i];
//            if (input.name == last) continue; // if this object name is the same as the last checked radio, go to next iteration


//        // checks to see if any  one of  similarly named radiobuttons is checked 
//            else if (input.type == "radio" )
//            {    
//                last = input.name;  
//                var radios = document.getElementsByName(input.name);
//                var radioSelected=false;

//         //iterate over question options
//                for (j=0; j < radios.length; j++)
//                {
//                    if(radios[j].checked)
//                    {
//                       radioSelected=true;
//                       break; // Found it, proceed to next question     
//                    }
//                    else
//                    {
//                         alert("You have not select any answer for this question");
//                    }
//                }
//                if (!radioSelected) // no option selected
//                {       // warn user, focus question
//                    alert("You did not answer question " + input.id.substring(0,input.id.length-1));
//                    input.focus();
//                    return false;
//                }                                  
//            }
//        }

//return true;
//}
//}
       
</script>       
</head>
<body>
   <asp:Button ID="SubmitButton" runat="server" Text=" Submit " Width="74px" 
                        onclick="SubmitButton_Click"  OnClientClick = "javascript: return validate()"  />
</body>
    </html>
</asp:Content>

plz tell me how can i do it.....................

Edited by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

2
Contributors
1
Reply
4
Views
7 Years
Discussion Span
Last Post by __avd
0

@ashab27

Please read the rules before posting again, in particular the 'keep it organized' one - For easy readability, always wrap programming code within posts in (code) (code blocks) and (icode) (inline code) tags. If you hit 10 infraction points your account gets an automatic ban, so it is worth obeying the rules if you want to continue to benefit from DaniWeb help.

Edited by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.