gridview中有一个input控件模板列,弹出层是用脚本写的,表的DataKeyNames="ID" 现在知道用 string id = GridView1.DataKeys[GridView1 .SelectedIndex ].Value.ToString();获取选中的checkbox值。,现在我想实现的是当点击“查看详情”的时候,后台判断checkbox是否被选中,如果选中了,后台将ID记录下来并出现弹出层(层里面的内容想用C#实现),通过id从数据库中提取数据出现在弹出层里。弹出层的代码:<script type="text/javascript">
function showid(idname) {
var isIE = (document.all) ? true : false;
var isIE6 = isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6);
var newbox = document.getElementById(idname);
newbox.style.zIndex = "9999";
newbox.style.display = "block"
newbox.style.position = !isIE6 ? "fixed" : "absolute";
newbox.style.top = newbox.style.left = "50%";
newbox.style.marginTop = -newbox.offsetHeight / 2 + "px";
newbox.style.marginLeft = -newbox.offsetWidth / 2 + "px";
var layer = document.createElement("div");
layer.id = "layer";
layer.style.width = layer.style.height = "100%";
layer.style.position = !isIE6 ? "fixed" : "absolute";
layer.style.top = layer.style.left = 0;
layer.style.backgroundColor = "#000";
layer.style.zIndex = "9998";
layer.style.opacity = "0.6";
document.body.appendChild(layer);
var sel = document.getElementsByTagName("select");
for (var i = 0; i < sel.length; i++) {
sel[i].style.visibility = "hidden";
}
function layer_iestyle() {
layer.style.width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth)
+ "px";
layer.style.height = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) +
"px";
}
function newbox_iestyle() {
newbox.style.marginTop = document.documentElement.scrollTop - newbox.offsetHeight / 2 + "px";
newbox.style.marginLeft = document.documentElement.scrollLeft - newbox.offsetWidth / 2 + "px";
}
if (isIE) { layer.style.filter = "alpha(opacity=60)"; }
if (isIE6) {
layer_iestyle()
newbox_iestyle();
window.attachEvent("onscroll", function () {
newbox_iestyle();
})
window.attachEvent("onresize", layer_iestyle)
}
layer.onclick = function () {
newbox.style.display = "none"; layer.style.display = "none"; for (var i = 0; i < sel.length; i++) {
sel[i].style.visibility = "visible";
}
}
}
</script>调用这个脚本:<asp:TemplateField HeaderText="详情">
<ItemTemplate>
<input name ="button_test" type ="button" onclick ="showid('smallLay'); " style =" width :65px; height :25px;" value="查看详情" />
</ItemTemplate>
</asp:TemplateField>,后面的思路比较清楚,就是在checkbox记录id这儿不知道该如何实现,求大侠指教,最好提供下代码并有注释,学习下~~~~~~~~~~~
function showid(idname) {
var isIE = (document.all) ? true : false;
var isIE6 = isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6);
var newbox = document.getElementById(idname);
newbox.style.zIndex = "9999";
newbox.style.display = "block"
newbox.style.position = !isIE6 ? "fixed" : "absolute";
newbox.style.top = newbox.style.left = "50%";
newbox.style.marginTop = -newbox.offsetHeight / 2 + "px";
newbox.style.marginLeft = -newbox.offsetWidth / 2 + "px";
var layer = document.createElement("div");
layer.id = "layer";
layer.style.width = layer.style.height = "100%";
layer.style.position = !isIE6 ? "fixed" : "absolute";
layer.style.top = layer.style.left = 0;
layer.style.backgroundColor = "#000";
layer.style.zIndex = "9998";
layer.style.opacity = "0.6";
document.body.appendChild(layer);
var sel = document.getElementsByTagName("select");
for (var i = 0; i < sel.length; i++) {
sel[i].style.visibility = "hidden";
}
function layer_iestyle() {
layer.style.width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth)
+ "px";
layer.style.height = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) +
"px";
}
function newbox_iestyle() {
newbox.style.marginTop = document.documentElement.scrollTop - newbox.offsetHeight / 2 + "px";
newbox.style.marginLeft = document.documentElement.scrollLeft - newbox.offsetWidth / 2 + "px";
}
if (isIE) { layer.style.filter = "alpha(opacity=60)"; }
if (isIE6) {
layer_iestyle()
newbox_iestyle();
window.attachEvent("onscroll", function () {
newbox_iestyle();
})
window.attachEvent("onresize", layer_iestyle)
}
layer.onclick = function () {
newbox.style.display = "none"; layer.style.display = "none"; for (var i = 0; i < sel.length; i++) {
sel[i].style.visibility = "visible";
}
}
}
</script>调用这个脚本:<asp:TemplateField HeaderText="详情">
<ItemTemplate>
<input name ="button_test" type ="button" onclick ="showid('smallLay'); " style =" width :65px; height :25px;" value="查看详情" />
</ItemTemplate>
</asp:TemplateField>,后面的思路比较清楚,就是在checkbox记录id这儿不知道该如何实现,求大侠指教,最好提供下代码并有注释,学习下~~~~~~~~~~~
<ItemTemplate>
<input name ="button_test" type ="button" onclick ='showid(<%#Eval("id")%>)' />
</ItemTemplate>
你是后台进行数据交互,这时候不可避免要postback,那你的js层效果直接在刷新后就没了,所以你的onclick ='showid(<%#Eval("id")%>)'是无效的,除非你点击checkbox的时候就把数据填充好,但明显这是不科学的做法如果你的确要做到这样的效果,那么正确的做法应该是前台
将你“查看详情”按钮转化成服务器控件,并对此控件添加Click事件,具体你可以拖一个按钮控件,并双击生成click事件后,将这个控件拖到模板列里面后台
在按钮的click事件里面将sender参数转化为你的button控件,类似下面的代码
Button bt = sender as Button;
if(bt!=null)
{
GridViewRow row = bt.NamingContainer as GridViewRow;
CheckBox cb = row.Cells[0].FindControl("checkBox1") as CheckBox;
if(cb.Checked)//判断是否选中
{
//填充层数据
//注册js脚本,以弹出层
}
}
这个代码都是手写的,具体可能有错误,反正思路就是这个样子另外如果可以ajax的还是ajax方便
{
Button bt = sender as Button;//将sender这个object对象转换成button对象,as转换,获取事件发送者button
if (bt != null)
{
///Control.NamingContainer属性获取对服务器控件的命名容器的引用,利用button控件的NamingContainer获取GridViewRow(当前行)
GridViewRow row = bt.NamingContainer as GridViewRow;
///FindControl返回的是一个control类型的控件
///获取第一列Control控件的集合转换成checkBox类型的控件,
CheckBox cb = row.Cells[0].FindControl("ck") as CheckBox ;
if (cb.Checked)
{
///获取选中checkbox的值
string id=GridView1.DataKeys[GridView1 .SelectedIndex ].Value.ToString();
string constr = ConfigurationManager.ConnectionStrings["Conn"].ToString();
OleDbConnection conn = new OleDbConnection(constr);
conn.Open();
string s = "select * from D_SheList where [ID]=id";
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(s, conn);
da.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
Label2.Text = ds.Tables[0].Rows[0][1].ToString();
Label3.Text = ds.Tables[0].Rows[0][2].ToString();
Label4.Text = ds.Tables[0].Rows[0][3].ToString();
TextBox2.Text = ds.Tables[0].Rows[0][4].ToString();
TextBox3.Text = ds.Tables[0].Rows[0][5].ToString();
TextBox4.Text = ds.Tables[0].Rows[0][6].ToString();
TextBox5.Text = ds.Tables[0].Rows[0][7].ToString();
TextBox6.Text = ds.Tables[0].Rows[0][8].ToString();
TextBox7.Text = ds.Tables[0].Rows[0][9].ToString();
DropDownList3.SelectedValue = ds.Tables[0].Rows[0][10].ToString();
///后台调用前台脚本
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "showid('smallLay')",true );
}
else {
Response.Write("<script>alert('请先选择');</script>");
}
}
}
}
string id=GridView1.DataKeys[GridView1 .SelectedIndex ].Value.ToString();这个有问题,应该改成row.RowIndex