as a part of an exercise i'm attempting to do, its used for a parents meetings day, where i want to display a table of meetings, with a radio button next to the hour and so a student can login and embed himself to the time he wants.
so i have a table control which i build dynamically from the database MeetingsTable that has the columns:
- meetingHour
- meetingDate
- meetingPlace
- studentID
the code behind looks like this: (some of it is in hebrew so ignore it)
public string error = "";//Initialize error string
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MultiView1.ActiveViewIndex = 0;
}
string fileName = "ParentsDayDB.accdb";
string selectQuery = "SELECT * FROM Meetings";
DataBindMeetingsTable(fileName, selectQuery);
}
protected void IDButton_Click(object sender, EventArgs e)
{
string studentID = IDTextBox.Text;
string pattern = @"^\d{9}$";
Regex reg = new Regex(pattern);
if (reg.IsMatch(studentID))
{
string fileName = "ParentsDayDB.accdb";
string selectQuery = "SELECT * FROM Students WHERE studentID = '" + studentID + "'";
if (MyAdoHelper.IsExist(fileName, selectQuery))
{
DataTable table = MyAdoHelper.ExecuteDataTable(fileName, selectQuery);
string studentName = (string)table.Rows[0]["studentName"];
string studentClass = (string)table.Rows[0]["studentClass"];
string parentName = (string)table.Rows[0]["parentName"];
Student student = new Student(studentID, studentClass, studentName, parentName);
Session["student"] = student;
if ((bool)table.Rows[0]["isAdmin"] == true)
{
Session["isAdmin"] = "true";
}
MultiView1.ActiveViewIndex = 1;
}
else
{
error = "<hr align=\"right\" width=\"230px\" /> תעדות הזהות לא קיימת";
}
}
else
{
error = "<hr align=\"right\" width=\"440px\" /> אנא הכנס תעודת זהות תקינה בעלת 9 ספרות";
}
}
public void DataBindMeetingsTable(string fileName, string selectQuery)
{
DataTable dt = MyAdoHelper.ExecuteDataTable(fileName, selectQuery);
//הוספת הכותרות
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Text = "שעה";
tr.Cells.Add(tc);
tc = new TableCell();
tc.Text = "תאריך";
tr.Cells.Add(tc);
tc = new TableCell();
tc.Text = "מיקום";
tr.Cells.Add(tc);
tc = new TableCell();
tc.Text = "ת.ז. תלמיד";
tr.Cells.Add(tc);
MeetingsTable.Rows.Add(tr);
//הוספת הנתונים לטבלה
int numCols = dt.Columns.Count;
int radioIndex = 1;
foreach (DataRow dr in dt.Rows)
{
tr = new TableRow();
for (int i = 0; i < numCols; i++)
{
object item = dr.ItemArray[i];
TableCell c = new TableCell();
c.BorderWidth = 1;
if (i == 0)//אם בתא צריך להיות כפתור של שעה
{
RadioButton r = new RadioButton();
r.Text = item.ToString();
r.AutoPostBack = true;
r.GroupName = "radioButtonList";
c.Controls.Add(r);
c.CssClass = "radioButtonCell";
}
else //אם התא הוא רגיל ורק צריך להציג בו מידע
c.Text = item.ToString();
tr.Cells.Add(c);
}
radioIndex++;
MeetingsTable.Rows.Add(tr);
}
}
public string GetRadioValue(ControlCollection controls, string groupName)// return the value of the selected radio button, empty string if nothing is selected.
{
// using Language Intergrated Query (LINQ) in order to find the radio button that is selected in the groupName.
var selectedRadioButton = controls.OfType<RadioButton>().FirstOrDefault(rb => rb.GroupName == groupName && rb.Checked);
return selectedRadioButton == null ? string.Empty : selectedRadioButton.Attributes["Value"]; //trinary operator (shortcut of an if expresison)
}
protected void meetButton_Click(object sender, EventArgs e)
{
//string radioValue = RadioButtonList1.SelectedValue;
//string radioValue = GetRadioValue(form1.Controls, "radioButtonList");
string radioValue = GetRadioValue(form1.Controls, "radioButtonList")
if (!radioValue.Equals("")) // אם נבחרה שעה
{
string hour = radioValue;
Student student = (Student)Session["Student"];
string id = student.StudentID;
if (MeetingHelper.Meet(hour, id))
promptLabel.Text = "הפגישה נקבעה בהצלחה";
else
promptLabel.Text = "השעה שבחרת תפוסה, בחר שעה אחרת";
}
else
promptLabel.Text = "בחר שעה";
}
the page will look like this: screenshot
The problem is when i hit the embed meeting button, it will not find the radio button that is selected, after debugging i have noticed there is a problem with the control collection that i am sending, so I tried sending this.Controls or just using Controls. neither helped.
While i could just use a Radio Button List control, and access the selected value attribute, I couldn't manage to display it the way i wanted to look, each radio button in a separate row with the other columns added to the generated table (or some other trick to make the same result).
I have all the using calls that are needed:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;
I really am frustrated at this, please enlighten me!
The MyAdoHelper class is not really relevant for this problem because it doesn't even get into the if statement in the button click call, so i have excluded it.