带单选按钮列表的gridview [英] gridview with radiobuttonlist

查看:56
本文介绍了带单选按钮列表的gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用在线测试系统.i正在使用gridview来检索问题n单选按钮列表以获取答案.但是当我更改第一页n选择第二个问题的答案时,请对下一页上的其他问题执行相同的操作.我无法将选定的值保留在radiobuttonlist.so上,因此我想在更改页面时保留我的值,以便我可以找到所选择的答案.
请尽快给我发消息.....


i am working on online test system.i am using gridview for retrieving question n radiobuttonlist for answer.but when i change the first page n select the answer of 2nd question n do the same for further question on the next page.i am unable to hold the selected value on radiobuttonlist.so i want to hold my value when i will change the page so that i can find that what answer i have selected.
plz rply me soon.....

推荐答案

在本文中,我将向您简要说明如何在GridView中使用单选按钮.实际上,我将展示如何通过选择GridView内部的单选按钮并单击GridView外部的Delete按钮来从GridView中删除行.


受邀的步骤
创建符合我们要求的表
收合|复制代码

USE [样本]
GO
/******对象:表格[dbo].[tblCustomers1]脚本日期:05/12/2010 16:42:17 ******/
设置ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
设置ANSI_PADDING ON
GO
创建表[dbo].[Customers1](
[CustomerId] [int] IDENTITY(1,1)NOT NULL,
[城市] [varchar](50)收集SQL_Latin1_General_CP1_CI_AS NULL,
[PostalCode] [varchar](50)收集SQL_Latin1_General_CP1_CI_AS NULL
)于[PRIMARY]
GO
设置ANSI_PADDING OFF

用于删除的存储过程
收合|复制代码

USE [样本]
GO
/******对象:StoredProcedure [dbo].[empdel]脚本日期:05/12/2010 16:43:43 ******/
设置ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
创建过程[dbo].[empdel]
(
@CustomerID int
)
AS
从Customer1中删除,其中CustomerID = @ CustomerID
返回

数据绑定

现在是时候将数据绑定到GridView了:
收合|复制代码

私有void BindGrid()
{
字符串strQuery =从"Customers1"中选择"CustomerID,City,PostalCode"";
DataTable dt = new DataTable();
con =新的SqlConnection(ConfigurationSettings.AppSettings ["sqlcon"].ToString());
SqlDataAdapter sda =新的SqlDataAdapter();
cmd =新的SqlCommand(strQuery);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
试试
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch(ex ex例外)
{
扔前;
}
终于
{
con.Close();
sda.Dispose();
con.Dispose();
}
}

在按钮单击"事件中,编写以下代码:
收合|复制代码

受保护的void Button1_Click(对象发送者,EventArgs e)
{
Label1.Visible = false;
int Id = 0;
foreach(GridView1.Rows中的GridViewRow行)
{
RadioButton rb =(RadioButton)row.FindControl("RadioButton1");
如果(rb.Checked)
{
ID = Convert.ToInt32(GridView1.Rows [row.RowIndex] .Cells [1] .Text);
con = newSqlConnection(ConfigurationSettings.AppSettings ["sqlcon"].ToString());
cmd =新的SqlCommand("uspCustomers1Delete",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ CustomerID",SqlDbType.Int);
cmd.Parameters ["@ CustomerID"].Value = ID;
con.Open();
cmd.ExecuteNonQuery();
Label1.Visible = true;
Label1.Text =成功删除";
BindGrid();
}
}
}

现在,我将向您展示如何在未选择任何单选按钮并且单击删除"按钮的情况下发出警报.
范例画面:


客户端脚本

为此,在设计"页面中,如下编写JavaScript:
收合|复制代码

< script type ="text/javascript">
函数Validate(){
var gv = document.getElementById(<%= GridView1.ClientID%>");
var rbs = gv.getElementsByTagName("input");
var标志= 0;
for(var i = 0; i< rbs.length; i ++){

if(rbs [i] .type =="radio"){
如果(rbs [i] .checked){
标志= 1;
休息;
}
}
}
if(flag == 0){
alert(选择一个");
返回false;
}
其他{
var x = Confirm(确定要删除吗?");
if(x == true)
返回true;
其他
{
if(document.getElementById(<%= Label1.ClientID%>")!= null)
document.getElementById(<%= Label1.ClientID%>").innerText =";
返回false;
}
}
}
</script>

使用单选按钮时的一个问题是将选中所有单选按钮.因此,要仅选择一个单选按钮,请使用以下脚本:
收合|复制代码

< script type ="text/javascript">
函数RadioCheck(rb){
var gv = document.getElementById(<%= GridView1.ClientID%>");
var rbs = gv.getElementsByTagName("input");
var row = rb.parentNode.parentNode;
for(var i = 0; i< rbs.length; i ++){
if(rbs [i] .type =="radio"){
if(rbs [i] .checked&&rbs [i]!= rb){
rbs [i] .checked = false;
休息;
}
}
}
}
</script>

在定义单选按钮时调用此脚本,例如:
收合|复制代码

< asp:RadioButton ID ="RadioButton1" runat ="server" onclick ="RadioCheck(this);"/>

后台代码

最后,在页面加载中调用以下方法:
收合|复制代码

受保护的void Page_Load(对象发送者,EventArgs e)
{
Button1.Attributes.Add("onclick","javascript:return Validate()");
Label1.Visible = false;
如果(!IsPostBack)
{
BindGrid();
}
}
In this article, I am going to give you a brief explanation regarding how to use a radio button in a GridView. Actually I am going to show how we can delete a row from a GridView by selecting a radio button inside the GridView and by clicking on a Delete button outside the GridView.


Steps invloved
Creating a table for our requirement
Collapse | Copy Code

USE [Sample]
GO
/****** Object: Table [dbo].[tblCustomers1] Script Date: 05/12/2010 16:42:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Customers1](
[CustomerId] [int] IDENTITY(1,1) NOT NULL,
[City] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PostalCode] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF

Stored Procedure for deleting
Collapse | Copy Code

USE [Sample]
GO
/****** Object: StoredProcedure [dbo].[empdel] Script Date: 05/12/2010 16:43:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[empdel]
(
@CustomerID int
)
AS
delete from Customers1 where CustomerID=@CustomerID
RETURN

Databinding

Now it''s time to bind the data to the GridView:
Collapse | Copy Code

private void BindGrid()
{
string strQuery = "select CustomerID,City,PostalCode from Customers1";
DataTable dt = new DataTable();
con = new SqlConnection(ConfigurationSettings.AppSettings["sqlcon"].ToString());
SqlDataAdapter sda = new SqlDataAdapter();
cmd = new SqlCommand(strQuery);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}

In the Button click event, write the following code:
Collapse | Copy Code

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Visible = false;
int Id = 0;
foreach (GridViewRow row in GridView1.Rows)
{
RadioButton rb = (RadioButton)row.FindControl("RadioButton1");
if (rb.Checked)
{
Id =Convert.ToInt32(GridView1.Rows[row.RowIndex].Cells[1].Text);
con=newSqlConnection(ConfigurationSettings.AppSettings["sqlcon"].ToString());
cmd = new SqlCommand("uspCustomers1Delete", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CustomerID", SqlDbType.Int);
cmd.Parameters["@CustomerID"].Value = Id;
con.Open();
cmd.ExecuteNonQuery();
Label1.Visible = true;
Label1.Text = "Successfully Deleted";
BindGrid();
}
}
}

Now I will show you how to raise an alert if none of the radio buttons get selected and if you click on the Delete button.
Sample screen:


Client-side script

For this, in the Design page, write the JavaScript as follows:
Collapse | Copy Code

<script type="text/javascript">
function Validate() {
var gv = document.getElementById("<%=GridView1.ClientID%>");
var rbs = gv.getElementsByTagName("input");
var flag = 0;
for (var i = 0; i < rbs.length; i++) {

if (rbs[i].type == "radio") {
if (rbs[i].checked) {
flag = 1;
break;
}
}
}
if (flag == 0) {
alert("Select One");
return false;
}
else {
var x= confirm("Are you sure you want to delete?");
if(x==true)
return true;
else
{
if(document.getElementById("<%=Label1.ClientID%>") != null)
document.getElementById("<%=Label1.ClientID%>").innerText = "";
return false;
}
}
}
</script>

One problem when using radio buttons is all the radio buttons will be selected. So for getting only a single radio button selected, use the following script:
Collapse | Copy Code

<script type="text/javascript">
function RadioCheck(rb) {
var gv = document.getElementById("<%=GridView1.ClientID%>");
var rbs = gv.getElementsByTagName("input");
var row = rb.parentNode.parentNode;
for (var i = 0; i < rbs.length; i++) {
if (rbs[i].type == "radio") {
if (rbs[i].checked && rbs[i] != rb) {
rbs[i].checked = false;
break;
}
}
}
}
</script>

Call this script while defining the radio button, like:
Collapse | Copy Code

<asp:RadioButton ID="RadioButton1" runat="server" onclick="RadioCheck(this);"/>

Code-behind

Finally, call the method below in the page load:
Collapse | Copy Code

protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "javascript:return Validate()");
Label1.Visible = false;
if (!IsPostBack)
{
BindGrid();
}
}


这篇关于带单选按钮列表的gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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