实际上我试图通过删除按钮发回帖后滚动页面。我试图从按钮点击事件调用JavaScript函数,按钮在gridview中。但是javascript函数没有调用。我想从代码隐藏调用JavaScript函数,而不是通过OnClientClick事件。请帮帮我
protected void btnDelete_Click(object sender, EventArgs e)
{
string pos = hdnfield.Value;
GridViewRow currentRow = (GridViewRow)((Button)sender).Parent.Parent;
Button btnDelete= (Button)currentRow.FindControl("btnDelete");
long ImageId = Convert.ToInt64(gridImages.DataKeys[currentRow.RowIndex]["IMAGE_ID"].ToString());
bool bRetValue = DeleteImage(ImageId);
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:scrollTo('" + pos + "')", true);
}
function scrollTo(val) {
$('html').scrollTop(val);
}
<asp:GridView ID="gridImages" runat="server" AutoGenerateColumns="False"
Height="234px" Width="368px" ShowHeader="false" BorderStyle="None" style="border-collapse:separate; border-spacing:20px;" GridLines="None" DataKeyNames ="IMAGE_ID">
<Columns>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#"ShowImages.ashx?ImgID="+ Eval("IMAGE_ID") %>' Height="78px" Width="118px" style="border:3px solid #CCCCFF"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete"
OnClick="btnDelete_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
答案 0 :(得分:0)
请改为尝试:
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "scrollTo('" + pos + "');", true);
另外,我注意到你的Javascript函数可能不正确。
function scrollTo(val) {
$('html').scrollTop(pos);
}
我假设您要使用val
方法中的scrollTop
参数。
function scrollTo(val) {
$('html').scrollTop(val);
}