DataGrid 模版列问题

luoyi107 2006-08-23 09:38:54
在DataGrid有一个模版列
模版列里装了两个控件
一个是多选框<input id="checkAll" name="checkAll" type="checkbox" runat="server" />
另一个是单选框<input id="radioPartial" name="radioPartial" type="radio" runat="server"/>

有多条数据时单选框只能选择一个
同一行的的单选和多选也只能是一个

请问用脚本怎么实现
小弟对脚本不熟
请各位大虾帮写好
如果觉提我的问题说的不明白的请指出来
谢谢了
...全文
159 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoyi107 2006-08-23
  • 打赏
  • 举报
回复
都没用
[](表示多选) ○(表示单选)

结构如下
列1 列2 列3 模版列
行1 a dd [] ○
行2 b cc [] ◎ 这个单选是选中
行3 c dd [] ○

我再去选择 行1 的单选框,那么, 行2 的单选框的Checked = false;
luoyi107 2006-08-23
  • 打赏
  • 举报
回复
好长啊
Eddie005 2006-08-23
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace bsTest2005_8_16.Samples
{
/// <summary>
/// DataGrid翻页多选 的摘要说明。
/// </summary>
public class DataGrid翻页多选 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox Textbox6;
protected System.Web.UI.WebControls.TextBox Textbox3;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
BindData();

this.Button1.Attributes.Add("onclick","return confirm('您确认要删除所有选中的记录吗?');");
}

private void BindData()
{
//模拟出一些原始数据绑定DataGrid
DataTable dt1 = new DataTable();
dt1.Columns.Add("field1");
dt1.Columns.Add("field2");
dt1.Columns.Add("field3");

Random radm = new Random();
for(int i=1;i<30;i++)
dt1.Rows.Add(new object[]{"id"+i.ToString(),(i+1)*(i+2)," ABC"+radm.Next(10000).ToString()});

this.DataGrid1.DataSource=dt1;
this.DataGrid1.DataBind();
}

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindData();
}


private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Header)
{
CheckBox cb = e.Item.Cells[0].FindControl("CheckBoxAll") as CheckBox;
cb.Attributes.Add("onclick","selectAll(this);");//本页全选或取消的脚本函数
}
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBox cb = e.Item.Cells[0].FindControl("CheckBox1") as CheckBox;
cb.Attributes.Add("onclick","selectOne(this);");//选中或取消当前行的脚本函数

//如果当前行已选,那么checkbox应该勾上
string curID = e.Item.Cells[1].Text;
if(this.TextBox1.Text.IndexOf(curID)>-1)
cb.Checked = true;
}
}

private void Button1_Click(object sender, System.EventArgs e)
{
string a = this.TextBox1.Text;
if(a.Length>0)
{
string sql = "delete from table1 where id in ("+a.TrimEnd(',')+")";

System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection("连接字符串");
System.Data.OleDb.OleDbCommand cm = new System.Data.OleDb.OleDbCommand(sql,cnn);
cnn.Open();
int deletedRowsCount = cm.ExecuteNonQuery();
cnn.Close();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

}
}
Eddie005 2006-08-23
  • 打赏
  • 举报
回复
<%@ Page language="c#" Codebehind="DataGrid翻页多选.aspx.cs" AutoEventWireup="false" Inherits="bsTest2005_8_16.Samples.DataGrid翻页多选" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGrid翻页多选</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function selectAll(obj)
{
var i = 3;
var cbname = "DataGrid1__ctl"+i+"_CheckBox1";//DataGrid里嵌套的CheckBox的id是有规律的,利用之
while(document.getElementById(cbname)!=null && ((checkSelected() && obj.checked) || !obj.checked))
{
document.getElementById(cbname).checked = obj.checked;
selectOne(document.getElementById(cbname));
i = i+1;
cbname = "DataGrid1__ctl"+i+"_CheckBox1";
}

}

function selectOne(obj)
{
var curID = obj.parentElement.parentElement.cells[1].innerText;
var selectedID = document.getElementById('TextBox1').value;
if(obj.checked)
{
if(selectedID.indexOf(curID)<0)
{
if(checkSelected(obj))
{
document.getElementById('TextBox1').value = selectedID + curID + ',';
var count = new Number(document.getElementById('TextBox2').value);
document.getElementById('TextBox2').value = count + 1;
}
else
{
obj.checked = false;
}
}
}
else
{
if(selectedID.indexOf(curID)>-1)
{
var count = new Number(document.getElementById('TextBox2').value);
document.getElementById('TextBox2').value = count - 1;
}
document.getElementById('TextBox1').value = selectedID.replace(curID,'').replace(",,",",");

}
}

function checkSelected(obj)
{
var count1 = new Number(document.getElementById('TextBox2').value);
var count2 = new Number(document.getElementById('TextBox3').value);
if(count1 >= count2)
{
alert("您好,请选择不多于"+count2+"条记录");
return false;
}
else
{
return true;

}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 103; LEFT: 8px; POSITION: absolute; TOP: 8px" cellSpacing="0"
cellPadding="0" width="80%" border="0">
<TR>
<TD>隐藏域:
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" runat="server" Width="40px">0</asp:TextBox></TD>
<TR>
<TD>最多选择
<asp:TextBox id="Textbox3" runat="server" Width="40px">4</asp:TextBox>条记录</TD>
</TR>
<TR id="tr1">
<TD id="td1">
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" PageSize="8" AllowPaging="True">
<Columns>
<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderTemplate>
<asp:CheckBox id="CheckBoxAll" runat="server" Text="本页全选"></asp:CheckBox>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="CheckBox1" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="field1"></asp:BoundColumn>
<asp:BoundColumn DataField="field2"></asp:BoundColumn>
<asp:BoundColumn DataField="field3"></asp:BoundColumn>
</Columns>
</asp:DataGrid></TD>
</TR>
<TR>
<TD><asp:Button id="Button1" runat="server" Text="获得选中记录的id"></asp:Button></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
Eddie005 2006-08-23
  • 打赏
  • 举报
回复
<%@ Page language="c#" Codebehind="DataGrid加单选框.aspx.cs" AutoEventWireup="false" Inherits="bsTest2005_8_16.Samples.DataGrid加单选框" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<body>
<form runat="server" id="MM">
<input type="hidden" id="rd" runat="server" NAME="rd">
<asp:DataGrid id="ItemsGrid" runat="server" BorderColor="black" BorderWidth="1" CellPadding="3"
AllowPaging="true" AutoGenerateColumns="false">
<HeaderStyle BackColor="#00aaaa"></HeaderStyle>
<PagerStyle Mode="NumericPages"></PagerStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<input type=radio name="RadioName" value='<%# DataBinder.Eval(Container.DataItem, "IntegerValue")%>'/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="数字列" DataField="IntegerValue" />
<asp:BoundColumn HeaderText="字符串列" DataField="StringValue" />
<asp:BoundColumn HeaderText="货币列" DataField="CurrencyValue" DataFormatString="{0:c}">
<ItemStyle HorizontalAlign="right"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
<br>
<asp:Button id="Btn" Text="看你选择的" runat="server" />
<asp:Label id="Label1" Text="" runat="server" />
</form>
</body>
</HTML>
*********************************************8
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace bsTest2005_8_16.Samples
{
/// <summary>
/// DataGrid加单选框 的摘要说明。
/// </summary>
public class DataGrid加单选框 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid ItemsGrid;
protected System.Web.UI.WebControls.Button Btn;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.HtmlControls.HtmlInputHidden rd;



private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
System.Text.StringBuilder js = new System.Text.StringBuilder();
js.Append("<script>\r\n");
js.Append("function ld(){\r\n");
js.Append("for(i=0;i<document.getElementsByName('RadioName').length;i++)\r\n");
js.Append("if(document.getElementsByName('RadioName')[i].value==");
js.Append("document.getElementById('" + rd.ClientID + "').value) ");
js.Append("document.getElementsByName('RadioName')[i].checked=true\r\n");
js.Append("}\r\n");
js.Append("window.onload=ld\r\n");
js.Append("</"+"script>\r\n");
this.RegisterStartupScript("js",js.ToString());
if (!IsPostBack)
{
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
}

private DataView CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

for (int i = 0; i < 101; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i+1);
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}

private void Btn_Click(object sender, System.EventArgs e)
{
if(Request.Form["RadioName"] != null)
{
rd.Value = Request.Form["RadioName"].ToString();
Label1.Text = "您所选择的是:<font color=red>" + Request.Form["RadioName"].ToString() +"</font>";
}
}

private void ItemsGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
ItemsGrid.CurrentPageIndex = e.NewPageIndex;
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ItemsGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.ItemsGrid_PageIndexChanged);
this.Btn.Click += new System.EventHandler(this.Btn_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion


}
}
luoyi107 2006-08-23
  • 打赏
  • 举报
回复
顶起来
luoyi107 2006-08-23
  • 打赏
  • 举报
回复
发来看看
谢谢
Eddie005 2006-08-23
  • 打赏
  • 举报
回复
这个js有点复杂~~ 偶只有DataGrid加单选 或者多选的例子,没有合在一起的,要不要?~~
luoyi107 2006-08-23
  • 打赏
  • 举报
回复
onclick 事件
用javascrip 来写
marysxj 2006-08-23
  • 打赏
  • 举报
回复
不太明白,楼主想在什么事件里写?

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧