[英]anchor button to call server side method
我想使用锚点按钮调用服务器端方法。 我不能使用linkbutton
因为我想在代码隐藏中创建一个锚点按钮。
这是一个快速的代码示例:我在前端创建了一个div
,id是“dv”
<div id ="dv" runat="server">
代码背后:
dv.InnerHtml = "<a href=\"javascript:void(0);\" id=" + dxm.Id + " onclick=\"__doPostBack('" + dxm.Id + "', '');\" > </a>";
在这个方法中,我想通过调用按钮传入id。 单击该按钮时,它将执行回发并将id发送到此方法。
protect void PopupBox(string id)
{
//using the id get the data.
//show modalpopup box
}
我不想使用linkbutton。 我用div作为例子。 我在里面使用了一个树视图,我将使用节点文本来创建一个锚点按钮
node.text = "<a href=\"javascript:void(0);\" id=" + dxm.Id + " onclick=\"__doPostBack('" + dxm.Id + "', '');\" > </a>";
在asp.net版本4中,由于版本4包含额外的安全检查,因此回发的“黑客”操作不起作用。 使用asp.net控件执行此操作的最佳方法是动态加载控件。 这是我刚检查和工作的一些代码。
protected void Page_Load(object sender, EventArgs e)
{
// create the control dynamically
LinkButton lbOneMore = new LinkButton();
// the text and the commands
lbOneMore.Text = "One more click";
lbOneMore.CommandArgument = "cArg";
lbOneMore.CommandName = "CName";
// the click handler
lbOneMore.Click += new EventHandler(lbOneMore_Click);
// and now add this button link to your div
DivControl.Controls.Add(lbOneMore);
}
// here you come with the click, and the sender contains the commands
void lbOneMore_Click(object sender, EventArgs e)
{
txtDebug.Text += "<br> Command: " + ((LinkButton)sender).CommandArgument;
}
在asp.net页面上:
<div runat="server" id="DivControl"></div>
<asp:Literal runat="server" ID="txtDebug" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.