Using aspnet 3.5, c# - Is there a way to insert Html into a gridview row?
Yes. Use the TemplateField, and then type your html directly into the markup. If the html is suppose to be dynamically created I would use a Literal instead of a Label.
<asp:GridView id="GridView1" runat="server">
<Columns>
<asp:TemplateField headertext="Column1">
<ItemTemplate>
<br />
<h1>
<%# Eval ("DataColumnName") %>
</h1>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Column2">
<ItemTemplate>
<asp:Literal id="Literal1" runat="server" text='<%# Eval ("DataColumnName2") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Simply modify the Text property of a cell.
I haven't tested this, but you should be able to add a Label control to the GridView cell. Then write your HTML to the Label's Text property. The Label should render the HTML.
Related
I have a gridview and it has HTML label element like below:
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="ID" ItemStyle-Width="2%">
<ItemTemplate>
<label id="test" runat="server" title="" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
I want to change title attribute of label on C#. I tried this code below bu it did not work.
Label test= (Label)GridView1.Rows[1].Cells[1].FindControl("test");
test.ToolTip= "abc";
Am I missing something or did something wrong? I know I can use <asp:Label> but I don't want to
I have a logLink column in my database.
I have my hyperlink field in gridView as below :
<asp:HyperLinkField DataNavigateUrlFields="logLink" DataTextField="logLink" DataNavigateUrlFormatString="{0}" Text="Link" ControlStyle-CssClass="hlink" HeaderText="LOG LINK" ItemStyle-Width="6%" ItemStyle-Font-Underline="true" />
But this link is not clickable.
I want the values in the log link column of my db to come here.
Does anyone know how to solve this ?
try this.
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="lnk" runat="server" Target="_blank" Text='<%# Eval("yourText") %>'
NavigateUrl='<%# Eval("logLink") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
I am importing a column in datatable to my grid. Now I want navigate to a new page on selecting a cell in grid by fetching the selected value. I have tried this by including bound field in my grid like
<asp:GridView ID="GDV_1" runat="server" EnableModelValidation="true" AutoGenerateColumns="false" OnSelectedIndexChanged="GDV_1_SelectedIndexChanged" AutoGenerateSelectButton="false" DataKeyNames="SROID">
<Columns>
<asp:BoundField DataField="SRONameinEnglish" HtmlEncode="False" DataFormatString="<a target='_blank' href='Test.aspx?code={0}>ClickHere</a>" />
</Columns>
</asp:GridView>
Doing this way my requirement is achieved but the all cells are displaying Common text Click here instead of showing data from Database.
Please give your suggestion on how to get the value from database into cell and make it clickable. I don't want to use Select button. Please find my current output.
This is my current output I want my data from DB instead of ClickHere.
You can use TemplateField
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnk<%# Eval("SRONameinEnglish")%>"><%# Eval("SRONameinEnglish")%></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
and click of LinkButton put your code to navigate anywhere.
In your case you are binding boundfield with static a tag which have href attribute so your not able to change text on that boundfield from your database.To get your approach you should
use TemplateField and bind data with text attribute using eval keyword as below example.
Try it:
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("name") %>' />
</ItemTemplate>
</asp:TemplateField>
OR
you can also bind link with your hyperlink using NavigateUrl property of hyperlink as below example.
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink id="HyperLink2" NavigateUrl='<%#Eval("YourUrl") %>' Text='<%#Eval("name") %>' runat="server"/>
</ItemTemplate>
</asp:TemplateField>
I hope it will helpful to you.
I have a gridview which contains template fields in which every template field contains a litreal control, and I want to bind that grid view with my DataSet, please look into the code to find more.
Code to create DataSet-
DataTable Record = new DataTable();
Record.Columns.Add("zerker");
DataRow dr = Record.NewRow();
dr["zerker"] = "SomeText";
Record.Rows.Add(dr);
gvCustomres.DataSource = Record;
gvCustomres.DataBind();
Code to create GridView-
<asp:GridView ID="gvCustomres" runat="server" PageSize="4" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Zerker">
<ItemTemplate>
<asp:Literal ID="zerkername" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
Please help me to find correct way to do this.
Thanks,
Your code above is all right if you just want to bind Column "zerker" to your gridiview.
All you are missing is the Text Property for your Literal control.
<asp:Literal ID="zerkername" runat="server" Text='<%# Eval("zerker") %>'>
</asp:Literal>
Hi I am writing some code to display some records in Grid view. I am using Firefox as my Default browser. I wish to wrap a header text. but Firefox is not supporting the wrap property. How can I achieve this?
Use the Div tag to place the Header text. -- before that convert the BOUNDEDFIELD to the TEMPLATEFIELD
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
<Columns>
<asp:TemplateField HeaderText="UserID" SortExpression="UserID">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UserID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UserID") %>'></asp:Label>
</ItemTemplate>
<HeaderTemplate> <div STYLE="word-wrap: break-word">Your HEADER</div>
</HeaderTemplate>
</asp:TemplateField></asp:GridView>
Set attribute HeaderStyle-Wrap="true" in <asp:TemplateField> tag
Please mark as answer if it helps.
Apply a style sheet to the header template. In the style add white-space:normal. It will wrap the header text automatically
For eg, class name is mystyle in stylesheet:
.mystyle
{
white-space:normal;
}
In the aspx page, include Headerstyle-CssClass="myStyle".
HeaderStyle-Wrap="true" or ItemStyle-Wrap="true" cannot wrap header text. Only HeaderStyle-Width = something, say "120px", then it will force the header text to wrap