By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

Gridview Selection

P: n/a
Is it possible to select an item on a gridview by clicking on it and
then being able to click a link for example that says EditOrder in a
treeview and have the EditOrder page open up viewing what you selected
in the gridview? My EditOrder link is in a sitemap so I don't know if
it is possible becuase you would almost have to pass a variable to the
sitemap. What would be the easiest way to accomplish it?

Feb 21 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
The best approach would be be to add the Edit link in the GridView
rather than in the TreeView. Adding values to the query string is easy
inside the GridView, just add a HyperLinkField and populate the
DataNavigateURLFields and DataNavigateUrlFormatString. However, if you
are stuck using a TreeView to navigate to the Edit Page, you have to
update the URL programatically (someone correct me if I'm wrong about
this). Here's a form with a GridView control and a TreeView.

<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView" runat="server"
DataSourceID="SiteMapDataSource">
</asp:TreeView>

<asp:GridView ID="GridView" runat="server"
DataSourceID="SqlDataSource"
OnSelectedIndexChanged="GridView_SelectedIndexChan ged">
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
<SelectedRowStyle BackColor="#FFFFC0" />
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString
%>"
SelectCommand="SELECT [ord_num], [ord_date], [qty] FROM
[sales]">
</asp:SqlDataSource>
</div>
<asp:SiteMapDataSource ID="SiteMapDataSource" runat="server" />
</form>

And here's the Web.Sitemap:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/Default.aspx" title="Home">
<siteMapNode url="~/EditOrder.aspx" title="Edit Order" />
</siteMapNode>
</siteMap>

To set the querystring on the EditOrder link update it when the
selected item on the grid changes:

protected void GridView_SelectedIndexChanged(object source, EventArgs
e)
{
GridViewRow row = GridView.SelectedRow;
string order_number = row.Cells[1].Text;

TreeNode tn = TreeView.FindNode(Server.HtmlEncode("Home/Edit
Order"));
tn.NavigateUrl = string.Format("~/EditOrder.aspx?ord_num={0}",
order_number);
}

You can improve on the code to get the order_number and find the
TreeNode, but the key is updating the NavigateURL each time the
selected item changes. It's a little ugly, but if you need to use a
TreeView bound to a sitemap file it may be your only option.

-Carl

Feb 21 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.