简体   繁体   English

在ASP.NET中使用查询字符串链接按钮

[英]Link Button with query string in ASP.NET

I have a link button as follow 我有一个链接按钮如下

<asp:LinkButton ID="LinkButtonSearchClient" PostBackUrl="~/UI/clients.aspx" runat="server">Recherche </asp:LinkButton>

i want get a query string from it 我想从中获取查询字符串

<asp:LinkButton ID="LinkButtonSearchClient" PostBackUrl="~/UI/clients.aspx?id=12" runat="server">Recherche </asp:LinkButton>

and the id value comes from the source code 并且id值来自源代码

public string ID
    {
        get { return ViewState["id"]; }
    }

To get the value on page load (in the backend .cs file) : 要获取页面加载时的值(在后端.cs文件中):

protected void Page_Load(object sender, EventArgs e)
{
   var id = Request.QueryString["id"];
   if (id != null)
   {
      // use id
   }
}

Or you might want to put the id into the link (in the html) : 或者您可能想将id放入链接(在html中):

<asp:LinkButton ID="LinkButtonSearchClient" runat="server" 
    NavigateUrl='<%# String.Format("~/UI/clients.aspx?id={0}", Eval("ID"))%>' 
    Text='Recherche'></asp:LinkButton>

You probably do not need a postback, look here : PostbackUrl vs NavigateUrl 您可能不需要回发,请看这里: PostbackUrl vs NavigateUrl

Try this, 尝试这个,

public string ID
    {
        get { Request.QueryString["id"]; }
    }

Edit : In your page load set your postback url like this, access postbackurl in server side 编辑:在页面加载中设置这样的回发网址,在服务器端访问postbackurl

LinkButtonSearchClient.PostBackUrl = "~/UI/clients.aspx?id=" + this.ID;

See my following example : 看我下面的例子:

in design as follow 在设计上如下

<asp:HyperLink ID="HLink" runat="server"  
NavigateUrl='<%#"Mobiles_Details.aspx?ID=" + Eval("ID") %>' Text='<%# 
Bind("Name") %>' Height="70px" Width="200px" Font-Bold="true" Font-
Size="10pt"  Font-Names="Times New Roman" />

In coding the class Mobiles_Details is: 在编写类Mobiles_Details是:

public partial class Mobiles_Details : System.Web.UI.Page
{

public string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {

        try
        {
            Session["UserID"] = "100";
            //int ID = 102;
            int ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
            Session["Product_ID"] = ID;

            if (Session["UserID"] == null || Session["UserID"].ToString() == string.Empty)
            {
                Response.Redirect("Login.aspx", false);
            }
            else
            {

                if (ID != null)
                {   

                    DataTable dt = Load_Items_ID(Convert.ToInt32(Session["UserID"].ToString()), ID);

                    lbl_Name.Text = dt.Rows[0]["Name"].ToString();
                    lbl_Price.Text = dt.Rows[0]["Price"].ToString();
                    lbl_Details.Text = dt.Rows[0]["Details"].ToString();
                    img_Product.ImageUrl = dt.Rows[0]["image"].ToString();

                }
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('An Error has been occured ');" + ex.ToString(), true);

        }
    }

}

public DataTable Load_Items_ID(int UserID, int ID)
{
    DataTable Returned_Value = new DataTable();

    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM  Products  where  UserID= " + UserID + " and  Status='False'  and  ID =" + ID))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(Returned_Value);
                }
            }
        }
    }

    return Returned_Value;
}
}

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

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