I'm using ASP.NET (C#). I have designed a gridview and loaded data from database (SQL Server). That's ok.
Here is advanced requirement:
-> The database contains a field that it cannot be showed while debugging. Because I had wrote:
<asp:BoundField DataField="course_group" HeaderText="Course group" SortExpression="course_group" HeaderStyle-CssClass="hidden" ItemStyle-CssClass="hidden" />
In css:
.hidden
{
display: none;
}
-> Now, I wanna get the value of course_group and set it into gridview row as a "Tooltip". Like this:
Visit W3Schools.com!
I know the way to write a link tooltip, but not for gridview row tooltip.
I think it's difficult, because you might want to many different tooltip text for all gridview rows
Is there anyway to do that?
Can you give me any C# code behind solution (mouseover event) or CSS/Javascript code solution (hover event)?
Thank you!
Try:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.ToolTip = (e.Row.DataItem as DataRowView)["Description"].ToString();
}
}
Reference Here
Related
[This shows the lots count of every step in the process of manufacturing the product, but I want to know if its possible to make a link to know which lots are included in this lots count, I'm using dx:pivotGridField, inside a dx:ASPxPivotGrid all with devexpress, the only thing that I don't know is that if a can put a link on every single data of that column, click on any number to send me a chart with the info of that lots.
Thanks!]1
You can use ValueTemplate
<dx:PivotGridField Area="ColumnArea" AreaIndex="0" FieldName="ConsumptionDate" ID="fieldConsumptionMonth">
<ValueTemplate>
<dx:ASPxHyperLink ID="ASPxHyperLink1" runat="server" ondatabinding="OnHyperLinkDataBinding" NavigateUrl='test.aspx' />
</ValueTemplate>
</dx:PivotGridField>
And then set the text in the backend
protected void OnHyperLinkDataBinding(object sender, EventArgs e) {
ASPxHyperLink link = (ASPxHyperLink)sender;
link.Text = ((PivotGridFieldValueTemplateContainer)link.Parent).Text;
}
I have a dropdownlist inside a template column on a obout grid. Currently i am populating the dropdownlist on page load with a sqldatasource. However, i now have to load the dropdownlist dependent on the value of a certain column. For example: If status = 1, i populate the dropdownlist with a list of available options that pertain to status 1.
<obout:Column ID="colStatus" DataField="wf_status_id" Align="center" HeaderText="Status" HeaderAlign="center" Width="130px" Wrap="true" runat="server" AllowGroupBy="true" AllowFilter="true">
<TemplateSettings EditTemplateId="tmpStatusIDEdit" TemplateId="tmpStatusID" />
</obout:Column>
<obout:GridTemplate runat="server" ID="tmpStatusID" >
<Template>
<%# Container.DataItem["Status"]%>
</Template>
</obout:GridTemplate>
<obout:GridTemplate runat="server" ID="tmpStatusIDEdit" ControlID="ddlStatus" ControlPropertyName="value">
<Template>
<obout:OboutDropDownList runat="server" ID="ddlStatus" Width="100%" Height="200" MenuWidth="215" DataSourceID="sdsStatus" DataTextField="wf_status_text" DataValueField="wf_status_id" />
</Template>
</obout:GridTemplate>
public void OnGridRowDataBound(object sender, Obout.Grid.GridRowEventArgs e)
{
if (e.Row.RowType == Obout.Grid.GridRowType.DataRow)
{
DropDownList ddlStatus = new DropDownList();
ddlStatus = (DropDownList)e.Row.FindControl("ddlStatus");
//LOAD DROP DOWN HERE//
}
}
When i attempt to execute this code, it shows that ddlStatus is null everytime. I have a tried a multitude of ways to get this and for some reason cannot seem to get it. Perhaps aother set of eyes or other ideas could help me out. Please let me know what it is i am doing wrong. Thank you ahead of time
UPDATE:DATABOUND EVENT CODE
<obout:OboutDropDownList runat="server" ID="ddlStatus" Width="100%" Height="200" MenuWidth="215" OnDataBinding="ddlStatus_DataBinding" DataSourceID="sdsStatus" DataTextField="wf_status_text" DataValueField="wf_status_id" />
protected void ddlStatus_DataBinding(object sender, EventArgs e)
{
Obout.Interface.OboutDropDownList ddl = (Obout.Interface.OboutDropDownList)(sender);
string statusID = Eval("wf_status_id").ToString();
}
I added the DataSource because i do not see another way to have the databinding event triggered.
I don't really know anything about obout controls but I have to assume they act very similar to the asp.net controls and have just been extended. With that assumption in mind, I will try and answer your question.
Your RowDataBound event has a few coding issues... for example, I am not sure why you are defining a new DropDownList and then trying to overwrite it with the next line. Also it sounds like the next line is returning null anyways.
First off I suggest not using the DataBound event at the row level. Use the control's DataBinding event as it will localize your code a lot better because you can trigger off the specific control's DataBinding and therefore not have to search for it. If your code changes (markup or codebehind) it is also a lot easier to change as it will not affect other things and there is less room for introducing bugs.
So I would make the following changes to address this:
Change your DropDownList definition to implement DataBinding:
<obout:OboutDropDownList runat="server" ID="ddlStatus" Width="100%" Height="200"
MenuWidth="215" OnDataBinding="ddlStatus_DataBinding" />
Then implement the OnDataBinding event (get rid of your DataBound event if you weren't using it for anything else):
protected void ddlStatus_DataBinding(object sender, System.EventArgs e)
{
// This will point to ddlStatus on the current row that is DataBinding so you
// don't have to search for it.
OboutDropDownList ddl = (OboutDropDownList)(sender);
// Now you can fill the DropDownList and set the default value how ever you like
...
}
You can also see this other question I answered a long time ago to see if it helps as it is doing the same thing but with a Repeater but it is pretty much the same thing as a grid:
Populating DropDownList inside Repeater not working
EDIT: Changed the code to use OboutDropDownList in the DataBinding.
you can find plenty of samples on obout knowledge base and examples. These should help you out: http://www.obout.com/combobox/aspnet_integration_grid.aspx, http://www.obout.com/grid/aspnet_ajax_cascading_comboboxes.aspx
(they refer to comboBox, but you can easily adapt them do a ddl)
I've a gridview with client ID, client name, client contact number and a hyperlink to client detail.
I would like to pass selected clint ID to another asp page (clientDetail.aspx & ClientContact.aspx).
I'm thinking of passing the clientID as session. But how do i go about doing this? Can someone guild me on this as im quite new to this.
& how do i use the passed data in clientDetail.aspx & ClientContact.aspx?
Thanks in advance for your help.
Add two new columns of type HyperLinkField to your gridView as follows. Now clientID is passed as QueryString. One link is here
<asp:HyperLinkField DataNavigateUrlFields="ClientID"
DataNavigateUrlFormatString="~/ClientDetails.aspx?id={0}"
Text="Client Details" />
Assuming that you are selected the grid row with a button or link button you could use the OnRowCommand event
When you wire into this event you can pick out the value you want from the selected item then save it into the Session which will then be available for subsequent pages. In the below exampel I've assumed the value is in a label field so you can pick it out of that control.
void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblMyValue = (Label)e.Row.FindControl("lblMyValue");
Session["myValue"] = lblMyValue .Text;
}
}
There are other variants of this for instance you could store the value you are interested in in the CommandArgument property of the button that you use to select the row. The command argument will then be available in the RowCommand event
void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
string arg = e.CommandArgument;
//.. put into session here
}
And there are alternatives using different events for instance you could use the DataKeys collection of the GridView to store the value you are interested in and pick out the value from there
Markup fragment
<asp:gridview id="CustomersGridView"
//.. more properties
datakeynames="myID"
onselectedindexchanged="MyGridView_SelectedIndexChanged"
runat="server">
Code behind
void MyGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
int index = MyGridView.SelectedIndex;
Session["myValue"] = CustomersGridView.DataKeys[index].Value.ToString();
}
There are a number of alternatives to get this working. I would use the first one detailed if it were me - I've always found it easiest to get to work. You can make the label hidden with Css if you want - if it isn't suitable for the UI. Or use a hidden field (with runat="server"). I'm going to stop - I'm risking confusuing by just typing on.
You should be able to evaluate the clientid field of the asp.net gridview in the navgiate url of the hyperlink and pass it as a query string like so:
<asp:gridview id="OrdersGridView"
datasourceid="OrdersSqlDataSource"
autogeneratecolumns="false"
runat="server">
<columns>
<asp:boundfield datafield="ClientID"
headertext="Client ID"/>
<asp:boundfield datafield="ClientName"
headertext="Client Name"/>
<asp:boundfield datafield="ClientContact"
headertext="Client Contact"/>
<asp:hyperlinkfield text="Details..."
navigateurl="~\details.aspx?clientid='<%# Eval("ClientID") %>'"
headertext="Order Details"
target="_blank" />
</columns>
</asp:gridview>
I am displaying a gridview which should display a column containing image button.
How can I add image button to gridview row dynamically?
I don't want to enter by using template field from design field of the gridview. As this is image button I should be able to capture the event of the same.How to do the same?
i think you can add image to your edit and delete buttons from .aspx file also, i just tried and got it..
1.firstly make the gridview and then add activities like OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting"
i ma showing you jow to add images to edit and delete button for "update" and "cancel" you can proceed in the same way..
now go to source of gridview..
you will see code like this
<asp:CommandField ShowEditButton="True"/>
<asp:CommandField ShowDeleteButton="True"/>
after that change few things like add
<asp:CommandField ShowEditButton="True" ButtonType="Image" EditImageUrl="~/uploads/edit.png" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Image" DeleteImageUrl="~/uploads/delete.png" />
And after that make sure to save your images in 45 * 25 sizes only , and save it in any folder and remember to specify the path as i have done here , my folder is uploads.
Note: don't store images in App_data , it is for storing .mdf database files , any images will not work for you.. if the images is not showing in drop down list while adding imageurl try to add its path physically.
I think you can use template field for this. What you have to do is at the GridView RowDataBound event find the ImageButton you added in the template field and then give an ID or Row number or something which you can use to identify which row the ImageButton is in, as an Attribute of the ImageButton.
You can use this place to give the ImageUrl as well a sample would look like the below one.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton imgbtn = (ImageButton)e.Row.FindControl("imgbtn1");
if (imgbtn != null)
{
imgbtn.Attributes["id"] = e.Row.RowIndex.ToString();
}
}
}
then you can create the click event for ImageButton. In that you can get the id/rowindex of the image button which was clicked and do what ever you want. Event may look like the below
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ImageButton btn = (ImageButton)sender;
string rowindex = btn.Attributes["id"];
}
then in the GridView template field you can add the click event to ImageButtons onClick event. This may look like the below
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:ImageButton runat ="server" ID="imgbtn1" onclick="ImageButton1_Click"/>
</ItemTemplate>
</asp:TemplateField>
I have an ASP.NET GridView control with two asp:CommandField columns that are both using the Select command to perform different tasks. How do I distinguish which column was selected in the OnRowCommand event when both return "Select" when I check the CommandName property of the GridViewCommandEventArgs object?
Here is my source code:
ASPX page:
<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false" OnRowCommand="MyGridView_OnRowCommand">
<Columns>
<asp:CommandField ButtonType="Link" ShowSelectButton="true" SelectText="Click Me!" />
<asp:CommandField ButtonType="Link" ShowSelectButton="true" SelectText="No Click Me!" />
</Columns>
</asp:GridView>
Code behind:
protected void MyGridView_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
string x = e.CommandName //returns "Select" for both asp:CommandField columns
}
Use a button column instead that way you can specify the specific command names and work with it from there
<asp:ButtonField ButtonType="Link" Text="Click Me" CommandName="MyCommand1" />
<asp:ButtonField ButtonType="Link" Text="No Click Me" CommandName="MyCommand2" />
Then you can take action based on e.CommandName
Use the GridViewCommandEventArgs.CommandArgument property !
Well, first do you HAVE to use SELECt as the command? You could set that to something else that makes more sense.
Secondly, you could set the CommandArgument property to different values so you know which one is being clicked.
use the command argument property.
e.commandargument
Or you can dynamically create the button in the command field and set the commandName to anything you want.
in gridview_Load
for (int i = 0; i <= GridView1.Rows.Count - 1; i++) {
linkbutton btnedit = new linkbutton();
GridView1.Rows(i).Cells(3).Controls.Add(btnedit);
//the above is where you want the button to be on the grid
btndel.CommandName = "Select2";
btndel.CommandArgument = "whatever you want";
}
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "Select1" Then
//do stuff
End Sub
The answer you seek is simple and tricky. I had that problem too in my website project, so I surfed the internet for days and not found what I and you were needed.
One day I just thought about the problem alone and made experiments for hours and finally realized that the only easy way to know the column that the button was clicked is in the RowCommand event of the GridView in the CommandName property of the GridViewCommandEventArgs. More correctly probably and comfortable is to edit columns of GridView through the design mode and replace your Command fields to Button fields.
You can give any string/text you want to each button field in its CommandName, so in the RowCommand event you can know which was clicked. Button1 or Button2, but you don't want to give these strings, because you request something that the buttons should select and give to you, so the CommandName should be the word select, but it can be SELECT too and Select and selecT and etc.
The select command can be mention in the CommandName in many forms, and still the system will recognize it. So for example if you have two Button fields in the GridView, whose first CommandName is simply select and the other is SELECT, then when any of them is clicked the RowCommand event raises and
if (e.CommandName == "select")
{
Label1.Text = "You clicked first button";
}
else
{
Label1.Text = "You clicked second button";
}
and the SelectedDataKey of your GridView will contains the data you requested. What other people offered in their answer to differ in CommandName by setting button one to select_one and setting button two to select_two will help you to know if either select_one was clicked or other, but the SelectedDataKey of your GridView will remain null or DataKey instance that doesn't contain the information you need at all.
In other words, the buttons will not select any necessary information, so it is very important to follow my answer. It is the exact, perfect and great solution to your problem!