简体   繁体   English

获取Razor中动态生成的按钮的ID

[英]Get id of dynamically generated button in Razor

I've a table with a list of object and in every row I've dynamically generated button to delete the row. 我有一个带有对象列表的表,并且在每行中我都动态生成了一个删除行的按钮。

how to get in @{ c# } the id of the pressed button? 如何获取@{ c# }所按下按钮的ID?

@for (int i = 0; i < BL.SNG.GetListLen(); i++)
{
    <form method="post"
          action="">
        <input type="submit"
               value="Delete @i"
               id="@i"
               onclick="button_click(this.id)" />
    </form>
    </td>

You are placing the id on the wrong element. 您将ID放在错误的元素上。 Since the loop creates a new form element and a new button on each iteration. 由于循环在每次迭代时都会创建一个新的表单元素和一个新的按钮。 the id should be assigned to the form element itself. id应该分配给form元素本身。 I assume button_click refers to some JavaScript function defined elsewhere. 我假设button_click引用了其他地方定义的一些JavaScript函数。

Note that it is discouraged to attach event handlers to elements in the html markup portion of your page. 请注意,不建议将事件处理程序附加到页面html标记部分中的元素。 You should generally add them from JavaScript. 通常,您应该从JavaScript添加它们。 Like this: 像这样:

document.getElementsByTagName("input")
.forEach( addEventListener("click", function (...) { });

Sorry, I just noticed your comment about deleting the row represented from the database. 抱歉,我刚刚注意到您有关删除数据库中代表的行的评论。 You could use the submit action of the form itself to do that as well. 您也可以使用表单本身的Submit操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击时如何获取动态生成的按钮的id - How to get the id of dynamically generated button when clicked 如何获取从事件处理程序中的背后代码动态生成的链接按钮ID - How to get link button id that is generated dynamically from code behind in the event handler 在动态生成的菜单中获取SelectedItem的ID - Get SelectedItem's Id in dynamically generated Menu 获取动态生成的ajax文本框ID - get dynamically generated ajax textbox id 从Razor到jQuery获取动态创建的TextBox ID - Get Dynamically Created TextBox ID From Razor To jQuery 如何在 Razor 代码中获取所选单选按钮的 ID? - How to get the id of selected radio button in Razor code? 获取动态生成的文本框的ID,该文本框正在调用javascript函数 - Get the ID of the dynamically generated textbox which is calling a javascript function 如何获取动态生成的控件的ID? - How can I get ID's of Dynamically generated Controls? 如果在foreach循环下动态生成Textarea多次,如何在Razor(blazor)组件中获取Textarea值 - How to get Textarea values in Razor(blazor) component, if Textarea is generated under foreach loop dynamically for more then once 从动态生成的文本框中获取文本以使用按钮插入 - Get text from dynamically-generated textbox to insert with button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM