如何将超链接添加到动态创建的GridView? [英] How do I add a hyperlink to a dynamically created GridView?

查看:140
本文介绍了如何将超链接添加到动态创建的GridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将超链接添加到动态生成的第一列(票号).

I would like to add a hyperlink to the first column(Ticket number) that is generated dynamically.

protected void Button2_Click(object sender, EventArgs e)
{
    string TextSearch = txtSearch.Text, CaseNumberQuery=null, DescriptionQuery=null, StrinrQuery=null, ticketsearch=null;
    int SelectedSearch = int.Parse(dbSelectSearch.SelectedValue);
    ticketsearch = txtTicket_No.Text;

    DescriptionQuery = "SELECT  TicketInfo.Ticket_No, u1.FirstName AS [Open By], StatusTypeTable.StatusType_Desc,ApplicationTable.Application_Desc, CommentsTablex.Comments_Text,u2.FirstName AS [Assigned To] FROM TicketInfo,StatusTypeTable, UsersTable u1, UsersTable u2, ApplicationTable, CommentsTablex WHERE StatusTypeTable.Status_id = TicketInfo.StatusType_id AND ApplicationTable.Application_id= TicketInfo.Application_id AND CommentsTablex.Ticket_No = TicketInfo.Ticket_No AND u1.Users_id=TicketInfo.ReportedBy_User_id AND u2.Users_id=TicketInfo.Assigned_User_id AND CommentsTablex.Comments_Text LIKE '%" + TextSearch + "%'";
    CaseNumberQuery = "SELECT TicketInfo.Ticket_No, u1.FirstName AS [Open By], dbo.StatusTypeTable.StatusType_Desc, dbo.ApplicationTable.Application_Desc,CommentsTablex.Comments_Text, u2.FirstName AS [Assigned To] FROM dbo.TicketInfo INNER JOIN dbo.StatusTypeTable ON dbo.TicketInfo.StatusType_id = dbo.StatusTypeTable.Status_id INNER JOIN dbo.ApplicationTable ON dbo.TicketInfo.Application_id = dbo.ApplicationTable.Application_id INNER JOIN CommentsTablex ON dbo.TicketInfo.Ticket_No = CommentsTablex.Ticket_No INNER JOIN dbo.UsersTable AS u1 ON dbo.TicketInfo.ReportedBy_User_id = u1.Users_id INNER JOIN dbo.UsersTable AS u2 ON dbo.TicketInfo.Assigned_User_id = u2.Users_id WHERE (CommentsTablex.Comments_Text LIKE '%" + TextSearch + "%') AND (dbo.TicketInfo.Ticket_No =" + "'" + ticketsearch + "'" + ")";

    /* if ((txtSearch.Text.Length  <  1)||(txtTicket_No.Text.Length < 1))
    {
        Response.Redirect("Search.aspx");
    } */
    //TextSearch = txtSearch;
    SqlDataReader SearchReader;
    SqlConnection SearchConnection = new SqlConnection();
    SearchConnection.ConnectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

    if (SelectedSearch == 1)
    {
       StrinrQuery = DescriptionQuery;
    }
    else
        if (SelectedSearch == 2)
        {
            StrinrQuery = CaseNumberQuery;
        }
        
        SqlCommand SearchCommand = new SqlCommand();
        SearchCommand.CommandText = StrinrQuery;
        SearchCommand.CommandType = CommandType.Text;
        SearchCommand.Connection = SearchConnection;
        SearchCommand.Connection.Open();
        SearchReader = SearchCommand.ExecuteReader(CommandBehavior.CloseConnection);
        
        GridView1.DataSource = SearchReader;
        GridView1.DataBind();

}

推荐答案

通常在网格视图的模板中定义列.另一种更难的方法是处理itemdatabound事件,找到您的列,然后从那里插入HTML.
You define the column in the template for the gridview, typically. The other way, which is harder, is to handle the itemdatabound event, find your column, and insert the HTML from there.


您可以在gridview databind之前创建动态模板列.请参阅下面的链接

动态地将模板列添加到GridView [ http://www.developer.com/net/asp/article.php/3609991/Dynamic-Template-Columns-in-the-ASPNET-20-GridView-Control.htm [
You can create dynamic template columns before gridview databind. Refer the links below

Dynamically Adding Template columns to a GridView[^]

http://www.developer.com/net/asp/article.php/3609991/Dynamic-Template-Columns-in-the-ASPNET-20-GridView-Control.htm[^]


.aspx代码:

< asp:gridview id ="GrdAdminSearch" autogeneratecolumns ="False" runat ="server" datakeynames ="EMAIL"
onrowdatabound ="GrdAdminSearch_RowDataBound">
<列>
< asp:templatefield headertext =用户ID">
< itemtemplate>
< asp:hyperlink id ="HyperLink1" runat ="server" navigationurl =<%#Eval(" EMAIL," MyPage.aspx?Qstring = {0})%>"
text =<%#DataBinder.Eval(Container.DataItem," EMAIL)%>">



< asp:gridview>


.cs代码:

受保护的void GrdAdminSearch_RowDataBound(object sender,GridViewRowEventArgs e)
{

超链接HyperLink1 =(HyperLink)(e.Row.FindControl("HyperLink1"));

字符串Qstring ="ID = id =" + Server.HtmlDecode(HyperLink1.Text);

HyperLink1.NavigateUrl ="MyPage.aspx?" + Qstring;

}
.aspx code:

<asp:gridview id="GrdAdminSearch" autogeneratecolumns="False" runat="server" datakeynames="EMAIL"
onrowdatabound="GrdAdminSearch_RowDataBound">
<columns>
<asp:templatefield headertext="User Id">
<itemtemplate>
<asp:hyperlink id="HyperLink1" runat="server" navigateurl="<%#Eval("EMAIL", "MyPage.aspx?Qstring={0}") %>"
text="<%# DataBinder.Eval(Container.DataItem, "EMAIL") %>">



<asp:gridview>


.cs code:

protected void GrdAdminSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{

HyperLink HyperLink1 = (HyperLink)(e.Row.FindControl("HyperLink1"));

string Qstring = "ID=id=" + Server.HtmlDecode(HyperLink1.Text);

HyperLink1.NavigateUrl = "MyPage.aspx?" + Qstring;

}


这篇关于如何将超链接添加到动态创建的GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆