作为我正在尝试的练习的一部分,它用于父母会议日,我想在那里显示会议桌,小时旁边有一个单选按钮,所以学生可以登录 将自己嵌入他想要的时间。
所以我有一个表格控件,我从数据库 MeetingsTable 动态构建,其中包含以下列:
背后的代码如下所示: (其中一些是希伯来语所以忽略它)
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 = "בחר שעה";
}
页面如下所示: screenshot
问题是,当我点击嵌入式会议按钮时,它将找不到所选的单选按钮,调试后我发现我发送的控件集合有问题,所以我试着发送这个.Controls或者只是使用控件。没有帮助。
虽然我可以使用单选按钮列表控件,并访问所选的值属性,但我无法按照我想要的方式显示它,每个单选按钮位于一个单独的行中,其他列添加到生成表(或其他一些技巧,以产生相同的结果)。
我有所需的所有使用电话:
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;
我真的很沮丧,请赐教!
MyAdoHelper类与此问题无关,因为 它甚至没有进入按钮点击调用中的if语句,所以 我排除了它。
答案 0 :(得分:1)
您可以通过以下方式阅读所选的单选按钮文本:
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.Text; //trinary operator (shortcut of an if expresison)
}
并调用方法
string text = GetRadioValue(form1.Controls, "RadioGroupName"); //where form1 is your container id.
和asp:RadioButton
看起来像
<asp:RadioButton ID="radio1" Text="Text for radio button" runat="server" GroupName="RadioGroupName" />
答案 1 :(得分:0)
调试后,我意识到我没有包含所有radioButton控件的控件所有者或控件容器。 因此使用LINQ是无关紧要的,我使用以下代码来获取Table控件中所选radioButton的radioValue:
public string GetRadioValue() { string radioValue = ""; foreach (TableRow dr in MeetingsTable.Rows) { int countControls = dr.Cells[0].Controls.Count; if (countControls != 0) { RadioButton r = dr.Cells[0].Controls[0] as RadioButton; if (r.Checked) radioValue = r.Text; } } return radioValue; }