如何validationRadioButtonList是否在C#中检查/选择它?
我有一个从数据库填充的RadioButtonList,用户至少需要检查/选择一个RadioButtons。 我怎样才能确保他们检查/选择RadioButton? 我尝试了很多不同的方法,但没有运气,这是我的代码。 我也试图提醒他们uncheck radiobutton。
我要做的是validationRadioButtonList是否被选中/选中。
if (cblstatus.SelectedItem.Value == "1") { //Create Category con.Open(); SqlCommand departament = new SqlCommand("insert into categories(ca_name, ca_number, ca_status, ca_department, ca_date, ca_time, ca_user) " + "values('" + category.Text + "', '" + number.Text + "', 'Y', '" + catdepartment.SelectedValue + "', '" + date + "', '" + time + "', '" + user + "')", con); departament.ExecuteNonQuery(); con.Close(); } else if (cblstatus.SelectedItem.Value == "2") { //Create Category con.Open(); SqlCommand departament = new SqlCommand("insert into categories(ca_name, ca_number, ca_status, ca_department, ca_date, ca_time, ca_user) " + "values('" + category.Text + "', '" + number.Text + "', 'N', '" + catdepartment.SelectedValue + "', '" + date + "', '" + time + "', '" + user + "')", con); departament.ExecuteNonQuery(); con.Close(); } else if (cblstatus. == null) { alert.InnerHtml = "
" + "" + "setTimeout(function () { $('#success').fadeOut(); }, 2000);" + ""; }
而不是在后面的代码上使用asp.net必需的字段validation器:
这是一个例子:
*
在你背后的代码上可以创建一个空白div,然后在出错时添加文本,你可以使用jquery change函数进一步操作它:
if (gender.SelectedItem.Value == "Male") { //do stuff } else if (gender.SelectedItem.Value == "Female") { //do stuff } else { errorDiv.InnerText = "Error Messeage"; }
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator.aspx
我们可以使用普通的RequiredFieldValidation而不定义InitialValue属性
Yes No
它对我有用,没有复杂的思考
可以使用这种方法,但完全代码背后
public bool ValidateRadioButtonList() { bool rbchecked= false; for (int i = 0; i < RadioButtonList1.Items.Count - 1; i++) { if (RadioButtonList1.Items[i].Selected==true) { rbchecked = true; } } if (rbchecked == true) { lblError.Text = string.Empty; return true; } else { lblError.Text = "Please select RadioButton!"; return false; } }
.aspx:
.aspx.cs:
在ViewState中添加/插入值 (例如,通过单击按钮):
protected void btnSend_Click(object sender, EventArgs e) { foreach (ListItem r in rbAny.Items) if (r.Selected) ViewState["anyValue"] = r.Text; }
从ViewState读取值:
protected void btnShow_Click(object sender, EventArgs e) { StringBuilder str = new StringBuilder(); str.Append("anyValue: " + ViewState["anyValue"]); someLabel.Text = str.ToString(); }
Imports System.IO Imports System.Text Imports System Public Class WebForm1 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub 'Protected Sub Calendar1_SelectionChanged(sender As Object, e As EventArgs) ' TextBox1.Text = Calendar1.SelectedDate.ToShortDateString() ' Calendar1.Visible = False 'End Sub 'Protected Sub LinkButton1_Click(sender As Object, e As EventArgs) ' 'Calendar1.Visible = True 'End Sub 'Protected Sub LinkButton2_Click(sender As Object, e As EventArgs) ' Calendar2.Visible = True 'End Sub 'Protected Sub Calendar2_SelectionChanged(sender As Object, e As EventArgs) ' TextBox2.Text = Calendar2.SelectedDate.ToShortDateString() ' Calendar2.Visible = False 'End Sub Public Sub DownloadCSV() Dim csvPath1 As String = "D:\CSVFile1.csv" 'Download and read all Text within the uploaded Text file. Dim csvContentStr1 As String = File.ReadAllText(csvPath1) Dim split As String() = csvContentStr1.Split(","c) 'Here you can see the output as CSV content. 'Response.Write(csvContentStr) End Sub Public Sub UploadCSV() Dim csvContent As New StringBuilder() DownloadCSV() Dim dt1 As DateTime = Convert.ToDateTime(TextBox1.Text) Dim dt2 As DateTime = Convert.ToDateTime(TextBox2.Text) Dim dt3 As TimeSpan = dt2.Subtract(dt1) Dim NoOfDays As Integer = dt3.Days + 1 'TextBox3.Text = NoOfDays.ToString Dim index As DateTime = Convert.ToDateTime(TextBox1.Text) While index <= Convert.ToDateTime(TextBox2.Text) If index.DayOfWeek = DayOfWeek.Saturday OrElse index.DayOfWeek = DayOfWeek.Sunday Then NoOfDays = NoOfDays - 1 End If index = index.AddDays(1) End While Dim a1 As String = TextBox1.ToString() Dim b1 As String = TextBox2.ToString() Dim c1 As String = RadioButtonList1.Text.ToString() Dim d1 As String = TextBox3.ToString() Dim z1 As String = TextBox1.Text + "," + TextBox2.Text + "," + RadioButtonList1.Text + "," + TextBox3.Text + "," + NoOfDays.ToString 'csvContent.AppendLine("From Date,To Date,Laeve Type,Reason") csvContent.AppendLine(z1) Dim csvPath As String = "D:\CSVFile2.csv" ' Save or upload CSV format File (.csv) File.AppendAllText(csvPath, csvContent.ToString()) End Sub Protected Sub Button1_Click(sender As Object, e As EventArgs) If Page.IsValid Then Dim a1 As String = TextBox1.ToString() Dim b1 As String = TextBox2.ToString() Dim c1 As String = RadioButtonList1.Text.ToString() Dim d1 As String = TextBox3.ToString() UploadCSV() DownloadCSV() End If End Sub Protected Sub Button2_Click(sender As Object, e As EventArgs) End Sub End Class
Aspx代码
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master" CodeBehind="WebForm1.aspx.vb" Inherits="Leave12.WebForm1" %> <%-- Select From Date..... --%> <%-- Select To Date..... --%> <%-- --%> Casual Leave Sick Leave Earned Leave <%-- --%>