[英]dynamic LinkButton OnClick event not working on ASP.Net
I want to create dynamic LinkButton for image, <img>
tag is not working dynamically so I am using LinkButton with image. 我想为图像创建动态LinkButton,
<img>
标签不能动态工作,所以我将LinkButton与图像一起使用。
I don't want to provide ID to LinkButton because I want to generate more LinkButton dynamically. 我不想为LinkButton提供ID,因为我想动态生成更多LinkButton。 I am using following code in Default.aspx
我在Default.aspx中使用以下代码
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(@"<asp:LinkButton runat=""server"" OnClick=""btn_click""><img src=""close-icon (1).png"" /></asp:LinkButton>");
}
public void btn_click(object sender, EventArgs e)
{
Response.Write("HELLO");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
I also tried to write tag code in Default.aspx.cs file but not work. 我也尝试在Default.aspx.cs文件中编写标记代码,但不起作用。
It's showing me following error. 它向我显示以下错误。
Error 1 'ASP.default_aspx' does not contain a definition for 'img_Click' and no extension method 'img_Click' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)
错误1'ASP.default_aspx'不包含'img_Click'的定义,并且找不到扩展方法'img_Click'接受类型为'ASP.default_aspx'的第一个参数(您是否缺少using指令或程序集引用?)
Please help me to solve this problem. 请帮我解决这个问题。
I found my answer,answer is below 我找到了答案,答案在下面
Default.aspx.cs Page Default.aspx.cs页面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class dynamicimage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = @"<asp:ImageButton ID=""dynoimage"" ImageUrl=""~/images/about01.jpg"" runat=""server"" oncommand=""clickme"" commandname=""btn"" />";
Control c = ParseControl(str);
form1.Controls.Add(c);
((ImageButton)Page.FindControl("dynoimage")).Command += new CommandEventHandler(clickme);
}
protected void clickme(object sender,CommandEventArgs e)
{
Response.Write("Image clicked");
Label1.Text = "Image clicked";
}
}
and here is the Default.aspx Page 这是Default.aspx页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamicimage.aspx.cs" Inherits="dynamicimage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<p>
<asp:Label ID="Label1" runat="server" Text="before click"></asp:Label>
</p>
</form>
</body>
</html>
You have got the approach wrong, your first Response.Write("...") not the Response.Write(" 您的方法有误,您的第一个Response.Write(“ ...”)而不是Response.Write(“
Read this aritcle to use controls in ASP.NET dynamically. 阅读此文章以动态使用ASP.NET中的控件。
http://msdn.microsoft.com/en-us/library/kyt0fzt1%28v=vs.100%29.aspx http://msdn.microsoft.com/en-us/library/kyt0fzt1%28v=vs.100%29.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.