What is use of Datanavigateurlformatstring of hyperlink in gridview? - asp.net

How to use Datanavigateurlformatstring of hyperlink in gridview?
I want to display page with the name of product like
http://localhost:55724/Admin/Category/Update/computer when I clicked on computer in gridview, it says resource not found.
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="id" HeaderText="Id" />
<asp:HyperLinkField DataNavigateUrlFields="id"
DataTextField="Productname"
HeaderText="Product"
Target="_blank"
NavigateUrl="Searchdetails.aspx?product={0}"
DataNavigateUrlFormatString="~/Product/Searchdetails.aspx?product={0}" />
</Columns>
</asp:GridView>
How to fix this error.

Related

Find column value in Gridview in code behind

Here is a gridview from ASPX page (abbreviated):
<asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView_RowCommand" DataKeyNames="order_no" onrowdatabound="Gridview1_RowDataBound" AllowPaging="false" >
<Columns>
<asp:HyperLinkField DataTextField="order_no" HeaderText="Order Number" ItemStyle-Font-Bold="true" /><asp:HyperLinkField />
<asp:BoundField DataField="id_number" HeaderText="Customer ID" ItemStyle-Font-Bold="true" />
</Columns>
</asp:gridview>
The ASPX page has a textbox with a label "Enter Customer ID" and a Button. When user enters a text and clicks the button, I want in the code behind find the value of Order_no.
You might be looking for something like this
<asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView_RowCommand" DataKeyNames="order_no" onrowdatabound="Gridview1_RowDataBound" AllowPaging="false" >
<Columns>
<asp:TemplateField >
<ItemTemplate>
<%# (Eval("order_no") > 0 ? "Something" : "") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id_number" HeaderText="Customer ID" ItemStyle-Font-Bold="true" />
</Columns>
</asp:gridview>

Display gridview cell values to textbox in asp.net

I'm working with asp.net nested gridview,in which I need to show the gridview cell values to the textbox for editing.
My problem is that I don't know how to display values to textbox if i ve used both template field & boundfield. Here is my aspx.
<Columns>
<asp:TemplateField ItemStyle-Width="10px">
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnsections" runat="server" Style="display: none;">
<asp:HiddenField ID="HdnId" runat="server" Value='<%# Eval("Dept_Name") %>' />
<asp:GridView ID="gvsections" Width="100%" CssClass="table table-bordered table-hover" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvsections_rowdatabound" OnRowEditing="gvsections_rowediting" DataKeyNames="Dept_Name">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="currdept" HeaderText="Deptcurrent" />
<asp:BoundField ItemStyle-Width="150px" DataField="Dept_Name" HeaderText="SectionName" />
<asp:BoundField ItemStyle-Width="150px" DataField="FLDTYPE" HeaderText="Type" HeaderStyle-CssClass=" visible-lg visible-md" ItemStyle-CssClass=" visible-lg visible-md " />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="Dept_Name" HeaderText="Department" />
<asp:BoundField ItemStyle-Width="150px" DataField="FLDTYPE" HeaderText="Type" />
</Columns>
</asp:GridView>
if you are writing the code in selectedindexchanging event of the grid view then try this and it will work
GridViewRow row = gvdepts.Rows[e.NewSelectedIndex];
deptname.Text=row.Cells[1].Text;
I hope this helps
In your code 3 fields contain dept name -- HdnId(Hiddenfield),inner gridview bound field (Dept_Name),Outer gridview bound field(Dept_Name).
1. deptname.Text = (gvdepts.SelectedRow.Cells[1].FindControl("HdnId") as HiddenField).Value;
2. deptname.Text = (gvdepts.SelectedRow.Cells[1].FindControl("gvsections") as GridView).Rows[0].Cells[2].Text;
3. deptname.Text = gvdepts.SelectedRow.Cells[2].Text;
may this link solve your problem:
http://www.aspforums.net/Threads/133072/Edit-Update-Delete-in-Nested-Child-GridView-in-ASPNet/

Adding Drop down inside gridview header

I am new to ASP.net and trying to add dropdownlist and search text boxes under my header fields in Gridview control.
<asp:GridView ID="EmpGridView" runat="server" AutoGenerateColumns="false"
DataKeyNames="EMPLOYEEID"
AllowSorting="True" AllowPaging="true" PageSize="50"
OnPageIndexChanging="EmpGridView_PageIndexChanging" style="margin-right: 52px" OnSelectedIndexChanged="EmpGridView_SelectedIndexChanged"
>
<Columns>
<asp:BoundField DataField="EMPLOYEEID"
HeaderText="Employee ID" ReadOnly="true"
SortExpression="EMPLOYEEID" />
<asp:BoundField DataField="PERSONNAME"
HeaderText="Person Name" ReadOnly="true"
SortExpression="PERSONNAME" />
<asp:BoundField DataField="DIVISIONNAME"
HeaderText="Division Name" ReadOnly="true"
SortExpression="DIVISIONNAME" />
<asp:BoundField DataField="DESIGNATION"
HeaderText="Designation" ReadOnly="true"
SortExpression="DESIGNATION"
/>
<asp:BoundField DataField="CNIC"
HeaderText="CNIC" ReadOnly="true"
SortExpression="CNIC" />
</Columns>
</asp:GridView>
I want following
EMPLOYEEID PERSONNAME DIVISIONNAME <----HeaderText
TextBox control TextBox Control DropDownlist control <------aspcontrols
..data ..data ..data <------ rest is db
..data ..data ..data
Meaning I want my labels there too with my asp.net controls
How should I do it?
So far I tried following but could not work out where to place it? since if I add it separately then there are rows of each control which I don't want and boundfield tag does not permit it in it.
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="searchBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
You need to use HeaderTemplate of the TemplateField for placing the controls on the header
<asp:TemplateField SortExpression="PERSONNAME">
<HeaderTemplate>
<asp:Literal runat="server">Person Name</asp:Literal>
<asp:TextBox runat="server" ID="searchBox"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Literal runat="server" Text='<%# Bind("PERSONNAME") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>

C# How do I create a Hyperlink OnClick event on GridView?

I'm having trouble creating the GridView I want.
I would like the user to get inside the website and see the GridView which is attached to a DB.
Columns are: ID, InsertionTime, Filepath, ProccessedByUser
Now I want the user to click the filepath he/she would like to process. When he/she clicks the filepath, I want their username (logged in with built-in asp website authentication) to be updated (added) into DB.
My markup is standard and I haven't got to manage with code behind.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="starttime" HeaderText="starttime"
SortExpression="starttime" />
<asp:HyperLinkField DataNavigateUrlFields="path" DataTextField="path"
HeaderText="path" />
<asp:BoundField DataField="user" HeaderText="user" SortExpression="user" />
</Columns>
</asp:GridView>
I tried using HyperlinkField but it doesn't seem to support onlick events.
Any suggestions?
Thanks.
I assume you are looking for the LinkButton control which has an OnClick event.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="starttime" HeaderText="starttime"
SortExpression="starttime" />
<asp:TemplateField HeaderText="Path" SortExpression="Filepath">
<ItemTemplate>
<asp:LinkButton ID="LbPath" runat="server"
Text='<%# Eval("Filepath") %>'
CommandName="PathUpdate"
CommandArgument='<%#Bind("path") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="user" HeaderText="user" SortExpression="user" />
</Columns>
</asp:GridView>
Now you can handle the LinkButton's click event or the GridView's RowCommand event.
protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "PathUpdate")
{
string path= e.CommandArgument.ToString();
// do you what you need to do
}
}
Note that i've used a TemplateField which is the most dynamic column type in a GridView since you can add anything you want, even nested GridViews or UserControls.
You could use a ButtonField, and then handle OnRowCommand of the gridview. Example here:
http://msdn.microsoft.com/SV-SE/library/system.web.ui.webcontrols.buttonfieldbase.buttontype.aspx
You can set the ButtonType attribute of ButtonField to display the button as a Link.

How can I pass along the Page Number in a HyperLinkField in a GridView?

I have gridview that I am using paging on. I want to pass along the current page in a asp:Hyperlink so I can send them back to the current page when they are done viewing the details of a record. Is this possible? If it is how can I do this?
<asp:GridView ID="grdObitList" runat="server" allowpaging="true"
PageSize="10" AutoGenerateColumns="false" CssClass="grdClass"
BorderStyle="None" GridLines="None" CellSpacing="2" >
<PagerStyle HorizontalAlign="Center" />
<PagerSettings Position="Bottom" FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" />
<Columns>
<asp:HyperLinkField HeaderText="Name" DataTextField="obit_fullname" DataNavigateUrlFields="obit_id" DataNavigateUrlFormatString="obitDisplay.aspx?oid={0}" />
<asp:BoundField ReadOnly="true" HeaderText="Date" DataField="obit_dod" DataFormatString="{0:d/M/yyyy}" />
<asp:BoundField ReadOnly="true" HeaderText="Resident Of" DataField="obit_resident" />
<asp:BoundField ReadOnly="true" HeaderText="Funeral Home" DataField="obit_funeralhome" />
</Columns>
One way to do it is converting it to a template column, that way you can use normal databind syntaxt to get to it (<%#)

Resources