Marq Marq -4 years ago 108
ASP.NET (C#) Question

How can I make my button in datalist use command argument, not the asp:button

I'm trying to have my datalist open a modal pop-up when a button is pressed. My problem is data-toggle and data-target is not working on my asp:Button in my datalist. But works with just button, the problem with button is I cannot use CommandArgument which I need to make that item selected on the datalist show on my modal.

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


And this is my c# code that loads up my modal:

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();


Right now if I press the "button" it shows up the modal but empty, And if I click the asp:button it loads up the data and put it in the modal but wont show the modal. So, now if you press the button again, now it's filled. I've been doing some research that maybe I can just fire an event that clicks the asp:button when I click the "button", and have no luck. I'm stuck and would appreciate all the help!

Answer Source

Making a small assumption here without seeing the rest of the page, you are doing a PostBack to the server to fill the modal dialog. This triggers the page to fully refresh, therefore your data attributes do not have any impact on the display of the dialog.

The "Quickest" route would be to register a startup JS in your ItemComment code that tells the modal to show. I'll update the post shortly with the full script.

Assuming you have jQuery included on the page you might do something like this, at the bottom of your method.

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

This will add a script block that will open the dialog on refresh.

Now, going forward, this could be done many better ways, but it will take more to make that happen.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download