472,114 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,114 software developers and data experts.

validating dynamically created radio buttons using Response.write

hi all,
In my web page i have created radio buttons dynamically on to the page .....following is my code
Expand|Select|Wrap|Line Numbers
  1.          string Course, Semester, Section;
  2.     int rowsCount;
  3.     string con = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
  4.  
  5.     protected void Page_Load(object sender, EventArgs e)
  6.     {
  7.         try
  8.         {
  9.             SqlConnection objCon;
  10.             SqlDataAdapter objDA;
  11.             Course = (string)Session["Course"];
  12.             Semester = (string)Session["Semester"];
  13.             Section = (string)Session["Section"];
  14.             if (!IsPostBack)
  15.             {
  16.                 string strFeedBackDetails = "";
  17.                 Response.Write("<i>COURSE</i>  <b>  :" + Course + " </b>  |   ");
  18.                 Response.Write("<i>SEMESTER</i> <b>   :" + Semester + "</b>   |   ");
  19.                 Response.Write("<i>SECTION </i><b>  :" + Section + "</b><br/><br/><br/><br/>");
  20.                 string query = "select SubjectCode from tblCollegeSubject where CourseName = '" + Course + "' and Semester = '" + Semester + "' union select miscSubject from tblCollegeMisc";
  21.                 objCon = new SqlConnection(con);
  22.                 objDA = new SqlDataAdapter(query, objCon);
  23.                 DataTable objDT = new DataTable("tblSubjectCode");
  24.                 objDA.Fill(objDT);
  25.                 rowsCount = objDT.Rows.Count;
  26.                 strFeedBackDetails += "<div align=\"center\"><table align=\"center\" cellpadding=\"5\" cellspacing=\"2\" frame=\"border\" bordercolordark=\"#000000\">" +
  27.                    "<tr  bgcolor=Gray><th><b>SUBJECT</b></th>" +
  28.                    "<th><b>EXCELLENT-1</b></th><th><b>VERY GOOD-2</b></th>" +
  29.                    "<th><b>GOOD-3</b></th><th><b>AVERAGE-4</b></th>" +
  30.                    "<th><b>POOR-5</b></th><th><b>COMMENTS</b></th></tr>";
  31.  
  32.                 for (int count = 0; count < rowsCount; count++)
  33.                 {
  34.                     strFeedBackDetails += "<tr><td style=\"font-style: italic;\"><input type=text name=\"subject\" readonly=\"readonly\" id=\"txtSubject" + count.ToString() + "\" value='" + objDT.Rows[count]["SubjectCode"].ToString() + "' style=\"display: block; visibility: visible;\"></td>";
  35.                     strFeedBackDetails += "<td><input type=radio id='rbgrade1" + count.ToString() + "' value=\"A\" name='grade" + count.ToString() + "' /></td>";
  36.                     strFeedBackDetails += "<td><input type=radio id='rbgrade2" + count.ToString() + "' value=\"B\" name='grade" + count.ToString() + "' /></td>";
  37.                     strFeedBackDetails += "<td><input type=radio id='rbgrade3" + count.ToString() + "' value=\"C\" name='grade" + count.ToString() + "' /></td>";
  38.                     strFeedBackDetails += "<td><input type=radio id='rbgrade4" + count.ToString() + "' value=\"D\" name='grade" + count.ToString() + "' /></td>";
  39.                     strFeedBackDetails += "<td><input type=radio id='rbgrade5" + count.ToString() + "' value=\"E\" name='grade" + count.ToString() + "' /></td>";
  40.                     strFeedBackDetails += "<td><input type=text id='txtComments" + count.ToString() + "' name=\"txtComments\" maxlength=200 /> </td>";
  41.                     strFeedBackDetails += "</tr>";
  42.                 }
  43.                 strFeedBackDetails += "<tr><td colspan=7 align=center><input type=\"submit\" id=\"btnSave\" value=\"Save\" language=\"javascript\" onClick=\"return Save()\"  /></td>";
  44.                 strFeedBackDetails += "<td><input type=hidden id=\"hidCount\" name=\"hidCount\" value='" + objDT.Rows.Count.ToString() + "'/></td></tr>";
  45.                 strFeedBackDetails += "</table></div>";
  46.                 Page.Form.Controls.Add(new LiteralControl(strFeedBackDetails));
  47.             }
  48.             else
  49.             {
  50.                 string lstrSubject = null;
  51.                 string lstrComments = null;
  52.                 string[] larrSubject = null;
  53.                 string[] larrComments = null;
  54.                 int lrowsCount = Convert.ToInt16(Request["hidCount"]);
  55.                 lstrSubject += (Request["subject"]);
  56.                 lstrComments += (Request["txtComments"]);
  57.                 larrSubject = lstrSubject.Split(',');
  58.                 //showControlids();                       
  59.                 larrComments = lstrComments.Split(',');
  60.                 string[] lgrade = new string[lrowsCount];
  61.                 objCon = new SqlConnection(con);
  62.                 SqlCommand objCmd = new SqlCommand("uspInsFeedBack", objCon);
  63.                 objCmd.CommandType = CommandType.StoredProcedure;
  64.                 objCmd.Parameters.Add("@FeedBackDate", SqlDbType.DateTime).Value = System.DateTime.Now.ToString();
  65.                 objCmd.Parameters.Add("@CourseName", SqlDbType.Char).Value = Course;
  66.                 objCmd.Parameters.Add("@Semester", SqlDbType.Char).Value = Semester;
  67.                 objCmd.Parameters.Add("@SectionName", SqlDbType.Char).Value = Section;
  68.                 SqlParameter RetVal = objCmd.Parameters.Add("@Refer", SqlDbType.Int);
  69.                 RetVal.Direction = ParameterDirection.Output;
  70.                 try
  71.                 {
  72.                     objCon.Open();
  73.                     objCmd.ExecuteNonQuery();
  74.                     for (int count = 0; count < lrowsCount; count++)
  75.                     {
  76.                         lgrade[count] = (Request["grade" + count.ToString()]);                        
  77.                         string insertSQL;
  78.                         insertSQL = "INSERT INTO tblCollegeFeedBackForm(";
  79.                         insertSQL += "SubjectCode, FeedBackId, Grade, Comments)";
  80.                         insertSQL += "VALUES (";
  81.                         insertSQL += "@SubjectCode, @FeedBackId, @Grade, @Comments )";
  82.                         SqlCommand objCmd1 = new SqlCommand(insertSQL, objCon);
  83.                         objCmd1.Parameters.Add(new SqlParameter("@SubjectCode", SqlDbType.Char));
  84.                         objCmd1.Parameters["@SubjectCode"].Value = larrSubject[count];
  85.                         objCmd1.Parameters.Add(new SqlParameter("@FeedBackId", SqlDbType.Int));
  86.                         objCmd1.Parameters["@FeedBackId"].Value = RetVal.Value;
  87.                         objCmd1.Parameters.Add(new SqlParameter("@Grade", SqlDbType.Char));
  88.                         objCmd1.Parameters["@Grade"].Value = lgrade[count];
  89.                         objCmd1.Parameters.Add(new SqlParameter("@Comments", SqlDbType.VarChar, 200));
  90.                         objCmd1.Parameters["@Comments"].Value = larrComments[count];
  91.                         objCmd1.ExecuteNonQuery();                      
  92.                     }
  93.                 }
  94.                 catch (SqlException er)
  95.                 {
  96.                     Response.Write(er.Message);
  97.                 }
  98.                 finally
  99.                 {
  100.                     objCon.Close();
  101.                 }
  102.             }
  103.         }
  104.         catch (Exception er)
  105.         {
  106.             Response.Write(er.Message);
  107.         }
  108.     }
  109.  
And now i want to validate the page before it get submited...i want the user to check radio buttons in each row.. i validated using javascript as follows
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2. <!--
  3.  
  4. function Save() 
  5. {
  6.     var countId=getElementByName('hidCount');
  7.     var Count=countId.value;
  8.     var check='check';
  9.     for(i=0;i<count;i++)
  10.     {
  11.         var grade[i]=getElementByName('grade'+i);
  12.         for(j=0;j<grade[j].length;i++)
  13.         {
  14.             if(grade[j].checked==true)
  15.             {
  16.                 check=grade[i].value();
  17.             }
  18.          }
  19.          if(check=='check')
  20.          {
  21.             alert('PLZ Grade The Subject');
  22.             return false;
  23.          }           
  24.          else
  25.             return true;
  26.      }
  27. }
  28.  
i cold not able to validate becoz i could not get the the value of hidden variable and radio button....can any one plz solve by problem...


thanks
Feb 27 '09 #1
5 6560
Frinavale
9,735 Expert Mod 8TB
The first thing I'm going to say is that Response.Write should not be used in your C# code.

Why would I say such a thing?

Glad you asked, it writes the content "somewhere" in the output content stream.

What on earth does that mean?

Well it means that when you use Response.Write(), the string is written anywhere in the content stream sent to the browser. This usually results in the content being placed somewhere before the <html> tag, or somewhere between the <head> and <body> tags....

If you don't believe me, run your application, right click on it and click "view code"...

Do you see it?

Your HTML isn't valid is it?

Response.Write should only be called from the asp code.
Not used in the C# code.

This is probably why you're having problems.

I strongly recommend you use ASP.NET controls since you are developing an ASP.NET application.

In your case, I would recommend looking up the RadioButtonList Control, Repeaters .....and Labels
Feb 27 '09 #2
Hi satyabhaskar,

frinvale sir is right you can use response.write.

Expand|Select|Wrap|Line Numbers
  1.       Page.Form.Controls.Add(new LiteralControl(strFeedBackDetails)); 
insted this code you can write this way
Expand|Select|Wrap|Line Numbers
  1. Response.Write(strFeedBackDetails);
but if you want to check validation you have to make function in javascript;
whenever you find control in javascript you use this line

Expand|Select|Wrap|Line Numbers
  1. document.getElementByName 
  2. //(or)
  3. document.getElemenetById
Feb 28 '09 #3
@Frinavale
hi boss...
yes boss ur right.... im sorry the title which i displayed is wrong. actually i did not used response.write... i find the same fault which u said and i changed my code accordingly.... i used
Expand|Select|Wrap|Line Numbers
  1.  Page.Form.Controls.Add(new LiteralControl(strFeedBackDetails));

and nw my problem is i couldnt get the control's id when iam validating them client side using java script.........i don't need to validate all the controls its enough if i can validate radio buttons in each row....
Feb 28 '09 #4
hey boss i got the solution.... just want to share..... my code is as follows...
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2. function valid(e)
  3. {
  4.     for(var i=0;i<e.length;i++)
  5.     {
  6.         if(e[i].type=='radio')
  7.         {
  8.             var r=e[e[i].name], check=true;
  9.             for(var j=0;j<r.length;j++)
  10.             {
  11.                 if(r[j].checked)
  12.                     check=false;
  13.             }
  14.             if(check)
  15.             {
  16.                 alert('Please Grade All The Subjects');
  17.                 return false;
  18.             }
  19.             else
  20.              return true;
  21.         }
  22.     }
  23. }
  24. </script>
  25. </head>
  26. <body>
  27.     <form id="form1" method="post" name="form1" runat="server" onsubmit="return valid(this.elements)" >
  28.     </form>
  29. </body>
  30. </html>
  31.  
Mar 1 '09 #5
Frinavale
9,735 Expert Mod 8TB
I'm glad you found your solution :)

For your information, the reason why you couldn't get the control's ID for validation using JavaScript is because Literal Controls just print pure text. This text does not have an ID associated with it...

If you had used a Label, your text would have been rendered in <span> tags, which has an ID that will let you access it with JavaScript.

In your case, you want to validate a RadioButtonList, which has an ID...

Your solution works fine for your purposes though: loop through all of the elements in the page and find a Radio Button List....then validate.

For more information check out how to use JavaScript in ASP.NET
Mar 2 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by obsidian8 | last post: by
3 posts views Thread by Ken Varn | last post: by
1 post views Thread by Karthick Kumar | last post: by
6 posts views Thread by Blinky | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.