I have a gridview for which I am binding data from a Generic List collection. Currently not connected to DB. All the columns in the GridView are defined as properties(get;set.
I want to have tooltip on one of the columns. The column has a very big description. I want to show only 3 words in the column and the rest of the description should appear in tooltip.
Currently my gridview has only asp:gridview id and runat server
There are no columns, headertemplate and itemtemplate.
Could anyone suggest some idea on this.
Thanks in advance
Well, I don't know how you are getting your columns in place...but I suppose you could put a Label webcontrol in the itemtemplate for the header of each column, and give that label's tooltip property the value you want it to have.
Seems to me like that would be a pretty viable solution for this, if I'm understanding you correctly..
You can use an itme template with a label as mentioned. Bind the label the full description:
<ItemTemplate>
<asp:Label id="l1" runat="server" Text='<%# Eval("Description")' />
</ItemTemplate>
In code-behind, use RowDataBound to process each row accordingly:
protected void RowDB(..)
{
Label l = e.Row.Cells[4].Controls[1] as Label;
if (l == null) return;
string description = l.Text;
l.Text = //Partial text here
}
And the grid will bind each row, the full text is passed to a variable, and you can insert a substring of the text (3 words or such) by assigning the new value to the text property.
What kind of tooltip are you looking for? You could use the tooltip property, or consider using the ACT HoverMenuExtender: http://www.asp.net/AJAX/AjaxControlToolkit/Samples/HoverMenu/HoverMenu.aspx
Related
I'm populating a gridview using a table and there is column which holds link IDs.
There can by many link IDs in the same cell.
If the cell contains one link ID then it works fine. The challenge is to support arbitrary number of links on the same cell.
My code:
<asp:HyperLinkField DataNavigateUrlFields="CWEID"
DataNavigateUrlFormatString="https://cwe.mitre.org/data/definitions/{0}.html"
DataTextField="CWEID" HeaderText="CWE ID" Target="_blank"/>
The cell value retrieved from table can be: [770\n838\n120], thus the cell should show 3 links for each ID.
I searched around, and it seems we can do it statically but not for arbitrary number of links.
Any pointers on how to accomplish this?
My suggestion is to replace HyperLinkField with TemplateField and nested Repeater inside.
There is example :
<asp:TemplateField HeaderText="CWE ID">
<ItemTemplate>
<asp:Repeater runat="server" ID="rptLinks" DataSource='<%# Eval("CWEID").ToString.Replace("\n", "").Split(" ")%>'>
<ItemTemplate>
<%# Container.DataItem%>
<br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
Repeater will split CWEID (with \n, I think that is vbCrLf in vb.net so You can replace \n with vbCrLf, without double quotes) record and place all links in separated <a href.... You'll get all links in that cell. There is no matter how many links are stored into CWEID (one or more).
So, result will be (for example) :
770<br>
838<br>
120<br>
Ot just one link, depend how many links You have in CWEID.
UPDATE :
Code changed for DataSource to <%# Eval("CWEID").ToString.Replace("\n", " ").Split(" ")%>
You could add a handler to the gridview's RowDataBound event. In that event you have the ability to modify the contents of any of the cells being rendered in the current row of the gridview, including the ability to create new HTML elements and insert them. In the code below I'm adding an icon to the first cell in a gridview if the current row has child data to be shown.
'New Bootstrap style toggle button for details view
Dim toggle As New HtmlGenericControl("i")
With toggle
.Attributes("class") = "icon-plus icon-white"
.Attributes("onclick") = "javascript: gvrowtoggle(" & e.Row.RowIndex + (e.Row.RowIndex + 2) & ")"
.Attributes("id") = "toggleBttn"
End With
Then I append the new control to the first cell in the current row:
e.Row.Cells(0).Controls.Add(toggle)
I have a dropdownlist, which is getting bind with the datasource on page load.
On SelectedIndexChanged event I need to get multiple values from SelectedItem
I don't want to get one value and call database/session/viewstate to get object with the value.
I search if I can add multiple DataValueField but seems it is not possible, Thus I am thinking to add an attribute to Item and assign additional value field to this attribute.
How can I bind attribute to DDL Item? I tried DataBinding event but don't know how to write code for it. Can anybody help? This is normal asp.net not MVC.
I also tried following (this works for Repeater control):
<asp:DropDownList ID="SetTypes" IsProfiled="<%# DataBinder.Eval(Container.DataItem, "SiteID") %>" runat="server" Width="250" AutoPostBack="True"></asp:DropDownList>
but it seems Container does not have DataItem property.
Try this out:-
SetTypes.DataSource = new List<string>(){
"Type1","Type2","Type3"};
SetTypes.DataBind();
foreach (ListItem item in SetTypes.Items)
{
//Add multiple attributes
item.Attributes.Add("CustomAttribue", "CustomValue");
}
how can i write this code from aspx page in code behind?
<img src="http://download.skype.com/share/skypebuttons /buttons/call_blue_white_124x52.png" style="border: none;" width="124" height="52" alt="Skype Me™!" />
thanks
Place a PlaceHolder control in the Page where you want this to appear.
Then write
PlaceHolder1.Controls.Add(New LiteralControl("<img src=\"http://download.skype.com/share/skypebuttons/buttons/call_blue_white_124x52.png\" style=\"border: none;\" width=\"124\" height=\"52\" alt=\"Skype Me™!\" />"));
Hopes you are using C#
Edit
If you are looking to add dynamically, then place the above place holder and then use below.
HyperLink hyp=new HyperLink();
hyp.ID="hyp1";
hyp.ImageUrl="http://download.skype.com/share/skypebuttons/buttons/call_blue_white_124x52.png";
hyp.NavigateUrl="skype:MySkype?call";
hyp.Text="Skype Me™!" ;
hyp.ToolTip="Skype Me™!";
hyp.Target="_new";
PlaceHolder1.Controls.Add(hyp);
if you're going to create a DataTable for GridView and add all column names and datas from codebehind and need to rearrange some of columns (like adding hyperlinks or etc.) i think this might be useful.
for my situation i need to rearrange my rows' cells like that (currently i have 6 cells, and want to change 6. value)
in gridview's RowDataBound event
string myVariable = e.Row.Cells[0].Text;//i'm getting value from index number 0
HyperLink hp = new HyperLink();
hp.NavigateUrl = String.Format("some_aspx_page.aspx?myVariable={0}", myVariable);
hp.Text = "Link name";
e.Row.Cells[5].Controls.Add(hp); //and add to index number 5
Is it possible to grab the cell data from Repeater items property such as
Dim test As String = myRepeater.Items(index).DataItem(2)
where myRepeater has x rows and 10 columns and was bound from the DataTable
Seems like it should be trivial, but looks like individual cells CAN NOT be accessed. Please shed some light on this.
Not directly -- remember, repeaters are very, very simple webcontrols and the repeater itself really has a limited idea of what is underneath it.
But you can easily get at items within. The best method is to use a control within the item to help you find stuff. EG, given this repeater "row":
<ItemTemplate>
<asp:Button runat="server" ID="theButton" text="Click Me!" OnClick="doIt" /> <asp:TextBox id="theTextBox" value="Whateeavs" />
</ItemTemplate>
You can use the button's click handler to find other items in the row:
protected void doIt(object s, EventArgs e) {
var c = (Control)s;
var tbRef = c.NamingContainer.FindControl("theTextBox");
var tb = (ITextControl)tbRef;
doSomethingWith(tb.Text);
}
Typically much cleaner than finding things in rows using absolute positioning -- you don't have to worry about indexes and such.
PS: it has been a long time since I laid down significant web forms code, no garuntees I got the class names, etc, exactly right.
' />
'/>
this is my code where i am trying to display checkbox and images
my table has columns
itemID Imagespath
1 item1.jpg
2 item2.jpg
3 item3.jpg
4 item4.jpg
but in output it is showing as i want to show only checkbox and image
but now it is showing checkbox along with the [ID i.e 1 ,2,3 etc ] which i should make as [ID as invisiable in design but in code behind i should be able to get the selected value ID]
is there ant way i can remove the gridview border and row border.
actually i am trying to amke a checkboxlist alaong with the images, right now even images are not getting displayed
thank you
You can leverage the GridView's DataKeys property for this:
<asp:GridView ID="gvImages" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID">
In your code you can loop through the rows and read the data key for that row and get the checkbox like this:
foreach (GridViewRow gvr in gvImages.Rows)
{
int CustomerID = (int)gvImages.DataKeys[gvr.RowIndex].Value;
CheckBox CheckBox1 = (CheckBox)gvr.FindControl("CheckBox1");
}
Text property of Checkboxes not used for server side value. Checkboxes don't have a value property. They have only Checked property for value.
In your case you should add an hidden field in your row that will hold the value of ID column, and you can get it at server side.