2014年7月13日 星期日

[RESOLVED] Link to Details Page


I have a web page with the following construct:






<%# Eval("WineName")%>

Price Range:<%# Eval("PriceRange")%>....

..and so on. I want to be able to have a link in the item template using the above construct to link to a details page about the item. What is the best way to do this? Example appreciated. Thanks.



Hi,


You can add a link button to your item template and pass the id of selected item as query string


Try the below code


Solution 1







<%# Eval("WineName")%>

Price Range:<%# Eval("PriceRange")%>





Change the DataField name, Queryfield value as per your page.


Solution 2


Another solution is to Use the LinkButton click event


Change your GridView Mark like given below







<%# Eval("WineName")%>

Price Range:<%# Eval("PriceRange")%>





Then in code behind use the below code


 protected void lnkbtn_Click(object sender, EventArgs e)
{
//Creating object of link button from sender
LinkButton ObjlnkButton= (LinkButton)sender;
//Grab the command argument
string IDValue = ObjlnkButton.CommandArgument.ToString();
//Redirect to your details page with id value as query string
Response.Redirect("YourDetailsPage.aspx?ID=" + IDValue.ToString() + "");
}



Hi A2H, I see how both those can work. How would I setup the target page? I assume I would setup a datasource to my SQL and integrate what is being posted from the button or link into the SQL statement? Example appreciated.


 


Thanks



Hi,


As I mentioned earlier you can use the
Request.QueryString
 to pass the value to target page and then in your target page read the value you passed from your soruce page.



To read the value from Query string use the below code


protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.Request.QueryString.HasKeys())
{
//Grab the Query string values
string IDValue = Request.QueryString["ID"].ToString();
}
}

Use this value to reteive value from database and populate other  details in your target page.




Hi,


As a reference you can refer the below link




Hi, I am having some issues, but think I'm close to a solution. Here is my link setup:



WineID is a number in this case like 10. However, when  I get to my target page, WineDetails, I get a null reference exception. Request.Querystring("WineID")  nothing. Here is the page load on my target page (using vb.net):


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


If (HttpContext.Current.Request.QueryString.HasKeys()) Then
Dim MyWineID As String

'Grab the Query string values
MyWineID = Request.QueryString("WineID").ToString()

End If

End Sub


I dont know if this has anyting to do with it, but from my page with the link, the query string is already populated from my previous search. Other words, I use the query string to search for a wine and once I land on that page, I use the link to go to the details page. FYI: as a test, from the page with the link, I also put in a:


WineID:<%# Eval("WineID")%>

to make sure the page was getting the ID field and it is. Just cant seem to send it to the details page using the above. Help appreciated. Thanks!




 



Hi,


Please try with the below code to pass the value from grid view to details page in VB.Net



Use the below code to read the querystring in your target page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim MyWineID As String
'Checking querystring is null or not
If Not [String].IsNullOrEmpty(Request.QueryString("ID").ToString()) Then
'Retreiving the First QueryString Values
MyWineID = Request.QueryString("ID").ToString()
End If
End Sub












Thanks that looks like it is passing the ID to the target page. The only thing I'm having issues with now is integrating that value into my SQL query for the details view control. I am currently using the data source on the html source page:


            ConnectionString="<%$ ConnectionStrings:AdminConnectionString %>"
SelectCommand="SELECT [WineID], [WineName], [WAMrating] FROM [tblWines] ">


Is there a quick and dirty way to pass my id parameter using this construct or do I need to do everyting from the .vb page? Thanks!



Got it. I used:


            ConnectionString="<%$ ConnectionStrings:AdminConnectionString %>"
SelectCommand="SELECT [WineID], [WineName], [WAMrating] FROM [tblWines] where WineID = @WineID ">





Hi,


Glad you acheived your requirement.


I was about to post you the same :):):)


沒有留言:

張貼留言