使用标签值在gridview中进行超链接控制

时间:2014-09-27 14:36:40

标签: c# asp.net gridview hyperlink label

这是我的gridview代码,用于显示来自主数据源的数据透视表

<%@ Page Language="C#" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

protected void btnSend_Click(object sender, EventArgs e)
{

    Response.Redirect("Elect_CrewAccid.aspx?strdt=" + txtstrdt.Text + "&enddt=" + txtenddt.Text + "&reportedto=" + txtrepdt.Text);

} 

 </script>
 <script runat="server">
protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {

        lblstrdt.Text = Request.QueryString["strdt"];
        lblenddt.Text = Request.QueryString["enddt"];
        lblreprtd.Text = Request.QueryString["reportedto"];
        if (Request.QueryString["reportedto"] == "") lblreportedto.Text = "Railway & Railway Board";
        if (Request.QueryString["reportedto"] == "RLY") lblreportedto.Text = "Railway";
        if (Request.QueryString["reportedto"] == "RLBD") lblreportedto.Text = "Railway Board";

    }

}
 </script>
 <script runat="server">

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        switch ((e.Row.FindControl("Label2") as Label).Text)
        {
            case "SPAD":
                e.Row.BackColor = System.Drawing.Color.Pink;
                break;

        }
    }
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
    .style2
    {
        font-size: small;
        font-family: Arial, Helvetica, sans-serif;
    }
    .style3
    {
        font-family: Arial, Helvetica, sans-serif;
        font-weight: bold;
    }
    .style4
    {
        font-family: Tahoma;
    }
    .style6
    {
        font-weight: bold;
    }
    .style7
    {
        font-family: Tahoma;
        font-weight: bold;
    }
    .style8
    {
        font-size: small;
        text-align: center;
        font-family: Tahoma;
    }
    .style9
    {
        font-family: Tahoma;
        font-size: small;
        font-weight: bold;
    }
    .style10
    {
        font-family: Tahoma;
        font-size: small;
    }
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center; font-weight: 700; font-family: 'Comic Sans MS'; color: #0000CC">
    Accident (including SPAD incidences) attributed to Loco Pilots under Administrative
    Control of Electrical</div>
<table align="left" style="font-family: Tahoma; font-size: small">
    <tr>
        <td style="font-family: Arial, Helvetica, sans-serif">
            <b>From Date:</b><asp:TextBox ID="txtstrdt" runat="server" Contenteditable="false" /><span
                class="style2">
                <asp:RequiredFieldValidator ID="DateRequiredFieldValidator" ControlToValidate="txtstrdt"
                    ErrorMessage="Name" Text="Can not be left blank" runat="server" Display="Dynamic"
                    ForeColor="Red" Font-Names="Tahoma" Font-Size="Small" />
                <ajax:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtstrdt"
                    Format="dd-MMM-yy">
                </ajax:CalendarExtender>
            </span>
        </td>
        <td style="margin-left: 80px">
            <span class="style3">Up to Date:</span><asp:TextBox ID="txtenddt" runat="server"
                Contenteditable="false" /><span class="style2">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtenddt"
                        ErrorMessage="Name" Text="Can not be left blank" runat="server" Display="Dynamic"
                        ForeColor="Red" Font-Names="Tahoma" Font-Size="Small" /></span>
        </td>
        <ajax:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="txtenddt"
            Format="dd-MMM-yy">
        </ajax:CalendarExtender>
        <td>
            <asp:Button ID="btnSend" Text="Submit" runat="server" onclick="btnSend_Click"/>
        </td>
    </tr>
</table>
<asp:RadioButtonList ID="txtrepdt" runat="server" Style="font-family: Tahoma; font-size: small">
    <asp:ListItem Text="Reoprted To Rly & Board" Value="" Selected="true"></asp:ListItem>
    <asp:ListItem Text="Reported to Rly" Value="RLY"></asp:ListItem>
    <asp:ListItem Text="Reported to Rly Board" Value="RLBD"></asp:ListItem>
</asp:RadioButtonList>
<div>
    <table align="center">
        <tr>
            <td class="style9" align="center">
                <span class="style10"><strong>Cases From Date: </strong></span><span class="style6">
                    <asp:Label ID="lblstrdt" runat="server" Style="color: #CC0000" CssClass="style10" /></span>
            </td>
            <td class="style11" align="center">
                <span class="style10"><strong>&nbsp;to: </strong></span>
                <asp:Label ID="lblenddt" runat="server" Style="color: #CC0000" CssClass="style9" />
            </td>
            <td class="style9" align="center">
                &nbsp;
            </td>
            <td class="style9" align="center">
                &nbsp;
            </td>
            <td class="style7" align="center">
                <span class="style10"><strong>Reprorted to: </strong></span>
                <asp:Label ID="lblreportedto" runat="server" Style="color: #CC0000;" CssClass="style9" />
            </td>
            <td class="style10" align="center">
                <strong>Category</strong>
            </td>
            <td class="style7" align="center">
                <span class="style4"></span>
                <asp:Label ID="lblreprtd" runat="server" Visible="false" Style="color: #CC0000;"
                    CssClass="style9" />
            </td>
        </tr>
    </table>
</div>
<ajax:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajax:ToolkitScriptManager>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
    CellPadding="4" DataSourceID="SqlDataSource1" ShowFooter="True" ShowHeaderWhenEmpty="True"
    EmptyDataText="Oops.... nothing to diplay! Select another dates." HorizontalAlign="Center"
    OnRowDataBound="GridView1_RowDataBound" BackColor="White" BorderColor="#CC9966"
    BorderStyle="Solid" BorderWidth="1px">
    <Columns>
        <asp:TemplateField HeaderText="Classification" SortExpression="Classification" HeaderStyle-VerticalAlign="Middle"
            HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Middle"
            ItemStyle-Wrap="true">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Classification") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Classification") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                Total
            </FooterTemplate>
            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
            <FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" Font-Bold="True" Font-Names="Tahoma" />
            <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Wrap="True"></ItemStyle>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="CR" SortExpression="CR">
            <EditItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("CR") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>

            <asp:HyperLink ID="LabeCR" runat="server" Text='<%# Eval("CR") %>' NavigateUrl='<%# String.Format("~/detailspage.aspx?Rly=CR&Classification={0}", Eval("Classification")) %>'></asp:HyperLink>

            </ItemTemplate>
            <FooterTemplate>
                <asp:GridView ID="GridViewCR" runat="server" AutoGenerateColumns="False" HorizontalAlign="Center"
                    DataSourceID="SqlDataSourceCR" ShowHeader="False" RowStyle-VerticalAlign="Middle"
                    RowStyle-HorizontalAlign="Center" GridLines="None" SelectedRowStyle-VerticalAlign="Middle"
                    SelectedRowStyle-HorizontalAlign="Center">
                    <Columns>
                        <asp:BoundField DataField="Total" HeaderText="Total" ReadOnly="True" SortExpression="Total"
                            ShowHeader="False" ItemStyle-Font-Bold="true" ItemStyle-Font-Names="Tahoma" ItemStyle-HorizontalAlign="Center"
                            ItemStyle-VerticalAlign="Middle" />
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSourceCR" runat="server" CancelSelectOnNullParameter="false"
                    ConnectionString="<%$ ConnectionStrings:EsafetyConnectionString %>" SelectCommand="safety_summary_TTL"
                    SelectCommandType="StoredProcedure">
                    <SelectParameters>
                        <asp:QueryStringParameter Name="reportedto" QueryStringField="reportedto" Type="String" />
                        <asp:QueryStringParameter Name="strdt" QueryStringField="strdt" Type="DateTime" />
                        <asp:QueryStringParameter Name="enddt" QueryStringField="enddt" Type="DateTime" />
                        <asp:QueryStringParameter DefaultValue="CR" Name="Railway" QueryStringField="Railway"
                            Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ER" SortExpression="ER">
            <EditItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("ER") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:HyperLink ID="LabeCR" runat="server" Text='<%# Eval("ER") %>' NavigateUrl='<%# String.Format("~/detailspage.aspx?Rly=ER&Classification={0}", Eval("Classification")) %>'></asp:HyperLink>
            </ItemTemplate>
            <FooterTemplate>
                <asp:GridView ID="GridViewER" runat="server" AutoGenerateColumns="False" HorizontalAlign="Center"
                    DataSourceID="SqlDataSourceER" ShowHeader="False" RowStyle-VerticalAlign="Middle"
                    RowStyle-HorizontalAlign="Center" GridLines="None" SelectedRowStyle-VerticalAlign="Middle"
                    SelectedRowStyle-HorizontalAlign="Center">
                    <Columns>
                        <asp:BoundField DataField="Total" HeaderText="Total" ReadOnly="True" SortExpression="Total"
                            ShowHeader="False" ItemStyle-Font-Bold="true" ItemStyle-Font-Names="Tahoma" ItemStyle-HorizontalAlign="Center"
                            ItemStyle-VerticalAlign="Middle" />
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSourceER" runat="server" CancelSelectOnNullParameter="false"
                    ConnectionString="<%$ ConnectionStrings:EsafetyConnectionString %>" SelectCommand="safety_summary_TTL"
                    SelectCommandType="StoredProcedure">
                    <SelectParameters>
                        <asp:QueryStringParameter Name="reportedto" QueryStringField="reportedto" Type="String" />
                        <asp:QueryStringParameter Name="strdt" QueryStringField="strdt" Type="DateTime" />
                        <asp:QueryStringParameter Name="enddt" QueryStringField="enddt" Type="DateTime" />
                        <asp:QueryStringParameter DefaultValue="ER" Name="Railway" QueryStringField="Railway"
                            Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total" SortExpression="Total">
            <EditItemTemplate>
                <asp:Label ID="LabelTotal" runat="server" Text='<%# Eval("Total") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="LabelTotal" runat="server" Text='<%# Bind("Total") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:GridView ID="GridViewTotal" runat="server" AutoGenerateColumns="False" HorizontalAlign="Center"
                    DataSourceID="SqlDataSourceTotal" ShowHeader="False" RowStyle-VerticalAlign="Middle"
                    RowStyle-HorizontalAlign="Center" GridLines="None" SelectedRowStyle-VerticalAlign="Middle"
                    SelectedRowStyle-HorizontalAlign="Center">
                    <Columns>
                        <asp:BoundField DataField="Total" HeaderText="Total" ReadOnly="True" SortExpression="Total"
                            ShowHeader="False" ItemStyle-Font-Bold="true" ItemStyle-Font-Names="Tahoma" ItemStyle-HorizontalAlign="Center"
                            ItemStyle-VerticalAlign="Middle" />
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSourceTotal" runat="server" CancelSelectOnNullParameter="false"
                    ConnectionString="<%$ ConnectionStrings:EsafetyConnectionString %>" SelectCommand="safety_summary_TTL"
                    SelectCommandType="StoredProcedure">
                    <SelectParameters>
                        <asp:QueryStringParameter Name="reportedto" QueryStringField="reportedto" Type="String" />
                        <asp:QueryStringParameter Name="strdt" QueryStringField="strdt" Type="DateTime" />
                        <asp:QueryStringParameter Name="enddt" QueryStringField="enddt" Type="DateTime" />
                        <asp:QueryStringParameter  Name="Railway" QueryStringField="Railway"
                            Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
    <HeaderStyle BackColor="#FFFFCC" Font-Bold="True" ForeColor="Black" HorizontalAlign="Center"
        VerticalAlign="Middle" Font-Names="Tahoma" />
    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
    <RowStyle Font-Names="Tahoma" ForeColor="Black" HorizontalAlign="Center" VerticalAlign="Middle"
        BackColor="White" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
    <SortedAscendingCellStyle BackColor="#FEFCEB" />
    <SortedAscendingHeaderStyle BackColor="#AF0101" />
    <SortedDescendingCellStyle BackColor="#F6F0C0" />
    <SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" CancelSelectOnNullParameter="false"
    ConnectionString="<%$ ConnectionStrings:EsafetyConnectionString %>" SelectCommand="safety_summary"
    SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:QueryStringParameter Name="reportedto" QueryStringField="reportedto" Type="String" />
        <asp:QueryStringParameter Name="strdt" QueryStringField="strdt" Type="DateTime" />
        <asp:QueryStringParameter Name="enddt" QueryStringField="enddt" Type="DateTime" />
    </SelectParameters>
</asp:SqlDataSource>
</form>
<p class="style8">
    <strong><a href="../../elocosmainpage.htm">Home</a></strong></p>
</body>
</html>

现在困难在于如何添加'lblstrdt','lblenddt'和&amp;的值。 'lblreprtd'到以下超链接

 <asp:HyperLink ID="LabeCR" runat="server" Text='<%# Eval("CR") %>' NavigateUrl='<%# String.Format("~/detailspage.aspx?Rly=CR&Classification={0}", Eval("Classification")) %>'></asp:HyperLink>

以便我可以获取以下查询字符串以在详细信息页面上显示相应的记录

 "~/detailspage.aspx?Rly=CR&Classification=xxxx&strdt=xx/xx/xx&enddt=xx/xx/xx&reprtd=xxxxx"

或者建议另一种方法/代码来获得所需的结果。即使做了大量的谷歌搜索,我也无法找到解决方案。链接到我的网页,其中需要实现此超链接是 - http://elocos.railnet.gov.in/ElectSafety/Elect_CrewAccid.aspx。请帮忙。

1 个答案:

答案 0 :(得分:1)

试试这个

<asp:HyperLink ID="LabeCR" runat="server" Text='<%# Eval("CR") %>' NavigateUrl='<%# String.Format("~/detailspage.aspx?Rly=CR&Classification={0}&{1}", Eval("Classification"), Request.QueryString.ToString()) %>'></asp:HyperLink>