简体   繁体   English

如何使用C#给出指向标签的链接并将查询字符串传递给asp.net中的另一个页面

[英]how to give link to the label and pass querystring to another page in asp.net using c#

I have asp.net page with SQL server 2008 as database. 我有SQL Server 2008作为数据库的asp.net页。 I have 3 stored procedures . 我有3个存储过程。 one for total count of users in tbl_users and onother for total man users and last for woman users. 一个用于表示tbl_users中的用户总数,另一个用于表示男性用户总数,最后一个表示女性用户。 and i have 3 labels for display count of records. 我有3个标签来显示记录数。 I Want to give link to the labels so that when we click on label, open another page and run related stored procedure. 我想给标签提供链接,以便当我们单击标签时,打开另一个页面并运行相关的存储过程。 and show detail in grid view. 并在网格视图中显示详细信息。 For eg : enter image description here 例如: 在此处输入图像描述

<a href="show_records.aspx?val="+Label1.Text;"><asp:Label ID="lbl_total_records" runat="server"
</asp:Label></a>

But this code dont work . 但是这段代码不起作用。 please help me. 请帮我。

Use the LinkButton control instead: 改用LinkButton控件:

<asp:LinkButton runat="server" ID="lnk_total_records" />

This functions the same as an HTML anchor tag but allows you to set the attributes from the code behind like so: 此功能与HTML定位标记相同,但允许您从后面的代码中设置属性,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    lnk_total_records.Text = "LinkText";
    lnk_total_records.PostBackUrl = "show_records.aspx?val=LinkText";
}

I think what you are trying to do is this: 认为您正在尝试执行以下操作:

<a href="show_records.aspx?val=<%=lbl_total_records.Text%>"><asp:LabelID="lbl_total_records" runat="server" Text="1"></asp:Label></a>

This will properly concatenate the text of the label to your link. 这样可以将标签文本正确地链接到您的链接。 The "<%= %>" tags are shortcut for Response.Write(). “ <%=%>”标签是Response.Write()的快捷方式。

You can then get the value from your query string on the show_records.aspx page like this: 然后,您可以从show_records.aspx页上的查询字符串中获取值,如下所示:

if (Request.QueryString["val"] != null)
{
     string value = Request.QueryString["val"].ToString();
     //logic here to call the correct stored procedure based on value
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM