473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 6677
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

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

Similar topics

1
by: Brian | last post by:
Hello all... I have a page that will be performing a search. The search consists of 3 radio button options. The first 2 will search the entire web through google and the site as indexed by...
1
by: sman | last post by:
Hi, I recently read this article on About.com on how to create required fields for a form: http://javascript.about.com/library/scripts/blformvalidate.htm Everything works great except that there...
2
by: obsidian8 | last post by:
Hi All, I have looked around for an answer to this question, but haven't found one as of yet. I'm trying to use javascript to dynamically create raido buttons. I am able to create them easily...
3
by: Ken Varn | last post by:
This is probably a simple question, but I am new to this so bear with me. I have a ASP.NET form that has a checkbox and a 2 radio buttons. When the checkbox is checked, the 2 radio buttons are...
1
by: Karthick Kumar | last post by:
Hi, I have the following code which displays all the images from a specific folder with a Radio button in it: Dim objFile i = 1 For Each objFile In objFolder.Files If (i = 1) Then...
2
by: James P. | last post by:
Help, I need to display radio buttons on a form. The data is from SQL table: each row in each table is displayed as a radio button. I have multiple SQL tables so I understand I need to put...
6
by: Blinky | last post by:
Hi all, I have a dynamically generated page that can have 1 or more radio buttons. I am using javascript with onsubmit in the form statement to make sure a radio button is selected before...
7
by: moksha | last post by:
Hi, I am new to javascript and i am facing a problem in coding. plz help me out. I am using javascript for dynamically creating a table row which contains text boxes and radio...
0
by: swapnil1987 | last post by:
hello friends i am trying to find solution for this problem if u know something then please tell i am creating web application that runs on black berry. here i need to create selection list...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.