0

asp.net 链接按钮无法正常工作

<%@ Page Title="" Language="C#" MasterPageFile="~/Ui/MasterPage_UI.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Ui_Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<div class="content" runat="server">
<p>
I am a returning customer</p>
<b>E-Mail Address:</b><br />
    <asp:TextBox name="email" runat="server" ID="email" 
    ValidationGroup="email" ></asp:TextBox>
    <br />
    <br />
    <b>Password:</b><br />
    <asp:TextBox  ID="pwd" runat="server" name="password" TextMode="Password"></asp:TextBox>
    <br />
    <br />
    <asp:LinkButton ID="Submit" runat="server" CssClass="button" Text="Login" onclick="Submit_Click" ValidationGroup="email" />
</div>

</asp:Content>

动态生成的菜单的 aspx 代码是--> `

<div class="categories" runat="server">
<ul runat="server" id="nav">
<li class="home" style="background-repeat: no-repeat; background-color: #94C23C; background-image: url(home.jpg); background-position: center;"><a href="index.htm">Home</a></li>
<!--Dynamic menu-->
</ul>
</div>
</div>`

你的cs代码是-->

`public void bindmenu() { HtmlGenericControl li; HtmlAnchor 链接;HtmlGenericControl div;

    //for main menu
    DataTable dt = new BALCate().GetCate();
    foreach (DataRow dr in dt.Rows)
    {
        int cateid = int.Parse(dr["cate_id"].ToString());
        String cate_name = dr["cate_name"].ToString();
        li = new HtmlGenericControl("li");
        link = new HtmlAnchor();
        link.InnerText = dr["cate_name"].ToString();
        link.HRef = "CateProducts.aspx?cate_id=" + cateid + "&" + "catename=" + cate_name;
        li.Controls.Add(link);

        //for submenu
        DataTable subcate = new BALCate().GetSubCate(cateid);
        if (subcate.Rows.Count > 0)
        {
            div = new HtmlGenericControl("div");
            div.Attributes.Add("class", "sub-menu column-1");
            HtmlGenericControl sul = new HtmlGenericControl("ul");

            foreach (DataRow sdr in subcate.Rows)
            {
                int subcateid = int.Parse(sdr["subcate_id"].ToString());
                String subcatename = sdr["subcate_name"].ToString();
                HtmlGenericControl sli = new HtmlGenericControl("li");
                HtmlAnchor slink = new HtmlAnchor();
                slink.InnerHtml = sdr["subcate_name"].ToString();
                sli.Controls.Add(slink);
                slink.HRef = "SubCateProducts.aspx?subcate_id=" + subcateid + "&" + "catename=" + cate_name + "&" + "subcatename=" + subcatename;
                sul.Controls.Add(sli);

                DataTable subsubcate = new BALCate().GetSubSubCate(subcateid);
                if (subsubcate.Rows.Count > 0)
                {
                    HtmlGenericControl subdiv = new HtmlGenericControl("div");
                    subdiv.Attributes.Add("class", "sub-menu");
                    HtmlGenericControl ssul = new HtmlGenericControl("ul");

                    foreach (DataRow ssdr in subsubcate.Rows)
                    {
                        int subsubcateid = int.Parse(ssdr["subsubcate_id"].ToString());
                        String subsubcatename = ssdr["subsubcate_name"].ToString();
                        HtmlGenericControl ssli = new HtmlGenericControl("li");
                        HtmlAnchor sslink = new HtmlAnchor();
                        sslink.HRef = "SubSubCateProducts.aspx?subsubcate_id=" + subsubcateid + "&" + "catename=" + cate_name + "&" + "subcatename=" + subcatename + "&" + "subsubcatename=" + subsubcatename;
                        sslink.InnerText = ssdr["subsubcate_name"].ToString();
                        ssli.Controls.Add(sslink);
                        ssul.Controls.Add(ssli);
                    }
                    subdiv.Controls.Add(ssul);
                    sli.Controls.Add(subdiv);
                }
            }
            div.Controls.Add(sul);
            li.Controls.Add(div);
        }
        nav.Controls.Add(li);
    }
}`

实际上还有一个母版页,其中包含动态菜单,其中在页面加载时从数据库中动态加载数据(类别和子类别)。但问题是,每当我点击链接按钮时,菜单不会从数据库中检索数据,并且显示为空白,其余的静态部分。

4

1 回答 1

3

将 onclick 添加到执行数据检索的方法的按钮。

<asp:LinkButton ID="Submit" runat="server" CssClass="button" Text="Login" 
ValidationGroup="email" onclick="MethodName" />
于 2012-10-16T15:50:27.300 回答