简体   繁体   中英

Displaying a list of hyperlink from database with c#/asp.net

I'm an asp.net / c# newbie and i'm trying to create a very simple menu that is generated from my database. so far i have managed to extract the table from database and into a data table and from there to an array.

My problem is that i haven't figured out how to use the data in a loop in my page (the menu changes according to user types). I tried using <% =Array %> but i can't seem to use that inside a while/for loop or even assignment line.

Maybe i'm attacking this the wrong way , i guess my question is basically this: How can i use the data i gather from the database (names , urls) in my dynamic page?

here is my current code behind:

    public string[] keep = new string[100];
public void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Response.Write("response in postback");
        getMenu();
    }
}

void getMenu()
{
    Response.Write("response in getmenue");
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "server=(local);database=PhilipsMaterials;Integrated Security=SSPI;";
    con.Open();
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    string sql = "Select * from Materials";
    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    da.Fill(ds);
    dt = ds.Tables[0];
    DataRow[] drowpar = dt.Select();
    int i = 0;
    foreach (DataRow dr in drowpar)
    {
        keep[i] = dr["Material Name"] as string;
        i = i + 1;
    }

    con.Close();

}

Thank you.

You just need to use a data contol like gridview or repeater.You can then bind dataset to those control's datasource property. From my point of view repeater will be best datacontro for dynamic menu..

here is mark up for repeater

  <asp:Repeater ID="Rept" runat="server">
    <HeaderTemplate>
    <ul>
    </HeaderTemplate>
    <ItemTemplate>
    <li><a href='<%#Eval("url") %>'><%#Eval("names") %></a></li>
    </ItemTemplate>
    <FooterTemplate>
    </ul>
    </FooterTemplate>

and here is code to bind dataset

Rept.DataSource = ds;
Rept.DataBind();

A nice and clear way, that is also compatible with header, and UpdatePanel is to use a Literal control inside the page and render on it your menu.

Place a literal somewhere in your page:

<asp:Literal runat="server" ID="txtInfo" EnableViewState="false" />

Then render on code behind your data using a StringBuilder and then place it on this Literal as:

StringBuilder sbRenderOnMe = new StringBuilder();

sbRenderOnMe.Append("<br> Some Text");

txtInfo.Text = sbRenderOnMe.ToString();

Your code will be for example as:

using(SqlConnection con = new SqlConnection())
{
  con.ConnectionString = "server=(local);database=PhilipsMaterials;Integrated Security=SSPI;";
  con.Open();
  DataSet ds = new DataSet();
  DataTable dt = new DataTable();
  string sql = "Select * from Materials";
  SqlDataAdapter da = new SqlDataAdapter(sql, con);
  da.Fill(ds);
  dt = ds.Tables[0];
  DataRow[] drowpar = dt.Select();

  StringBuilder sbRenderOnMe = new StringBuilder();
  foreach (DataRow dr in drowpar)
  {
     sbRenderOnMe.AppendFormat("Some Menu Names : {0}", dr["Material Name"]);
  }

  con.Close();
}

txtInfo.Text = sbRenderOnMe.ToString();

Some notes: Do not use the Response.Write especial from code behind, because you probably write before the first bytes of the page. The Response.Write is direct write on the output - In your case you won to create some data and place them somewhere specific on your page, the Response.Write can not help, can not work on that from code behind because is just direct write on the page.

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