超链接mysql数据库中另一列相对路径值的列 [英] Hyperlink a column with relative path value of another column in mysql database

查看:190
本文介绍了超链接mysql数据库中另一列相对路径值的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个极端的初学者,在这方面做了很多搜索,并没有取得太大的成功 - 我不确定这是否有可能/有意义:我正在尝试创建一个工作板数据库和网页,并链接到作业的各个页面。在mySQL和ASP.net中有一个GridView,其中包含Job Title,Date和相关链接/页面路径,以及对作业的进一步描述。我想使用链接列中的相对路径来链接工作职位(希望只是编写脚本来生成完整的链接 - 对每行都不进行硬编码)。当此页面正在运行时,链接列很可能会被隐藏。这样,如果有任何更改,用户可以编辑链接列,而且不必编辑太多的代码。

c> [职务] [发布日期] [链接]
工作2012年6月1日/california/job1.asp
工作2012年8月2日/newyork/job2.asp
等。

我想在浏览器中显示:

 职位名称发布日期
[工作1] [1] 2012年6月
[工作2] [2] 2012年8月

这里是表格的asp代码,我知道我需要一个带有导航的Job Title的超链接字段或列。但不知道如何连接上面的两个表格列。 )

 < asp:Content ID =BodyContentrunat =server ContentPlaceHolderID = 搜索Maincontent > 
< h2>
工作发布
< / h2>
< p>
以下是当前未结头寸:


< p>
< asp:GridView ID =GridView1runat =serverAllowPaging =True
AllowSorting =TrueAutoGenerateColumns =FalseDataSourceID =SqlDataSource1
EmptyDataText =没有要显示的数据记录。 BackColor =White
BorderColor =#CCCCCCBorderStyle =NoneBorderWidth =1pxCellPadding =4
ForeColor =BlackGridLines =Horizo​​ntal>
<列>
< asp:BoundField DataField =JobTitleHeaderText =JobTitle
SortExpression =JobTitle/>
< asp:BoundField DataField =JobDatePostedHeaderText =JobDatePosted
SortExpression =JobDatePostedDataFormatString ={0:d}/>
< asp:BoundField DataField =JobLinkHeaderText =JobLink
SortExpression =JobLink/>
< /列>
< FooterStyle BackColor =#CCCC99ForeColor =Black/>
< HeaderStyle BackColor =#333333Font-Bold =TrueForeColor =White/>
< SelectedRowStyle BackColor =#CC3333Font-Bold =TrueForeColor =White/>
< SortedAscendingCellStyle BackColor =#F7F7F7/>
< SortedAscendingHeaderStyle BackColor =#4B4B4B/>
< SortedDescendingCellStyle BackColor =#E5E5E5/>
< SortedDescendingHeaderStyle BackColor =#242121/>
< / asp:GridView>
< asp:SqlDataSource ID =SqlDataSource1runat =server
ConnectionString =<%$ ConnectionStrings:ConnectionString%>
ProviderName =<%$ ConnectionStrings:ConnectionString.ProviderName%>
SelectCommand =SELECT [JobTitle],[JobDatePosted],[JobLink] FROM [jobpostings]>
< / asp:SqlDataSource>
< / p>
< / asp:Content>

如果这是一个愚蠢的想法,请让我知道更好的方法吗?

解决方案

最佳做法是使用ItemTemplate,如下所示:
添加超链接控件, set / edit / update 值以编程方式。添加editTemplate进行编辑。
$ b Design.aspx: (测试代码)

 < asp:GridView ID =GridView1runat =serverAutoGenerateColumns =False
onrowdatabound =GridView1_RowDataBound
onrowcancelingedit =GridView1_RowCancelingEdit
onrowediting =GridView1_RowEditingonrowupdating =GridView1_RowUpdating>
<列>
< asp:TemplateField HeaderText =Id>
< ItemTemplate>
< asp:Label ID =lblidrunat =serverText ='<%#Bind(id)%>'>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =JobTitle>
< ItemTemplate>
< asp:HyperLink ID =jobTitleText ='<%#Bind(JobTitle)%>'NavigateUrl ='<%#Bind(JobLink)%>'runat = 服务器 >< / ASP:超链接>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =发布日期>
< ItemTemplate>
< asp:Label ID =lbldatepostrunat =serverText ='<%#Bind(JobDatePosted,{0:dd MMM yyyy})%>>< / ASP:标签>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField>
< ItemTemplate>
< asp:Label ID =lblLinkrunat =serverText ='<%#Bind(JobLink)%>'>< / asp:Label>
< / ItemTemplate>
< EditItemTemplate>
< asp:TextBox ID =txtesetLinkrunat =serverText ='<%#Bind(JobLink)%>'>< / asp:TextBox>
< / EditItemTemplate>
< / asp:TemplateField>
< asp:TemplateField ShowHeader =False>
< EditItemTemplate>
< asp:LinkBut​​ton ID =LinkBut​​ton1runat =serverCausesValidation =True
CommandName =UpdateText =Update>< / asp:LinkBut​​ton>
& nbsp;< asp:LinkBut​​ton ID =LinkBut​​ton2runat =serverCausesValidation =False
CommandName =CancelText =Cancel>< / asp:LinkBut​​ton> ;
< / EditItemTemplate>
< ItemTemplate>
< asp:LinkBut​​ton ID =LinkBut​​ton1runat =serverCausesValidation =False
CommandName =EditText =Edit>< / asp:LinkBut​​ton>
< / ItemTemplate>
< / asp:TemplateField>
< /列>

< / asp:GridView>

背后的代码:

  protected void Page_Load(object sender,EventArgs e)
{
if(!Page.IsPostBack)
{
gvBind );

$ b $ 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 =记录更新;
Response.Write(记录更新);
}
GridView1.EditIndex = -1;
gvBind();

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

注意:希望它几乎解决了您的问题,让我现在如果有任何问题。


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?

解决方案

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.

这篇关于超链接mysql数据库中另一列相对路径值的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆