开发者

Asp.Net: DetailsView Control - Sending user to specific page

开发者 https://www.devze.com 2022-12-15 19:46 出处:网络
In classic ASP if you wanted to send a user to a specific page you would send/create a URL like : posts.asp?id=24...the querystring indicating the post.

In classic ASP if you wanted to send a user to a specific page you would send/create a URL like : posts.asp?id=24 ...the querystring indicating the post.

Well, in asp.net using a DetailsView control bound to a data开发者_如何学Cset, how do I do the same thing? In the address bar, all i see is posts.aspx when I'm paging through the records, no querystring part. How do I send a user to posts.aspx?id=24 when its a detailsview control on the page.

Note: I'm interested in sending a user to a specific postid not a specific index in the dataset.

Regards Melt


A quick way to do this would be to bind your details view in page load as:

string id = Request.QueryString["id"];
if(!string.IsNullOrEmpty(id))
{
   myDataSet.Filter = String.Format("id = {0}", id);
}

myDetailsView.DataSource = myDataSet;
myDetailsView.DataBind();

MSDN has more information on filtering and sorting DataSets.

Edit: In order to change to a specific page, maybe do something like this in page load:

int selectedPage = -1;
string id = Request.QueryString["id"];
if(!string.IsNullOrEmpty(id))
{
  for(int i = 0; i< myDataSet.Tables[0].Rows.Count; i++)
  {
     DataRow row = myDataSet.Tables[0].Rows[i];
     if(row["id"].ToString().Equals(id))
     {
         selectedPage = i;
         continue;
     }
  }
}

myDetailsView.DataSource = myDataSet;
myDetailsView.PageIndex = selectedPage;
myDetailsView.DataBind();
0

精彩评论

暂无评论...
验证码 换一张
取 消