我启用了GridView
选项。我想以编程方式更改这些链接的样式,但我不确定如何访问它们。我已经使用a
选择器将CSS应用于它们,但我无法通过代码执行此操作。
如何使用C#修改这些按钮的外观?
答案 0 :(得分:0)
尝试以下代码:
<asp:GridView ID="gvUserInfo" runat="server"
onrowdatabound="gvUserInfo_RowDataBound1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkButton" runat="server" Text="Link"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void gvUserInfo_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnk = e.Row.FindControl("lnkButton") as LinkButton;
lnk.ForeColor = System.Drawing.Color.Red;
}
}
答案 1 :(得分:0)
因为我的网格视图是一个表,你可以使用CSS
<html>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="RegNo" DataSourceID="SqlDataSource1" Width="931px" CellPadding="4" ForeColor="#333333" GridLines="None" Height="170px" AutoGenerateDeleteButton="True">
</asp:GridView>
<style>
td > a {
/*button*/
display: inline-block;
margin-bottom: 0;
font-weight: normal;
text-align: center;
vertical-align: middle;
-ms-touch-action: manipulation;
touch-action: manipulation;
cursor: pointer;
background-image: none;
border: 1px solid transparent;
white-space: nowrap;
padding: 4px 8px;
font-size: 14px;
line-height: 1.42857143;
border-radius: 4px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
/*button color*/
color: #ffffff;
background-color: #c71c22;
border-color: #c71c22;
}
td > a:hover {
color: #555555;
text-decoration: none;
background-color: #9a161a;
border-color: #911419;
}