How to find HTML Label element on Gridview with C# - c#

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

Related

GridView adding column with button

I need to add column with button in GridView ? i tied with asp:button i got error also i tried asp:ButtonField i got this error:
"Error Creating Control - narudzbaGridType 'System.Web.UI.WebControls.ButtonField' does not have a public property named 'ID'.
but i gave ID name to my Button field asp:ButtonField ID="example"
<asp:GridView ID="narudzbaGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Let" HeaderText="Let"/>
<%--<asp:BoundField DataField="Kolicina" HeaderText="Kolicina"/>--%>
</Columns>
</asp:GridView>
You can use TemplateField like this (add to columns block):
<asp:templatefield headertext="Author Name">
<itemtemplate>
<asp:button id="buttonl"
Text= 'Click Me'
runat="server"/>
</itemtemplate>
</asp:templatefield>
Hi you need to add an TemplateField. Everybody like use ImageButton but if you want use other control go ahead.
<asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:ImageButton ID="imgBtnEditar" runat="server" ImageUrl="~/iconos/Image.png" CommandName="edit" ToolTip="Edit">
</asp:ImageButton>
</ItemTemplate>
<ItemStyle Height="8px"></ItemStyle>
</asp:TemplateField>

How to select a cell in datagrid onClick in ASP.net C#

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.

Load Grid view data on scroll down

Hii I am using a grid with more than 1000 rows (from database). I was using Paging. a new requirement is to remove paging and show only 50 records at first load and then when user scrolls the page down and reaches to bottom of grid, show next 50 records and so on.. I really dont have any idea about it can any one plz help .... I am using VB.net with 4.0 framework.
following is my code to display grid
<%# Page Language="VB" MasterPageFile="~/MasterPage/abc.master" AutoEventWireup="false" CodeFile="TM.aspx.vb" Inherits="TM" %>
<asp:Content ContentPlaceHolderID="ContentPlaceHolderMain" runat="server" ID="ctn">
<div>
<asp:GridView ID="gvGrid" runat="server" Visible="false" AutoGenerateColumns="False" BorderStyle="Solid" BorderWidth="1px" BorderColor="#6495ED" BackColor="White" HeaderStyle-BackColor="#6495ED">
<AlternatingRowStyle BackColor="#DDEEFF" />
<Columns >
<asp:TemplateField HeaderStyle-Width="10px">
<HeaderTemplate><asp:CheckBox ID="Chk_All_ALert" runat="server" onClick="checkboxAll_click('gvGrid',this.checked,'chk_Alert');"></asp:CheckBox></HeaderTemplate>
<ItemTemplate>
<input type="checkbox" runat="server" id="chk_Alert" value=<%#Eval("UniqueID")%> onclick="CheckUncheck_All('gv_AlertHistory',this.checked,'gvGrid','chk_Alert');"/>
<%--<input type="hidden" id="hdnIDAlert" value=<%#Eval("SchoolID")%> runat="server"/>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Wrap="false">
<HeaderTemplate>Date/Time</HeaderTemplate>
<ItemTemplate><%#DisplayTime(Eval("SentDate"))%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Email Subject</HeaderTemplate>
<ItemTemplate><%#Eval("Subject")%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Email Text</HeaderTemplate>
<ItemTemplate><%#GetAlertGridMessage(Eval("Message"))%></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</asp:Content>
you should use 'Jscroll' or follow this example this may be helpful to you
http://aspsnippets.com/Articles/Load-data-while-Scrolling-Page-down-with-jQuery-AJAX-and-ASPNet.aspx

Conditional HyperLink in GridView?

I have a gridview with a hyperlink:
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="False"
CssClass="table table-hover table-striped" GridLines="None" >
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="EmployeName">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Bind("EmployeName") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="EmployeID" Visible="False">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("EmployeID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
However, it should only appear as a hyperlink if the employeeID is that of the logged in employee.
I can do all that, but what I do not know is how to make the hyperlink look like a Label. It's easy to have it not link anywhere, but I do not know how to make it look like a label.
Thanks
I believe if you set Enabled="false" it does. If it does not, then the only way to do that is put both a HyperLink and Label in the cell, and show the link when appropriate, and the label when appropriate, hiding the other one (which can be easily done in RowDataBound event).

Is there a way to insert Html into a gridview row?

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.

Categories