gridview中的超链接与过程

时间:2013-12-03 13:57:34

标签: c# asp.net gridview hyperlink

我有这个页面buscli.aspx

enter image description here

工作正常,现在griview的来源是存储过程

string valorC = "%" + TextBox1.Text + "%"; numo = DropDownList1.SelectedValue;
        switch (numo)
        { 
            case "Nome":  num3 = 1;  break;
            case "Endereço": num3 = 2; break ;
            case "Telefone": num3 = 3 ; break;
            case "Pedido": num3 = 4; break ;
        }

        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "cazacliente2";
        SqlParameter valor = new SqlParameter("@vbusca", SqlDbType.NVarChar);
        SqlParameter num = new SqlParameter("@bo",SqlDbType.Int );
        valor.Value = valorC   ; num.Value = num3 ; 
        cmd.Parameters.Add(valor); cmd.Parameters.Add(num);
        cmd.Connection = conex1;
        try
        {
            GridView1.EmptyDataText = "Nao se" + numo.ToString()  +"econtraron registros";
            GridView1.DataSource = cmd.ExecuteReader();
            GridView1.DataBind();

        }
        catch (Exception ex)
        { throw ex; }
        finally
        {
            conex1.Close();
            conex1.Dispose();
        }

但现在我需要在超链接中转换列pedido,我已经看到很多youtube示例但都是数据集,我已尝试使用超链接字段的任何结果。

2 个答案:

答案 0 :(得分:2)

您需要使用Gridview的RowDataBound事件来操纵数据的呈现方式。以下是来自MSDN http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound(v=vs.110).aspx

的示例
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Display the company name in italics.
      e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";

    }

  }

答案 1 :(得分:0)

您可以在gridview中简单地添加模板列并添加锚标记。请注意,您应该使用http://使链接正常工作。以下是一个示例。这里ColumnName和ColumnValue是来自DataTable / List / Collection作为数据源的列。

<asp:templatefield>
<itemtemplate>
  <a href='<%string.Format("http://www.yourlink.com/page.aspx?id={0}",
  Eval("ColumnName"))%>' runat="server">Eval("ColumnValue")</a>
</itemtemplate>
</asp:templatefield>

我希望这会对你有所帮助。