62,074
社区成员
发帖
与我相关
我的任务
分享
/*fancybox弹出层*/
$(document).ready(function () {
$(".fb").fancybox({
'width': 600,
'height': 380,
'autoScale': false,
'transitionIn': 'none',
'transitionOut': 'none',
'type': 'iframe',
'href': 'B.aspx',
'onClosed': function() {
window.location.href='A.aspx';
}
});
});
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到某行上,该行变色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#CCCCFC'");
//鼠标移开后,恢复
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
//定义css弹出fancybox
//e.Row.CssClass = "fb";
//点击后,实现该行的选中
//e.Row.Attributes.Add("onclick", "__doPostBack('gv1','Select$" + e.Row.RowIndex + "')");
}
}
protected void gv1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//this.btnyh.Text = this.gv1.Rows[e.NewSelectedIndex].Cells[4].Text.ToString().Trim();
Session["id"] = this.gv1.Rows[e.NewSelectedIndex].Cells[5].Text.ToString().Trim();
this.gv1.Rows[e.NewSelectedIndex].CssClass = "fb";
}
<section class="featured">
<div>
<asp:GridView ID="Grid1" runat="server" OnSelectedIndexChanged="Grid1_SelectedIndexChanged" DataKeyNames="ID" AutoGenerateColumns="False" OnRowDataBound="Grid1_RowDataBound">
<Columns>
<asp:TemplateField ControlStyle-CssClass="desc">
<ItemTemplate>
<a><%# Eval("ID") %></a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<a><%# Eval("Name") %></a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</section>
<script>
function thisID(id) {
alert(id);
}
</script>
protected void Grid1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow GridR = Grid1.SelectedRow;
string S = Grid1.DataKeys[GridR.DataItemIndex].Value.ToString();
Page.ClientScript.RegisterStartupScript(this.GetType(), "tk", "thisID(" + S + ")", true);
}
protected void Grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes["style"] = "cursor:hand";
PostBackOptions myPostBackOptions = new PostBackOptions(this);
myPostBackOptions.AutoPostBack = false;
myPostBackOptions.PerformValidation = false;
myPostBackOptions.RequiresJavaScriptProtocol = true; //加入javascript:头
String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as GridView, "Select$" + e.Row.RowIndex.ToString());
e.Row.Attributes.Add("onclick", evt);
}
页头page 里面加上EnableEventValidation="false"
e.Row.Attributes.Add("onclick", "showFancyBox(" + ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString() + ");");
这句话估计有点问题,里面的((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
取不出值来,如果我把这个换成一个常量,比如100,就能达到效果了,但是这样的话取出来的全部是100了。我的DataKeyNames是这么设置的,不知道有没有问题。
<div>
<!-- 数据显示区 !-->
<a id="openUrl" href="#" style="display:none;"></a>
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" HorizontalAlign="Center" PageSize="5" DataSourceID="ds_gv1" AllowPaging="True" onrowdatabound="gv1_RowDataBound" onselectedindexchanging="gv1_SelectedIndexChanging" Height="100%" Width="100%"
DataKeyNames="dm" >
<asp:SqlDataSource ID="ds_gv1" runat="server" ConnectionString="<%$ ConnectionStrings:connstr %>"
<script type="text/javascript">
$(document).ready(function(){
$("#openUrl").fancybox({
'width':'75%',
'height':'75%',
'autoScale':false,
'transitionIn':'none',
'transitionOut':'none',
'type':'iframe'
});
});
function showFancyBox(id)
{
$("#openUrl").attr("href","b.aspx?id="+id);
$("#openUrl").click();
}
</script>
这个隐藏的a标签,你放在body,随便什么地方,反正看不到。
<a id="openUrl" href="#" style="display:none;"></a>
后台cs
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到某行上,该行变色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#CCCCFC'");
//鼠标移开后,恢复
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
//就这儿了
e.Row.Attributes.Add("onclick", "showFancyBox("+((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString()+")");
}
}
Request.QueryString["id"].ToString();
方式获取的。[/quote]
是的,showFancyBox是让你自己写js函数,B.aspx就是通过你这个方法取得idRequest.QueryString["id"].ToString();
方式获取的。
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到某行上,该行变色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#CCCCFC'");
//鼠标移开后,恢复
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
//就这儿了
e.Row.Attributes.Add("onclick", "showFancyBox("+((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString()+")");
}
}
就此全部搞定e.Row.Attributes.Add("onclick", "__doPostBack('gv1','Select$" + e.Row.RowIndex + "')");
然后点任何位置都能弹出fancybox??谢谢<a shref="@Url.Action("ProductContract", "Account", new { productSn = currProduct.Sn })" class="_product_contract"> 检查</a>
$('a._product_contract').click(function (event) {
var href = $(this).attr("shref");
$.fancybox.open({
href: href,
type: 'iframe',
padding: 5
});
});
就是我没有用直接初始化的方法,而是自定义了click事件来打开fancybox,这个是我在mvc项目里用的
<asp:TemplateField>
<ItemTemplate>
<a class="xxoo" href='b.aspx?id=<%#Eval(id"")%>'>详细信息</a>
$(".xxoo").fancybox({
'width':'75%',
'height':'75%',
'autoScale':false,
'transitionIn':'none',
'transitionOut':'none',
'type':'iframe'
});
$('a._product_contract').click(function (event) {
var href = $(this).attr("shref");
$.fancybox.open({
href: href,
type: 'iframe',
padding: 5
});
});
这是我完整的写法,将href赋给了自定义的attribute,然后点击时从这里面取值