查找超链接列 [英] Finding Hyperlinkcolumn

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

问题描述

我有一个带有列的Datagrid:


< asp:HyperLinkColumn

DataTextField =" JobTitle"

DataNavigateUrlField =" PositionID"

DataNavigateUrlFormatString =" AddNewPositions.aspx? PositionID = {0}"

HeaderText ="职位名称"

可见=" True"

SortExpression =" JobTitle" />


我可以通过执行以下操作找到我感兴趣的行:


对于DataGrid1中的每个oGridItem作为DataGridItem。项目

pidLabel = CType(oGridItem.FindControl(" PositionID"),Label)

如果pidLabel.Text = 54则

做点什么

结束如果

next


但是我想让这个专栏的文本变成另一种颜色。问题

是我无法找到它。没有这个ID,所以我不能做一个

的findControl来获得它。


有没有办法找到控件并改变它颜色?


如果我能找到它,我会做类似的事情:


Answer.ForeColor = System.Drawing.Color.Yellow


谢谢,


Tom

解决方案

嗨Tom,


我会采取不同的方法,使用模板栏和助手

功能。


通行证对于辅助函数的PositionID并获取CSS类字符串。


在以下代码中观察坏的换行符...


< ;风格>

.blueclass {color:blue}

.redclass {color:red}

< / style>


< asp:datagrid id =" DataGrid1" runat =" server">

< columns>

< asp:templatecolumn HeaderText =" Job Title">

< itemtemplate>

< asp:HyperLink runat =" server"

CssClass =''<%#

GetCSSClass(DataBinder .Eval(Container," DataItem.PositionID"))%>''

Text =''<%#DataBinder.Eval(Container,

" DataItem.JobTitle")%>''

NavigateUrl =''<%#DataBinder.Eval(Container,

" DataItem.PositionID"," AddNewPositions .aspx?PositionID = {0}")%>''>

< / asp:超链接>

< / itemtemplate>

< / asp:templatecolumn>

< / columns>

< / asp:datagrid>

Private Sub Page_Load _

(ByVal sender As System.Object,_

ByVal e As System.EventArgs)处理MyBase.Load

如果不是IsPostBack那么

DataGrid1.DataSource = C. reateDataSource()

DataGrid1.DataBind()

结束如果

结束子


公共功能GetCSSClass _

(ByVal inVal As Integer)As String

如果inVal = 3那么

返回redclass

结束如果

返回" blueclass"

结束功能


函数CreateDataSource()As DataTable

Dim dt As New DataTable

Dim dr As DataRow

dt.Columns.Add(New DataColumn _

(" PositionID" ,GetType(Int32)))

dt.Columns.Add(New DataColumn _

(" JobTitle",GetType(String)))

dt.Columns.Add(New DataColumn _

(" CurrencyValue",GetType(Double)))

dt.Columns.Add(New DataColumn _

(" Boolean",GetType(Boolean)))

Dim i As Integer

for i = 0 To 4

dr = dt.NewRow()

dr(0)= i

dr(1)=" Jobname" + i.ToString()

dr(2)= 1.23 *(i + 1)

dr(3)=(i = 4)

dt.Rows.Add(博士)

下一页我

返回dt

结束功能''CreateDataSource

让我们知道这是否有帮助?




Microsoft MVP [ASP.NET]

多伦多

" tshad" < TS ********** @ ftsolutions.com>在消息中写道

新闻:OL ************** @ TK2MSFTNGP14.phx.gbl ...

我有一个Datagrid一栏:

< asp:HyperLinkColumn
DataTextField =" JobTitle"
DataNavigateUrlField =" PositionID"
DataNavigateUrlFormatString =" AddNewPositions.aspx? PositionID = {0}"
HeaderText ="职位名称"
Visible =" True"
SortExpression =" JobTitle" />

我可以通过执行以下操作找到我感兴趣的行:

对于DataGrid1.items中的每个oGridItem作为DataGridItem
pidLabel = CType(oGridItem.FindControl(" PositionID")),Label )
如果pidLabel.Text = 54那么
做一些
结束如果
下一页

但是我想让这个专栏的文本变成另一种颜色。
问题是我无法找到它。没有这个ID,所以我不能用findControl来获取它。

有没有办法找到控件并改变颜色?

Answer.ForeColor = System.Drawing.Color.Yellow

谢谢,

Tom




" Ken Cox [Microsoft MVP]" < BA ************ @ sympatico.ca>在消息中写道

新闻:e4 ************** @ TK2MSFTNGP14.phx.gbl ...

嗨汤姆,

我会采用不同的方法,使用模板列和帮助程序
函数。


我喜欢这个主意,因为这意味着我没有必要通过这些项目来找到正确的商品。这个更干净。


但是我似乎无法改变颜色。


我刚刚将你的专栏添加到了我的Datagrid,工作正常。但颜色

根本没有变化。在我的网格中,第3行的PositionID为54.


它实际上似乎正在工作,因为viewsource显示类设置

为蓝色和红色分别是(第3个是红色的)。


这是我正在使用的Datagrid(第1列是你设置的那个 -

工作正常) :


************************************* ************* ************************************* ****

< asp:DataGrid AllowPaging =" false"

AllowSorting =" True"

AutoGenerateColumns =" false" ;

CellPadding =" 3"

CellSpacing =" 0"

ID =" DataGrid1"

runat =" server"

ShowFooter =" false"

ShowHeader =" true"

OnSortCommand =" SortDataGrid"

border =" 0"

style =" margin:5px 5px 5px 5px;填充:5px;
< HeaderStyle Horizo​​ntalAlign =" center" BackColor =" #c0edee"

ForeColor ="#2FABAD" Font-Name =" Verdana,Arial,Helvetica,sans-serif"

Font-Bold =" true"字体尺寸= QUOT;较小" />

< ItemStyle BackColor ="#F2F2F2" Font-Name =" Verdana,Arial,Helvetica,

sans-serif"字体尺寸= QUOT;较小" />

< AlternatingItemStyle BackColor ="#E5E5E5" Font-Name =" Verdana,Arial,

Helvetica,sans-serif"字体尺寸= QUOT;较小" />

< FooterStyle Horizo​​ntalAlign =" center" BackColor ="#E8EBFD"

ForeColor ="#3D3DB6" Font-Name =" Verdana,Arial,Helvetica,sans-serif"

Font-Bold =" true"字体尺寸= QUOT;较小" />

< PagerStyle BackColor =" white" Font-Name =" Verdana,Arial,Helvetica,

sans-serif"字体尺寸= QUOT;较小" />

< Columns>

< asp:templatecolumn HeaderText =" Job Title">

< itemtemplate>

< asp:HyperLink runat =" server"

CssClass =''<%#GetCSSClass(DataBinder.Eval(Container,

) " DataItem.PositionID"))%>''

Text =''<%#DataBinder.Eval(Container," DataItem.JobTitle")%>''

NavigateUrl =''<%#DataBinder.Eval(Container,

" DataItem.PositionID"," AddNewPositions.aspx?PositionID = {0}")%> ;''>

< / asp:超链接>

< / itemtemplate>

< / asp:templatecolumn>

< asp:HyperLinkColumn

DataTextField =" JobTitle"

DataNavigateUrlField =" PositionID"

DataNavigateUrlFormatString =" ; AddNewPositions.aspx? PositionID = {0}"

HeaderText ="职位名称"

可见=" True"

SortExpression =" JobTitle" />

< asp:BoundColumn DataField =" PostedData"

HeaderText =" Date Posted"

ReadOnly =" true" ;

Visible =" True"
SortExpression =" DatePosted" />

< asp:BoundColumn DataField =" Company" ;

HeaderText =" Company"

ReadOnly =" true"
Visible =" True"

SortExpression =" Company" />

< asp:HyperLinkColumn DataNavigateUrlField =" PositionID"

ItemStyle-Width =" 10%"

ItemStyle-Horizo​​ntalAlign =" Center" HeaderText =" Screen Questions"

Text =" Screen"

DataNavigateUrlFormatString =" addScreenQuestions.as px?PositionID = {0}"

/>

< asp:HyperLinkColumn DataNavigateUrlField =" PositionID"

ItemStyle-Width =" 10%"

ItemStyle-Horizo​​ntalAlign =" Center" HeaderText =" Test Questions"

Text =" Test"

DataNavigateUrlFormatString =" addSkillstest.aspx?Po sitionID = {0}" />

< asp:TemplateColumn visible =" false">

< ItemTemplate>

< asp:Label id = QUOT; PositionID" RUNAT = QUOT;服务器" Text =''<%#

DataBinder.Eval(Container," DataItem.PositionID")%>''>

< / asp:Label> ;

< / ItemTemplate>

< / asp:templateColumn>

< / Columns>

< / asp:DataGrid>

*********************************** *************** *********************************** ******


这是GetCSSClass:


公共函数GetCSSClass _

(ByVal inVal作为整数)作为字符串

trace.warn("在GetCSSClass inVal ="& inVal)

如果inVal = 54那么

返回redclass

结束如果

返回" blueclass"

结束功能


这是CSS defs:


..theOrange {

颜色:橙色;

}

..blueclass {color:blue}

..redclass {color:red}

..thankYouMessage {

color:Black;

字体大小:14px;

font-weight:bold;

font-family:Verdana,Arial,Helvetica,sans-serif;

}


和渲染代码:


***************************** ********************* ***************************** **********

< table cellspacing =" 0" CELLPADDING = QUOT; 3英寸规则= QUOT;所有"边界=" 0" border =" 1"

id =" DataGrid1" style =" margin:5px 5px 5px 5px; padding:5px;">

< tr align =" Center" bgcolor =" #C0EDEE">

< td>< font face =" Verdana,Arial,Helvetica,sans-serif"

color =" #2FABAD">< b>职位名称< / b>< / font>< / td>< td>< font face =" Verdana,Arial,

Helvetica,无衬线" color ="#2FABAD">< b>< a

href =" javascript :__ doPostBack(''DataGrid1


_ctl1

I have a Datagrid with a column:

<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="AddNewPositions.aspx? PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>

I can find the row that I am interested in by doing the following:

For each oGridItem as DataGridItem in DataGrid1.items
pidLabel = CType(oGridItem.FindControl("PositionID"),Label)
if pidLabel.Text = 54 then
Do something
end if
next

But I want to make this text for this column a different color. The problem
is that I have no way to find it. There is no ID for this so I can''t do a
findControl to get it.

Is there a way to find the control and change the color?

If I could find it I would do something like:

Answer.ForeColor = System.Drawing.Color.Yellow

Thanks,

Tom

解决方案

Hi Tom,

I''d take a different approach, using a template column and a helper
function.

Pass the PositionID to the helper function and get back a CSS class string.

Watch for bad linebreaks in the following code...

<style>
.blueclass {color:blue}
.redclass {color:red}
</style>

<asp:datagrid id="DataGrid1" runat="server">
<columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass=''<%#
GetCSSClass(DataBinder.Eval(Container, "DataItem.PositionID")) %>''
Text=''<%# DataBinder.Eval(Container,
"DataItem.JobTitle") %>''
NavigateUrl=''<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>''>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
If inVal = 3 Then
Return "redclass"
End If
Return "blueclass"
End Function

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("PositionID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("JobTitle", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Jobname" + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function ''CreateDataSource

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto
"tshad" <ts**********@ftsolutions.com> wrote in message
news:OL**************@TK2MSFTNGP14.phx.gbl...

I have a Datagrid with a column:

<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="AddNewPositions.aspx? PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>

I can find the row that I am interested in by doing the following:

For each oGridItem as DataGridItem in DataGrid1.items
pidLabel = CType(oGridItem.FindControl("PositionID"),Label)
if pidLabel.Text = 54 then
Do something
end if
next

But I want to make this text for this column a different color. The
problem is that I have no way to find it. There is no ID for this so I
can''t do a findControl to get it.

Is there a way to find the control and change the color?

If I could find it I would do something like:

Answer.ForeColor = System.Drawing.Color.Yellow

Thanks,

Tom




"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:e4**************@TK2MSFTNGP14.phx.gbl...

Hi Tom,

I''d take a different approach, using a template column and a helper
function.
I like the idea, as it means I don''t have to go through the items to find
the correct one. This is much cleaner.

But I can''t seem to get it to change the color.

I just added your column into my Datagrid, which works fine. But the color
doesn''t change at all. In my grid the 3rd row has the PositionID as 54.

It actually does seem to be working as the viewsource shows the classes set
to blue and red respectively (3rd one being red).

Here is the Datagrid I am using (the 1st column is the one you set up -
which works fine):

************************************************** *****************************************
<asp:DataGrid AllowPaging="false"
AllowSorting="True"
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
ID="DataGrid1"
runat="server"
ShowFooter="false"
ShowHeader="true"
OnSortCommand="SortDataGrid"
border="0"
style="margin: 5px 5px 5px 5px; padding: 5px;" <HeaderStyle HorizontalAlign="center" BackColor="#c0edee"
ForeColor="#2FABAD" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana, Arial,
Helvetica, sans-serif" Font-Size="smaller" />
<FooterStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<PagerStyle BackColor="white" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<Columns>
<asp:templatecolumn HeaderText="Job Title">
<itemtemplate>
<asp:HyperLink runat="server"
CssClass=''<%# GetCSSClass(DataBinder.Eval(Container,
"DataItem.PositionID")) %>''
Text=''<%# DataBinder.Eval(Container, "DataItem.JobTitle") %>''
NavigateUrl=''<%# DataBinder.Eval(Container,
"DataItem.PositionID", "AddNewPositions.aspx?PositionID={0}") %>''>
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
<asp:HyperLinkColumn
DataTextField="JobTitle"
DataNavigateUrlField="PositionID"
DataNavigateUrlFormatString="AddNewPositions.aspx? PositionID={0}"
HeaderText="Job Title"
Visible="True"
SortExpression="JobTitle"/>
<asp:BoundColumn DataField="PostedData"
HeaderText="Date Posted"
ReadOnly="true"
Visible="True"
SortExpression="DatePosted"/>
<asp:BoundColumn DataField="Company"
HeaderText="Company"
ReadOnly="true"
Visible="True"
SortExpression="Company"/>
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Screen Questions"
Text="Screen"
DataNavigateUrlFormatString="addScreenQuestions.as px?PositionID={0}"
/>
<asp:HyperLinkColumn DataNavigateUrlField="PositionID"
ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="Test Questions"
Text="Test"
DataNavigateUrlFormatString="addSkillstest.aspx?Po sitionID={0}" />
<asp:TemplateColumn visible="false">
<ItemTemplate>
<asp:Label id="PositionID" runat="server" Text=''<%#
DataBinder.Eval(Container, "DataItem.PositionID") %>''>
</asp:Label>
</ItemTemplate>
</asp:templateColumn>
</Columns>
</asp:DataGrid>
************************************************** *****************************************

Here is the GetCSSClass:

Public Function GetCSSClass _
(ByVal inVal As Integer) As String
trace.warn("in GetCSSClass inVal = " & inVal)
If inVal = 54 Then
Return "redclass"
End If
Return "blueclass"
End Function

Here is the CSS defs:

..theOrange {
color:orange;
}
..blueclass {color:blue}
..redclass {color:red}
..thankYouMessage {
color: Black;
font-size: 14px;
font-weight: bold;
font-family:Verdana, Arial, Helvetica, sans-serif;
}

And the rendered code:

************************************************** ***************************************
<table cellspacing="0" cellpadding="3" rules="all" border="0" border="1"
id="DataGrid1" style="margin: 5px 5px 5px 5px; padding: 5px;">
<tr align="Center" bgcolor="#C0EDEE">
<td><font face="Verdana, Arial, Helvetica, sans-serif"
color="#2FABAD"><b>Job Title</b></font></td><td><font face="Verdana, Arial,
Helvetica, sans-serif" color="#2FABAD"><b><a
href="javascript:__doPostBack(''DataGrid1


_ctl1


这篇关于查找超链接列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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