-1

我有一个链接按钮如下

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

我想从中得到一个查询字符串

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

并且id值来自源代码

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

3 回答 3

2

要获取页面加载的值(在后端 .cs 文件中):

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

或者您可能希望将 id 放入链接中(在 html 中):

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

您可能不需要回发,请看这里:PostbackUrl vs NavigateUrl

于 2015-05-29T15:10:18.093 回答
1

尝试这个,

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

编辑:在您的页面加载中,像这样设置您的回发网址,在服务器端访问 postbackurl

LinkButtonSearchClient.PostBackUrl = "~/UI/clients.aspx?id=" + this.ID;
于 2015-05-29T15:04:57.610 回答
0

请参阅我的以下示例:

设计如下

<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" />

在编码类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;
}
}
于 2018-01-03T19:34:51.763 回答