[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

Gridview - ID select

I have a column called "id", which is also a primary key, on my table (MS-SQL2005). I am using SQLdatasource to populate data into a Gridview. My question is, how can I populate id column and use it as a select link?

sample :

ID     Orderdate  customername

1     4/28/2009   michael smith
2     4/28/2009   teresa  jones


When click on id # 1 it redirect to a different page.
0
VBdotnet2005
Asked:
VBdotnet2005
  • 10
  • 9
1 Solution
 
David RobitailleAnalyst ProgrammerCommented:
"use it as a select link? "
A select link like this?
 
http://www.asp.net/learn/data-access/tutorial-09-vb.aspx
 Or like this?
http://www.asp.net/learn/data-access/tutorial-10-vb.aspx
or other?
0
 
M3mph15Commented:
Hi,

If you want it as a link so that it redirects to a nother page then replace the column with an item template with a hyperlink in it.

So use <%#DataBinder.Eval(Container.DataItem, "ID")%> where the text of the Hyperlink goes. and then set the link to whatever. So this will show the ID of the record as a hyperlink so when the user clicks on it, it redirects to the link.

Hope this helps
-M3mph15
0
 
VBdotnet2005Author Commented:

M3mph15

I am not sure where to add. Could you help?  
<asp:TemplateField ShowHeader="true" HeaderText="View">
                                <ItemTemplate>
                                    &nbsp;
                                    <asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
David RobitailleAnalyst ProgrammerCommented:
"When click on id # 1 it redirect to a different page "
it it what you what to redirect to a different page? if yes then here an exemple replace the "~/Reports/PriceList/Options.aspx?TemplateID=" with the url you whan
<asp:HyperLink ID="HyperLink1" runat="server"
navigateurl='<%# string.format("~/Reports/PriceList/Options.aspx?TemplateID={0}", Eval("TemplateID"), DataBinder.Eval(Container.DataItem, "ID")) %>'
>
0
 
VBdotnet2005Author Commented:


Currently, I am using Linkbutton. I want to change to Hyperlink.
How can I get this line to work ? I want to get the Id from that column and pass it to the line below. javascript:return Launch is just open to a new window the size I want.

NavigateUrl="javascript:return Launch('review.aspx?id=?????????????"></asp:HyperLink>

--------------------------------------------------------------------

<asp:TemplateField ShowHeader="true" HeaderText="View">
                                <ItemTemplate>
                                    &nbsp;
                                    <asp:HyperLink ID="HyperLink1" runat="server"
                                        Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>'
                                        NavigateUrl="javascript:return Launch('review.aspx?id=114"></asp:HyperLink>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>



-----------------------------------------------------------------------

currently, this work




 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            'e.Row.Cells(0).Style("display") = "none"

            Dim l As LinkButton = DirectCast(e.Row.FindControl("LinkButton1"), LinkButton)
            l.Attributes.Add("onclick", "javascript:return Launch('review.aspx?id=" & e.Row.Cells(0).Text & "')")

            If e.Row.Cells(5).Text.StartsWith("*") Then

                imagStatus = CType(e.Row.FindControl("image1"), System.Web.UI.WebControls.Image)

                imagStatus.Visible = True
                imagStatus.ImageUrl = "check.png"

            Else

                imagStatus = CType(e.Row.FindControl("image1"), System.Web.UI.WebControls.Image)
                imagStatus.Visible = True
                imagStatus.ImageUrl = "star.png"


            End If

        End If

    End Sub

0
 
David RobitailleAnalyst ProgrammerCommented:
i<m not 100% sure balut the (") but you should try this

navigateurl='<%# string.format("javascript:return Launch(""review.aspx?id={0}"")",  DataBinder.Eval(Container.DataItem, "ID")) %>'
 
0
 
VBdotnet2005Author Commented:
Any ideas?

Error      1      The server tag is not well formed.            


    <asp:TemplateField ShowHeader="true" HeaderText="View">
                                <ItemTemplate>
                                    &nbsp;
                                    <asp:HyperLink ID="HyperLink1" runat="server"
                                       
                                     navigateurl='<%# string.format("javascript:return Launch(""review.aspx?id={0}"")",  DataBinder.Eval(Container.DataItem, "ID")) %>'</asp:HyperLink>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
0
 
David RobitailleAnalyst ProgrammerCommented:
you miss a ">"
>'></asp:HyperLink>
 
<asp:HyperLink ID="HyperLink1" runat="server"
                                       
                                     navigateurl='<%# string.format("javascript:return Launch(""review.aspx?id={0}"")",  DataBinder.Eval(Container.DataItem, "ID")) %>'></asp:HyperLink>

0
 
VBdotnet2005Author Commented:
I clicked it, but nothing happended
0
 
David RobitailleAnalyst ProgrammerCommented:
when your mouse is over the link, check in the status bar (at the bottom of IE) you should see the link and what is missing.
You could also do a right click - View  source and search for review.aspx?id
0
 
David RobitailleAnalyst ProgrammerCommented:
wait-a-minute.
Are you trying to put the code in onclick javascript event in the navigateurl?
if you link should just open a new window, you could do it like this:
 <asp:HyperLink ID="HyperLink1" runat="server"
                                       
Target="_blank"
                                     navigateurl='<%# String.format("review.aspx?id={0}",  DataBinder.Eval(Container.DataItem, "ID")) %>'></asp:HyperLink>
 
 
0
 
VBdotnet2005Author Commented:

This my javascript. This will control the size and others I want.  Target="_blank"  is just a new windows.
 
 <script type="text/javascript">
<!-- Begin
function Launch(page) {
OpenWin = this.open(page, "jStyle", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,width=430,height=700");
return false

}
// End -->
</script>  
0
 
VBdotnet2005Author Commented:
When move a mouse over the link, it displayed on the bottom left page.

javascript:return Launch("review.aspx?id=1")
0
 
David RobitailleAnalyst ProgrammerCommented:
ok, then the text is properly parsed, try to set the onclick property instead of the navigateurl property.
dont bother if it not there, it<s a javascript event, so asp.net dont "see" it; but it work.
0
 
VBdotnet2005Author Commented:
Ok I changed it to Hyperlink. It poped up without id #. It showed on the bottow left, the link "http://local/mysite/review.aspx?id=1"   Hmmmm....

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then


            Dim l As HyperLink = DirectCast(e.Row.FindControl("HyperLink1"), HyperLink)

           
            l.Attributes.Add("onclick", "javascript:return Launch('review.aspx?id=" & e.Row.Cells(0).Text & "')")

            If e.Row.Cells(5).Text.StartsWith("*") Then

                imagStatus = CType(e.Row.FindControl("image1"), System.Web.UI.WebControls.Image)

                imagStatus.Visible = True
                imagStatus.ImageUrl = "check.png"

            Else

                imagStatus = CType(e.Row.FindControl("image1"), System.Web.UI.WebControls.Image)
                imagStatus.Visible = True
                imagStatus.ImageUrl = "star.png"


            End If

        End If

    End Sub
0
 
VBdotnet2005Author Commented:
If id visible is true, it works fine. When I hide the Id, visible = false, it won't work. The reason it that I don't want it to display id column and then hyperlin (id) again. What should I do?

  <asp:BoundField DataField="id" HeaderText="id" Visible= "true"
                                SortExpression="Datetime" />  
0
 
David RobitailleAnalyst ProgrammerCommented:
you dont have to set it in the  GridView1_RowDataBound ; just set the onclick property

<asp:HyperLink ID="HyperLink1" runat="server"
                                     onclick ='<%# string.format("javascript:return Launch(""review.aspx?id={0}"")",  DataBinder.Eval(Container.DataItem, "ID")) %>'></asp:HyperLink>
 
0
 
VBdotnet2005Author Commented:
If I set it to onclick, then I can't click


from

<asp:HyperLink ID="HyperLink1" runat="server"
                                     navigateurl='<%# string.format("javascript:return Launch(""review.aspx?id={0}"")",  DataBinder.Eval(Container.DataItem, "ID")) %>'></asp:HyperLink>


to   can't click on hyperlink id


<asp:HyperLink ID="HyperLink1" runat="server"
                                     onclick ='<%# string.format("javascript:return Launch(""review.aspx?id={0}"")",  DataBinder.Eval(Container.DataItem, "ID")) %>'></asp:HyperLink>

0
 
David RobitailleAnalyst ProgrammerCommented:
RHAAAAAAAAAAAA!
it because tou dont have anu "link" set...
try with a "link button" and set it<s onClientClick instead:
<asp:LinkButton ID="HyperLink1" runat="server"
                              OnClientClick='<%# string.format("javascript:return Launch(""review.aspx?id={0}"")",  DataBinder.Eval(Container.DataItem, "ID")) %>'></asp:LinkButton>
0
 
VBdotnet2005Author Commented:
thank you very much
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now