2017-03-01 52 views
0

我想讓我的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:按鈕的事件,但沒有運氣。我卡住了,並會感謝所有的幫助!

回答

1

在沒有看到頁面的其餘部分的情況下做一個小的假設,你正在做一個PostBack到服務器來填充模態對話框。這會觸發頁面完全刷新,因此您的數據屬性對對話框的顯示沒有任何影響。

「最快」路線是在你的ItemComment代碼中註冊一個啓動JS,告訴模態顯示。我將盡快用完整的腳本更新這篇文章。

假設你在頁面上包含jQuery,你可以在你的方法的底部做這樣的事情。

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

這將添加一個腳本塊,它將在刷新時打開對話框。

現在,向前看,這可以做很多更好的方法,但要做到這一點還需要更多。

+0

This Works!非常感謝! – Marq

+0

很高興幫助!將來,您可能會考慮在Ajax調用的所有客戶端執行此操作,而不是數據列表操作,該操作需要回發並完全重新呈現頁面。這不僅僅是針對這個特定問題的簡單答案 –

相關問題