如何从单选按钮选择的索引中检索chkbox的值? [英] How to retrieve value of chkbox from radio button selected index ?

查看:75
本文介绍了如何从单选按钮选择的索引中检索chkbox的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何从单选按钮选择的索引中检索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

推荐答案

如果我正确理解了您的查询,则尝试将复选框的标签(即旁边的文本)更改为您正在从数据库加载的文本.

看起来您已经成功从数据库中获取了文本.但是,您需要执行
If I understand your query correctly you''re trying to change the label of the checkbox (i.e. the text that appears next to it) to text you''re loading from a database.

Looks like you''re successfully getting the text from the database. However, you need to execute
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.


您的StoredProcedure返回City1,City2,Sr_No1Customer表中的所有列.但是您正在尝试使用DataReader read方法访问名为State的表名称.请参阅如何使用使用DataReader(ADO.NET)检索数据http://msdn.microsoft. com/en-us/library/haa3afyz.aspx [ ^ ]

另一种选择,

改进您的SP以包含State

Your StoredProcedure returns City1,City2,Sr_No1 and All columns from Customer table. But your are trying to access a Table name called State using DataReader read method. Please see the how to use Retrieving Data Using a DataReader (ADO.NET)http://msdn.microsoft.com/en-us/library/haa3afyz.aspx[^]

Another option,

Improve your SP to include State Column

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.


尝试一下

将那个{reader ["State"].ToString()值与字符串
中的l.text值进行比较 然后

try this

take that {reader["State"].ToString() value and compare it with l.text } value in string
and then

foreach (ListItem l in CheckBoxList1.Items)
            {
                if (l.Text == "Your Data Base Value")
                {
                    l.Selected = true;
                }
            }


这篇关于如何从单选按钮选择的索引中检索chkbox的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆