处理动态生成的表中的按钮点击

发布于 2024-10-31 17:59:55 字数 1309 浏览 1 评论 0原文

<%foreach (var indication in Model.FindAll(m => m.Model != null && m.Model.Trx != null).OrderBy(m => m.Model.Trx.PrimarySponsor.Company))
              { %>
                <tr>
              <td><%= indication.DisplayUser %></td>
              <td><%= indication.ActiveIndicationUsers[0].FullName %></td>
              <td><%= string.IsNullOrEmpty(indication.Model.Trx.PrimarySponsor.Company) ? "Not Yet Saved" : indication.Model.Trx.PrimarySponsor.Company %></td>
              <td><%= indication.TimeOpened.ToString(Chatham.Web.Data.Constants.Format.DateTimeSecondsFormatString) %></td>
              <td><%= indication.Model.Trx.ProductCollection[0].ProductTypeFriendlyName %></td>
              <td><%= (!indication.Model.Trx.ID.HasValue) ? "Not Yet Saved" : indication.Model.Trx.ID.Value.ToString() %></td>
              <td><input type="button" value="Open" name="<%= (!indication.Model.Trx.ID.HasValue) ? "Not Yet Saved" : indication.Model.Trx.ID.Value.ToString() %>" /></td>

              </tr>
            <%} %>

因此,如您所见,上表是动态生成的。如何处理按钮点击?我还想将按钮的 name 属性传递到处理按钮单击的任何方法中。

谢谢!

<%foreach (var indication in Model.FindAll(m => m.Model != null && m.Model.Trx != null).OrderBy(m => m.Model.Trx.PrimarySponsor.Company))
              { %>
                <tr>
              <td><%= indication.DisplayUser %></td>
              <td><%= indication.ActiveIndicationUsers[0].FullName %></td>
              <td><%= string.IsNullOrEmpty(indication.Model.Trx.PrimarySponsor.Company) ? "Not Yet Saved" : indication.Model.Trx.PrimarySponsor.Company %></td>
              <td><%= indication.TimeOpened.ToString(Chatham.Web.Data.Constants.Format.DateTimeSecondsFormatString) %></td>
              <td><%= indication.Model.Trx.ProductCollection[0].ProductTypeFriendlyName %></td>
              <td><%= (!indication.Model.Trx.ID.HasValue) ? "Not Yet Saved" : indication.Model.Trx.ID.Value.ToString() %></td>
              <td><input type="button" value="Open" name="<%= (!indication.Model.Trx.ID.HasValue) ? "Not Yet Saved" : indication.Model.Trx.ID.Value.ToString() %>" /></td>

              </tr>
            <%} %>

So that above table, as you can see, is dynamically generated. How do I handle the button click? I also want to pass the name attribute of the button into whatever method handles the button click.

Thanks!

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

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

发布评论

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

评论(3

少女净妖师 2024-11-07 17:59:55

您可以使用jQuery的live功能。

试试这个:

$(function(){
    $("td input[type=button][value=Open]").live("click", function(e){
        var btn = $(this);
        alert(btn.attr("name"));
    });
})

You can use the live function of jQuery.

Try this:

$(function(){
    $("td input[type=button][value=Open]").live("click", function(e){
        var btn = $(this);
        alert(btn.attr("name"));
    });
})
神经大条 2024-11-07 17:59:55

与处理常规按钮单击的方式相同。动态创建代码来处理您生成的 http 代码中的常规按钮点击。

The same way you would handle a regular button click. Dynamically create the code to handle regular button clicks in the http code you're generating.

浅忆流年 2024-11-07 17:59:55

这段代码是悲剧性的,需要重构。光是看着就觉得眼睛疼。您没有对字符串进行编码,因此使该代码容易受到 XSS 攻击。

因此,与往常一样,在 ASP.NET MVC 中,您从视图模型开始:

public class MyViewModel
{
    public string DisplayUser { get; set; }
    public string ActiveIndicationsUserFullname { get; set; }
    public string Company { get; set; }
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]
    public DateTime TimeOpened { get; set; }
    public string TrxId { get; set; }
}

然后您将拥有一个控制器操作,它将从存储库中获取模型并将其映射到视图模型。您可以使用 AutoMapper 来简化此映射。在映射层中,您将转换所有内容以供视图直接使用,这样该视图就不会类似于可怕的标签汤:

public ActionResult Foo() 
{
    // it's here that you should do the LINQ queries, etc ...
    // not in the view. Views are not supposed to fetch any data
    // and to be intelligent. Views should be dumb and only render
    // the preformatted data that they have been fed by the controller action
    IEnumerable<SomeModel> model = ...

    IEnumerable<MyViewModel> viewModel = Mapper.Map<IEnumerable<SomeModel>, IEnumerable<MyViewModel>>(model);
    return View(viewModel);
}

接下来我们进入强类型视图,我们将在其中使用显示模板:

<table id="myTable">
    <thead>
        <tr>
            <th>DisplayUser</th>
            <th>ActiveIndicationsUserFullname</th>
            <th>Company</th>
            <th>TimeOpened</th>
            <th>TrxId</th>
        </tr>
    </thead>
    <tbody>
        <%= Html.DisplayForModel()
    </tbody>
</table>

并在相应的显示模板(~/Views/Shared/DisplayTemplates/MyViewModel.ascx)中:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>" %>
<tr>
    <td><%= Html.DisplayFor(x => x.DisplayUser) %></td>
    <td><%= Html.DisplayFor(x => x.ActiveIndicationsUserFullname) %></td>
    <td><%= Html.DisplayFor(x => x.Company) %></td>
    <td><%= Html.DisplayFor(x => x.TimeOpened) %></td>
    <td><%= Html.DisplayFor(x => x.TrxId) %></td>
    <td>
        <input type="button" value="Open" name="<%= Model.TrxId %>" />
    </td>
</tr>

最后您可以使用 jquery 附加到此按钮的单击并获取名称:

$(function() {
    $('#myTable button').click(function() {
        var btn = $(this);
        alert(btn.attr('name'));
    });
});

That code is tragic and screaming for refactoring. Just looking at it my eyes hurt. You are not encoding strings thus making this code vulnerable to XSS attacks.

So as always in ASP.NET MVC you start with a view model:

public class MyViewModel
{
    public string DisplayUser { get; set; }
    public string ActiveIndicationsUserFullname { get; set; }
    public string Company { get; set; }
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]
    public DateTime TimeOpened { get; set; }
    public string TrxId { get; set; }
}

then you will have a controller action which will fetch the model from the repository and map it to the view model. You could use AutoMapper to simplify this mapping. It's in the mapping layer that you will transform everything to be ready to be directly used by the view so that this views doesn't resemble to a horrible tag soup:

public ActionResult Foo() 
{
    // it's here that you should do the LINQ queries, etc ...
    // not in the view. Views are not supposed to fetch any data
    // and to be intelligent. Views should be dumb and only render
    // the preformatted data that they have been fed by the controller action
    IEnumerable<SomeModel> model = ...

    IEnumerable<MyViewModel> viewModel = Mapper.Map<IEnumerable<SomeModel>, IEnumerable<MyViewModel>>(model);
    return View(viewModel);
}

next we get to the strongly typed view where we will be using Display Templates:

<table id="myTable">
    <thead>
        <tr>
            <th>DisplayUser</th>
            <th>ActiveIndicationsUserFullname</th>
            <th>Company</th>
            <th>TimeOpened</th>
            <th>TrxId</th>
        </tr>
    </thead>
    <tbody>
        <%= Html.DisplayForModel()
    </tbody>
</table>

and in the corresponding display template (~/Views/Shared/DisplayTemplates/MyViewModel.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>" %>
<tr>
    <td><%= Html.DisplayFor(x => x.DisplayUser) %></td>
    <td><%= Html.DisplayFor(x => x.ActiveIndicationsUserFullname) %></td>
    <td><%= Html.DisplayFor(x => x.Company) %></td>
    <td><%= Html.DisplayFor(x => x.TimeOpened) %></td>
    <td><%= Html.DisplayFor(x => x.TrxId) %></td>
    <td>
        <input type="button" value="Open" name="<%= Model.TrxId %>" />
    </td>
</tr>

and finally you could use jquery to attach to the click of this button and fetch the name:

$(function() {
    $('#myTable button').click(function() {
        var btn = $(this);
        alert(btn.attr('name'));
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文