禁用radiobuttonlist中的单选按钮 [英] Disabling radio buttons in radiobuttonlist
问题描述
I have a single RadioButtonList control with 4 ListItems within it. At run time, based on a condition in my database, I may need to disable one or more of those ListItems.
我尝试了什么:
What I have tried:
void bind()
{
SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
string Query = "select slot from appoint where selectdate ='" + txtdate.Text + "'";
DataTable dtAdmin = new DataTable();
SqlDataAdapter da;
da = new SqlDataAdapter(Query, cnn);
da.Fill(dtAdmin);
if (dtAdmin.Rows.Count > 0)
{
foreach (ListItem item in RadioButtonList1.Items)
{
if (item.Text == Query)
{
item.Enabled = false;
}
else
{
item.Enabled = true;
}
}
}
}
推荐答案
推荐这个例子
refer this example
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Text="one" Value="1" />
<asp:ListItem Text="two" Value="2" />
<asp:ListItem Text="three" Value="3" />
<asp:ListItem Text="four" Value="4" />
</asp:RadioButtonList>
I可能需要禁用其中一个或多个ListItem。
I may need to disable one or more of those ListItems.
string[] itemsToDisable = { "1", "2" };
foreach (string item in itemsToDisable)
{
var listItem = RadioButtonList1.Items.FindByValue(item);
if (listItem != null)
listItem.Enabled = false;
}
注意:格式化sql查询字符串是易受攻击到 SQL注入 [ ^ ]攻击
总是使用参数化查询以防止SQL Server中的SQL注入攻击 [< a href =http://www.aspsnippets.com/Articles/Using-Parameterized-queries-to-prevent-SQL-Injection-Attacks-in-SQL-Server.aspx\"target =_ blanktitle =New Window > ^ ]
使用 FindByText [ ^ ]或 FindByValue [ ^ ]基于您填充列表项的方式。
对代码的更正
Note : Formatting the sql Query string is vulnerable to SQL Injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
use FindByText[^] or FindByValue[^] based on how you are populating the list items.
Correction to your code
SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
string Query = "select slot from appoint where selectdate =@date";
SqlCommand cmd = new SqlCommand(Query, cnn);
cmd.Parameters.Add("@date", txtdate.Text);
DataTable dtAdmin = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtAdmin);
RadioButtonList1.Enabled = dtAdmin.Rows.Count > 0;
这篇关于禁用radiobuttonlist中的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!