问题描述
如何从单选按钮选择的索引中检索chkbox的值??
代码是:
Hi,
How to retrieve value of chkbox from radio button selected index ??
CODE IS :
<asp:RadioButtonList ID="rdoSr" runat="server" AutoPostBack="True"
onselectedindexchanged="rdoSr_SelectedIndexChanged">
</asp:RadioButtonList>
<asp:CheckBoxList ID="chkSr" runat="server">
</asp:CheckBoxList>
数据库
DATABASE
CREATE TABLE [dbo].[Customer](
[Sr_No] [int] IDENTITY(1,1) NOT NULL,
[City] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[phone] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Population] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
[Sr_No] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[State](
[Sr_No] [int] IDENTITY(1,1) NOT NULL,
[City] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[State] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_State] PRIMARY KEY CLUSTERED
(
[Sr_No] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
ALTER procedure [dbo].[Customer_FullJoin_State_Retrieve]
@SNo int
as
select Customer.City AS City1,State.City AS City2,Customer.Sr_No as Sr_No1, * from Customer full Join State on Customer.City = State.City
where Customer.Sr_No=@SNo
RETURN
主要代码
Main Code
protected void rdoSr_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConnString);
SqlCommand cmd = new SqlCommand("Customer_FullJoin_State_Retrieve", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@SNo", rdoSr.SelectedItem.Value);
LoadRetreive3();// bind value of rdoSr from Customer table
LoadRetreive4();//bind value of chkSr from State table
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
{
chkSr.SelectedItem.Text=reader["State"].ToString(); // here's the error
reader.Close();
}
conn.Close();
}
我希望从数据库和表名"State"中选择chkSr值(选择一个)... rdoSr包含相同的state列值,并且我希望在rdoSr上选择后自动显示chksr的值. rdoSr和chksr都包含从表State的列Sate检索到的相同值.
I want chkSr value(selected one) from database and table name "State"... rdoSr contains same values of state column and I want after select on rdoSr, the value of chksr comes up automatically. Both rdoSr and chksr contains same values dat''s retrieve from column Sate of table State
推荐答案
chkSr.Text=reader["State"].ToString();
而不是
and not
chkSr.SelectedItem.Text=reader["State"].ToString();
换句话说,将您从数据库加载的文本直接分配给chkSr复选框的Text属性.删除"SelectedItem"部分.
In other words, assign the text you loaded from the database to the Text property of the chkSr checkbox directly; remove the "SelectedItem" portion.
ALTER procedure [dbo].[Customer_FullJoin_State_Retrieve]
@SNo int
as
select Customer.City AS City1,State.City AS City2, State.State AS State, Customer.Sr_No as Sr_No1, * from Customer full Join State on Customer.City = State.City
where Customer.Sr_No=@SNo
RETURN
希望对您有帮助.
I hope this will helps you well.
foreach (ListItem l in CheckBoxList1.Items)
{
if (l.Text == "Your Data Base Value")
{
l.Selected = true;
}
}
这篇关于如何从单选按钮选择的索引中检索chkbox的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!