Bind/naviagte menu items in web forms (ASP.net)

强颜欢笑 提交于 2019-12-12 03:13:56

问题


I am new to ASP.net web forms. Please help me for following issue.

Dragged menu control in the design. Edited menu items with its sub items. I have a grid view with the data bound to the database. When I select the sub menu item the gridview with the data should be shown below the menu. How to show he gridview when I select a menu item

I tried to bind the grid view id but it doesn't work.

      <asp:Menu ID="mini" runat="server" Orientation="Horizontal">
          <Items> <asp:MenuItem Text="Item">
              <asp:MenuItem Text="sdaad" Value="1">
                  <asp:MenuItem Text="MenuSub" Value="MenuSub" ></asp:MenuItem>
                  <asp:MenuItem Text="MenuSub1" Value="MenuSub1" ></asp:MenuItem>
              </asp:MenuItem>
              <asp:MenuItem Text="Item" Value="Item">
                  <asp:MenuItem Text="MenuSub" Value="MenuSub"></asp:MenuItem>
              </asp:MenuItem>
              </asp:MenuItem></Items>
      </asp:Menu>

I want to show the gridview when the menu item is selected

     <asp:GridView ID="GridView1" CssClass="table-condensed" runat="server" DataKeyNames="projectid" OnRowCommand="Unnamed_RowCommand">
        <Columns>
        <asp:TemplateField HeaderText="Proje">
        <ItemTemplate>
        <asp:Label ID="lbfid" runat="server" Text='<%#Eval("projectid")%>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Doc">
        <ItemTemplate>
        <asp:Label ID="lbfname" runat="server" Text='<%#Eval("video")%>'></asp:Label>
            <asp:HyperLink runat="server" CommandName="view" CommandArgument='<%#Eval("abstract")%>' >download</asp:HyperLink>

        </ItemTemplate>
        </asp:TemplateField>
            </Columns>

    </asp:GridView>

In my page load event

con.Open();


    SqlDataAdapter sda = new SqlDataAdapter("select * from table", con);
    DataSet ds = new DataSet();
    sda.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }
    con.Close();

Please help me


回答1:


I think This will help you

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">  
    </head>  
    <body>  
        <form runat="server" id="form1">
       <asp:Menu OnMenuItemClick="Menu1_MenuItemClick" ID="mini" runat="server" Orientation="Horizontal">
              <Items> <asp:MenuItem Text="Item">
                  <asp:MenuItem Text="GetUser" Value="1">
                      <asp:MenuItem Text="MenuSub" Value="MenuSub" ></asp:MenuItem>
                      <asp:MenuItem Text="MenuSub1" Value="MenuSub1" ></asp:MenuItem>
                  </asp:MenuItem>
                  <asp:MenuItem Text="Item" Value="Item">
                      <asp:MenuItem Text="MenuSub" Value="MenuSub"></asp:MenuItem>
                  </asp:MenuItem>
                  </asp:MenuItem></Items>
          </asp:Menu>

             <asp:GridView ID="GridView1" CssClass="table-condensed" runat="server" DataKeyNames="projectid" >
            <Columns>
            <asp:TemplateField HeaderText="Proje">
            <ItemTemplate>
            <asp:Label ID="lbfid" runat="server" Text='<%#Eval("projectid")%>'></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Doc">
            <ItemTemplate>
            <asp:Label ID="lbfname" runat="server" Text='<%#Eval("video")%>'></asp:Label>
                <asp:HyperLink runat="server" CommandName="view" CommandArgument='<%#Eval("abstract")%>' >download</asp:HyperLink>

            </ItemTemplate>
            </asp:TemplateField>
                </Columns>

        </asp:GridView>
            </form>

    </body>  
    </html>  


   using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class AutoCompleteCity : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        SqlConnection conn = new System.Data.SqlClient.SqlConnection("Your database connection");
        if(mini.SelectedItem.Value=="1")
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM [order]",conn);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}


来源:https://stackoverflow.com/questions/34217130/bind-naviagte-menu-items-in-web-forms-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!