Hyperlink a column with relative path value of ano

2019-09-09 06:53发布

I am an extreme beginner and have done quite a bit of searching on this with not much success-- and am not sure if this even possible/makes sense: I am trying to create a job board database and webpage with links to the respective pages of the jobs. I have a gridview in mySQL and ASP.net with Job Title, Date, and relative link/path to pages with further description of the jobs. I'd like to make the Job Title hyperlinked using the relative path from the Link column (hopefully just scripted to generate the full link-- not hard coded for each row). When this page is running, the Link column will most likely be hidden. This is so that the user can edit the link column if anything changes, and not have to edit the code much, if at all.

[Job Title]    [Date Posted]      [Link]
Job 1           June, 2012         /california/job1.asp
Job 2           August, 2012       /newyork/job2.asp
etc..

I would like to be displayed in the browser:

Job Title       Date Posted
[Job 1][1]      June, 2012
[Job 2][2]      August, 2012

Here's the asp code for the table, I know I need a hyperlinkfield or column for Job Title with a navigateurl. But don't know how to connect the two table columns I have above. I'd appreciate any help I can get :)

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <h2>
            Job Postings 
        </h2>
        <p>
            The following are the current open positions:
        </p>
        <p>
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
                EmptyDataText="There are no data records to display." BackColor="White" 
                BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
                ForeColor="Black" GridLines="Horizontal">
                <Columns>
                    <asp:BoundField DataField="JobTitle" HeaderText="JobTitle" 
                        SortExpression="JobTitle" />
                    <asp:BoundField DataField="JobDatePosted" HeaderText="JobDatePosted" 
                        SortExpression="JobDatePosted" DataFormatString="{0:d}" />
                    <asp:BoundField DataField="JobLink" HeaderText="JobLink" 
                        SortExpression="JobLink" />
                </Columns>
                <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
                <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
                <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F7F7F7" />
                <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
                <SortedDescendingCellStyle BackColor="#E5E5E5" />
                <SortedDescendingHeaderStyle BackColor="#242121" />
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
                SelectCommand="SELECT [JobTitle], [JobDatePosted], [JobLink] FROM [jobpostings]">
            </asp:SqlDataSource>
        </p>
    </asp:Content>

If this is a dumb idea, please let me know a better way of doing it?

2条回答
祖国的老花朵
2楼-- · 2019-09-09 07:40

Best practise is using ItemTemplate something like this Add hyperlink control and set/edit/update value programatically. Add editTemplate for editing purpose.

Design.aspx: (Tested code)

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onrowdatabound="GridView1_RowDataBound" 
            onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
         <Columns>
             <asp:TemplateField HeaderText="Id">
                 <ItemTemplate>
                     <asp:Label ID="lblid" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                 </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="JobTitle">
                 <ItemTemplate>
                     <asp:HyperLink ID="jobTitle" Text='<%# Bind("JobTitle") %>'  NavigateUrl='<%# Bind("JobLink") %>'  runat="server"></asp:HyperLink>
                 </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Post Date">
                 <ItemTemplate>
                     <asp:Label ID="lbldatepost" runat="server" Text='<%# Bind("JobDatePosted","{0:dd MMM yyyy}") %>'></asp:Label>
                 </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField >
                 <ItemTemplate>
                     <asp:Label ID="lblLink" runat="server" Text='<%# Bind("JobLink") %>'></asp:Label>
                 </ItemTemplate>
                 <EditItemTemplate>
                     <asp:TextBox ID="txtesetLink" runat="server" Text='<%# Bind("JobLink") %>'></asp:TextBox>
                 </EditItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField ShowHeader="False">
                 <EditItemTemplate>
                     <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                         CommandName="Update" Text="Update"></asp:LinkButton>
                     &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                         CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                 </EditItemTemplate>
                 <ItemTemplate>
                     <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                         CommandName="Edit" Text="Edit"></asp:LinkButton>
                 </ItemTemplate>
             </asp:TemplateField>
         </Columns>

    </asp:GridView>

Code Behind:

 protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
           gvBind();
        }
    }
    public void gvBind()
    {
        SqlDataAdapter dap = new SqlDataAdapter("SELECT id, [JobTitle], [JobDatePosted], [JobLink] FROM [jobpostings]", con);
        DataSet ds = new System.Data.DataSet();
        dap.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        gvBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        gvBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtlink = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtesetLink");
        Label lblid = (Label)GridView1.Rows[e.RowIndex].FindControl("lblid");

        int result = UpdateQuery(txtlink.Text, lblid.Text);

        if (result > 0)
        {
            //lblmsg.text = "Record updated";
            Response.Write("Record updated");
        }
        GridView1.EditIndex = -1;
        gvBind();

    }
    public int UpdateQuery(string setlink,string id)
    {
        SqlCommand cmd=new SqlCommand("update jobpostings set JobLink='"+setlink+"' where id='"+id+"'",con);
        con.Open();
        int temp = cmd.ExecuteNonQuery();
        con.Close();
        return temp;
    }

Note: Hope it almost solved your problem, let me now if any issue.

查看更多
小情绪 Triste *
3楼-- · 2019-09-09 07:41

you can try with this code

<asp:HyperLinkField 
DataNavigateUrlFields=""
DataNavigateUrlFormatString="....aspx?id={0}"
DataTextField=" 
NavigateUrl="" />

Link : http://msdn.microsoft.com/fr-fr/library/zt9c22bx%28v=vs.80%29

查看更多
登录 后发表回答