调用服务器端方法的锚按钮

发布于 2025-01-01 12:17:04 字数 755 浏览 4 评论 0 原文

我想使用锚按钮调用服务器端方法。我无法使用 linkbutton 因为我想在代码隐藏中创建一个锚按钮。

这是一个快速代码示例: 我在前端创建了一个 div ,id 是

<div id ="dv" runat="server">

后面的“dv”代码:

 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
}

我不想使用链接按钮。我用div作为例子。我正在使用树视图,其中我将使用节点文本创建锚按钮

node.text = "<a href=\"javascript:void(0);\" id=" + dxm.Id + " onclick=\"__doPostBack('" + dxm.Id + "', '');\" > </a>";

I want to call a server side method using an anchor button. I can't use linkbutton as I want to create an anchor button in the code-behind.

Here is a quick code sample:
I created a div in the front end and the id is "dv"

<div id ="dv" runat="server">

code behind:

 dv.InnerHtml =  "<a href=\"javascript:void(0);\" id=" + dxm.Id + " onclick=\"__doPostBack('" + dxm.Id + "', '');\" > </a>";

In this method i want to pass in the id by calling the button. When the button is clicked it will do a postback and send the id to this method.

protect void PopupBox(string id)
{
   //using the id get the data.
   //show modalpopup box
}

I dont want to use linkbutton. I used div as a example. I am using a treeview with inside this i will use node text to create a anchor button

node.text = "<a href=\"javascript:void(0);\" id=" + dxm.Id + " onclick=\"__doPostBack('" + dxm.Id + "', '');\" > </a>";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风挽心 2025-01-08 12:17:04

在 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" />

In asp.net version 4 this 'hack' action of postback does not work because of the extra security checks that version 4 includes. The best way to do this using asp.net controls is to do a dynamic load of the controls. Here is some code that I have just checked and working.

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;
}

On asp.net page :

<div runat="server" id="DivControl"></div>
<asp:Literal runat="server" ID="txtDebug" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文