Gridview Selection

M

MasterChief

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?
 
C

carl

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top