简体   繁体   中英

I Can't Go to Another Page Using CommandArgument in ASP.NET Web Forms

I bring products from the database to the default.aspx with Eval . My default.aspx code and view is as follows:

<section id="products" class="portfolio-area section-padding">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="title">
                    <h1>Products</h1>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="iso-wraper">
                    <div class="iso-nav">
                        <ul>
                            <li class="hvr-shutter-out-vertical none-border portfolio-active" data-filter="*">All</li>
                            <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
                                <ItemTemplate>
                                    <li class="hvr-shutter-out-vertical none-border" data-filter=".<%#Eval("productcategory") %>"><%#Eval("productcategory") %></li>
                                </ItemTemplate>
                            </asp:Repeater>
                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:rebornConnectionString %>" SelectCommand="SELECT [productcategory] FROM [product] GROUP BY [productcategory]"></asp:SqlDataSource>
                        </ul>
                    </div>
                    <div class="iso-content">
                        <asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource2">
                            <ItemTemplate>
                                <div class="single-project <%#Eval("productcategory") %>">
                                    <div class="col-md-12">
                                        <div class="trainer-part">
                                            <img src="<%#Eval("productimage") %>" alt="">
                                            <br />
                                            <br />
                                            <br />
                                            <br />
                                            <br />
                                            <div class="trainer-dec" style="height: 110px;">
                                                <h3><%#Eval("productname") %></h3>
                                                <h3>Fiyat: <%#Eval("price") %> TL</h3>
                                            </div>
                                            <div class="trainer-social-link">
                                                <asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Click" CommandArgument='<%#Eval("productid") %>'>Detail</asp:LinkButton>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </ItemTemplate>
                        </asp:Repeater>
                        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:rebornConnectionString %>" SelectCommand="SELECT [productid], [productcategory], [productname], [price], [productimage] FROM [product]"></asp:SqlDataSource>                            
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

Please click to see my default.aspx view.

And my default.aspx.cs code:

protected void LinkButton1_Click(object sender, CommandEventArgs e)
{
    Session["productid"] = e.CommandArgument.ToString();
    Response.Redirect("product-detail.aspx");
}

My problem is: When I click "Detail" button, nothing happens.

When I put the mouse over the detail button, something like this appears on the bottom left of the browser:

Please click to see the writing.

Response.Redirect("~/product-detail.aspx");

Try replacing OnCommand with OnClick

Your button click is not going to server side that is why your code is not working.

<section id="products" class="portfolio-area section-padding">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="title">
                    <h1>Products</h1>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="iso-wraper">
                    <div class="iso-nav">
                        <ul>
                            <li class="hvr-shutter-out-vertical none-border portfolio-active" data-filter="*">All</li>
                            <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
                                <ItemTemplate>
                                    <li class="hvr-shutter-out-vertical none-border" data-filter=".<%#Eval("productcategory") %>"><%#Eval("productcategory") %></li>
                                </ItemTemplate>
                            </asp:Repeater>
                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:rebornConnectionString %>" SelectCommand="SELECT [productcategory] FROM [product] GROUP BY [productcategory]"></asp:SqlDataSource>
                        </ul>
                    </div>
                    <div class="iso-content">
                        <asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource2">
                            <ItemTemplate>
                                <div class="single-project <%#Eval("productcategory") %>">
                                    <div class="col-md-12">
                                        <div class="trainer-part">
                                            <img src="<%#Eval("productimage") %>" alt="">
                                            <br />
                                            <br />
                                            <br />
                                            <br />
                                            <br />
                                            <div class="trainer-dec" style="height: 110px;">
                                                <h3><%#Eval("productname") %></h3>
                                                <h3>Fiyat: <%#Eval("price") %> TL</h3>
                                            </div>
                                            <div class="trainer-social-link">
                                                <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" CommandArgument='<%#Eval("productid") %>'>Detail</asp:LinkButton>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </ItemTemplate>
                        </asp:Repeater>
                        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:rebornConnectionString %>" SelectCommand="SELECT [productid], [productcategory], [productname], [price], [productimage] FROM [product]"></asp:SqlDataSource>                            
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM