我怎样才能让我的按钮在datalist使用命令参数,而不是asp:按钮

问题描述:

我想让我的datalist按下按钮时打开模式弹出窗口。我的问题是数据切换和数据目标不在我的datalist中的asp:Button上工作。但只适用于按钮,与按钮的问题是我不能使用CommandArgument,我需要在我的模式上的datalist显示选择该项目。我怎样才能让我的按钮在datalist使用命令参数,而不是asp:按钮

 <ItemTemplate> 
     <div> 
      <asp:Image CssClass="center-block" Width="200px" Height="200px" ImageUrl='<%# "\\productImages\\" + Eval("productImage") %>' runat="server" /> 
      <br /> 
      <b><%# Eval("ProductName") %></b> 
      <button id="btnmod" runat="server" type="button" class="btn-success btn-xs pull-right" data-toggle="modal" data-target="#myModal">Buy</button> 
      <asp:Button data-toggle="modal" data-target="#myModal" CssClass="btn-default pull-right btn-xs" ID="btnBuy" runat="server" Text="Buy" CommandName="Buy" CommandArgument='<%# Eval("productID") %>'/> 
      <b class="pull-right">$<%#string.Format("{0:n2}",Eval("ProductPrice")) %></b> 

      <br /> 

     </div> 

这是我加载了我的模态的C#代码:现在如果我按“按钮”

protected void dlProducts_ItemCommand(object source, DataListCommandEventArgs e) 
    { 

     DAL mydal = new DAL(conn); 
     string productID = Convert.ToString(e.CommandArgument); 
     mydal.AddParam("productID", productID); 
     DataSet ds = mydal.ExecuteProcedure("spGetProducts"); 

     lblmodName.Text = ds.Tables[0].Rows[0]["productName"].ToString(); 
     lblmoddescription.Text = ds.Tables[0].Rows[0]["productDescription"].ToString(); 
     modimgprod.ImageUrl = "\\productImages\\" + ds.Tables[0].Rows[0]["productImage"].ToString(); 



    } 

它显示了模态而空,如果我按一下ASP:按钮它加载数据并将其放入模式中,但不会显示模式。所以,现在如果你再次按下按钮,现在它已经填满了。我一直在做一些研究,也许我可以点击“按钮”点击一个点击asp:按钮的事件,但没有运气。我卡住了,并会感谢所有的帮助!

在没有看到页面的其余部分的情况下做一个小的假设,你正在做一个PostBack到服务器来填充模态对话框。这会触发页面完全刷新,因此您的数据属性对对话框的显示没有任何影响。

“最快”路线是在你的ItemComment代码中注册一个启动JS,告诉模态显示。我将尽快用完整的脚本更新这篇文章。

假设你在页面上包含jQuery,你可以在你的方法的底部做这样的事情。

Page.ClientScript.RegisterStartupScript(this.GetType(), "DialogOpen", "$('#myModal').modal('show');", true); 

这将添加一个脚本块,它将在刷新时打开对话框。

现在,向前看,这可以做很多更好的方法,但要做到这一点还需要更多。

+0

This Works!非常感谢! – Marq

+0

很高兴帮助!将来,您可能会考虑在Ajax调用的所有客户端执行此操作,而不是数据列表操作,该操作需要回发并完全重新呈现页面。这不仅仅是针对这个特定问题的简单答案 –