I have a Gridview which is autogenerated by fetching data from various tables.
Now my requirement is i need to make my first column as hyperlink so that if it is clicked then it has to navigate to another page with hyperlinked value.
In the second page i have Lable this label should show the hyperlink value and based on that the data will be populated on the second page.
For example:
I have a gridview with 10 columns..My first column is Emp Id.
If that id is clicked it has to take me to second page and the label control must get this id value and based on that id the rest of the info like emp name emp DOB should be filled.
i am using C# as my code behind.
Can anyone help me to proceed..
Awaiting ur reply
You need the <asp:HyperLinkField />
<asp:GridView>
<Columns>
<asp:HyperLinkField HeaderText="Id" DataTextField="YourID" DataNavigateUrlFields="YourID"
DataNavigateUrlFormatString="SecondPage.aspx?Id={0}" />
</Columns>
</asp:GridView>
You might need to remove the AutoGenerateColumns="true" property and type the fields yourself, selecting only the columns you want to show - using <asp:BoundFied DataField="ColumnName" />
In case you want to pass multiple values in the querystring, separate the fields with comma
DataNavigateUrlFields="YourID, SecondField"
and your format string would be
DataNavigateUrlFormatString="SecondPage.aspx?Id={0}¶m2={1}"
Other links
HyperLinkField.DataNavigateUrlFormatString Property
How to pass multiple values in HyperlinkField
You can use a TemplateField column in your GridView:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="linkToDetails" runat="server" NavigateUrl='Details.aspx?empId=<%# Eval("empId") %>' Text='<%# Eval("empId") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
On the Details.aspx page you should get the empId from the QueryString and get the details from the database.
Related
I've been try to merge the header column from 2 into 1, but the example and i already tried it with something like this
GridView.Controls[0].Controls.AddAt(0, oGridViewRow); the question and the example that floating around was just adding 1 column, and add it in the top of the old column, not merging it. So i want to actually merging 1 column and the rest of the column is still the same. Here is the picture of gridview column that i want to merge :
I want to Merge the "Action" column header from 2 into 1. So below the action column, there will be an edit and delete.
Oh i almost forgot, Here is the gridview code that handle the action column :
<asp:CommandField ShowEditButton="true" HeaderText="Action"/>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="linkDelete" runat="server"
CommandArgument = '<%# Eval("XXX")%>'
OnClientClick = "return confirm('Do you want to delete?')"
Text = "Delete" OnClick = "DeleteDetail"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Although I am strictly against hard-coding, this is what I have, you can write this in 'RowDataBound' event of gridview:-
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[3].ColumnSpan = 2;
e.Row.Cells[4].Visible = false;
e.Row.Cells[3].Text = "Action";
}
Here, You need the change the cell index according to your gridview design.
You can use Templates to customize how your column header and data look like
The below is a very simplified example that should show the desired result
But you will need to work on hadnling Edit and Delete actions your own way
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<div> Action </div>
</HeaderTemplate>
<ItemTemplate>
Edit | Delete
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
As you see, my idea is not merging the headers of both columns. Rather, I put the data of of both columns in a single column under single header. I think it achieves the same result.
I have a gridview and inside one of the columns is a HyperLinkField.
I want to set the url to a page Exemplu.aspx?id={}, where id is the id of the element from a database.
How to set the id for every element?
In the grid I have the id, but when I open the page how to I do Exemplu.aspx?id=50 for example?
Thank you for helping me.
I know that for some of you this isn't an issue.
Like this:
<asp:hyperlinkfield datatextfield="UnitPrice"
datanavigateurlfields="ProductID"
datanavigateurlformatstring="~\details.aspx?ProductID={0}"
target="_blank" />
try the following. Add hyperlink field in column
<COLUMNS>
<ASP:HYPERLINKFIELD text="Detail" datanavigateurlfields="id" datanavigateurlformatstring="Exemplu.aspx?id={0}"></ASP:HYPERLINKFIELD>
</COLUMNS>
I have a requirement to display row number in grid view. What is the best way to display the row number using BoundField or TemplateField?
Note: This need to be done using markup only (without code behind).
Note: When sorting happens, the row number should not be in sequence, the first row should go down with its content.
I have already referred the following:
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/2eead3e3-5cc2-40f7-a91c-8f7942d5329c/
<asp:TemplateField HeaderText="#" >
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
By the way, this solution proposed in article you referred. Why you don't like it and ask here?
The best place to do this would be to use the templatefield
<asp:TemplateField HeaderText="Row Number">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
An examples http://www.devcurry.com/2010/01/add-row-number-to-gridview.html
The BoundField displays the value of specified DataSource field as text.
The TemplateField allows to mix html or make use of web controls.
Please refer to the following explanation to confirm the difference.
http://forums.asp.net/t/1804988.aspx/1
The gridview is rendered as html table. If you don't want to calculate the row number in code behind, you should use JQuery.
var rowCount = $('#myTable tr').length;
And you should fill the table footer with rowCount value.
hi i have a hash table with following set of values
int ID 1
string Name ram
list date 2/3/2011
5/3/2011
Code:
<asp:TemplateField HeaderText="ID">
<ItemStyle Width="200px"> </ItemStyle>
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
its throws error in bind statement
You need to add a Repeator or GridView or Datalist control to bind the list (dates) inside the ItemTemplate of GridView.
Hash tables have keys and values so you can't bind your grid using the key names (as you are doing) because the key names are not properties of the hash table. If anything, you could have columns bound to Key and Value; however, your data has a List of things (dates) and the Gridview won't know how to bind this list to one column. As AVD suggested, you need to have an item template and inside it, have another gridview if you want to bind a data structure like this to a grid view.
I am displaying a table in a grid view in a asp.net webpage. I want the user to select one row in the website and delete it. How can I do that. I have a delete button in the same page, where I will do code behind to drop the row in the database. But my problem is how can user select one row in the table .
<asp:GridView ID="GridView1" runat="server" CssClass="style29">
<Columns>
<asp:TemplateField HeaderText="Send Message to Group">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server"
PostBackUrl='<%# Eval("GroupName", "SendMessage.aspx?GroupName={0}") %>'
Text='Send Message'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Is it possible to do without a check box? Or can I add a delete image to the table in a separate column and on clicking the button the row will be deleted.
You should add:
<asp:CommandField ShowSelectButton="True"/>
on your gridview.
Use ICallbackEventHandler to trigger an action on the server.
But my problem is how can user select one row in the table .
this code adds a Select button:
<asp:CommandField ShowSelectButton="True" />
Now your problem is only to get the server to execute some piece of code, do that using ICallbackEventHandler or Ajax.