asp.net hyperlinkfield set navigateurl "http=blabla/Id=" value from database - asp.net

I am new to asp.net. Please help.
I have 2 files. One default.aspx, one default.aspx.cs.
In the default.aspx, I have:
<asp:GridView ID="DataGridView" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2" HeaderStyle-BackColor="#3366FF" HeaderStyle-ForeColor="White">
<rowstyle backcolor="LightCyan" forecolor="Black" font-italic="true"/>
<alternatingrowstyle backcolor="PaleTurquoise" forecolor="Black" font-italic="true"/>
<Columns>
<asp:BoundField ItemStyle-Width="15%" DataField="ID" HeaderText="ID" />
<asp:hyperlinkfield text="Log" navigateurl="http://somelink&RecordId=" target="_blank" ItemStyle-Width="15%" HeaderText="Log" />
<asp:BoundField ItemStyle-Width="15%" HeaderText="Delayed Delivery" />
</Columns>
</asp:GridView>
My questions:
Column: hyperlink Log:
I want to bind it to a column in database called "Base_Id", but when I use DataField="Base_Id" in the hyperlink definition, it gives error;
I want to append the base_Id to the end of the url so that it gives navigateurl="http://somelink&RecordId=baseId_value" ; I tried TemplateField, but Visual Studio doesnot recognize it.
for the delayed delivery, I need to compare the data and do some math. Is there a way for me to add a function in default.aspx.cs? if there's, could anyone give an example please?
Any ideas are appreciated. Thanks a lot. =)

Convert the column
<asp:hyperlinkfield text="Log" navigateurl="http://somelink&RecordId=" target="_blank" ItemStyle-Width="15%" HeaderText="Log" />
to TemplateColumn and do this
<asp:TemplateField HeaderText="Log" ItemStyle-Width="15%">
<ItemTemplate>
<asp:HyperLink runat="server"
NavigateUrl='<%# GetUrl(Eval("Base_Id"))%>'
text="Log"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
and in code-behind
protected string GetUrl(object id)
{
return "http://somelink&RecordId=" + id;
}
Hope this helps!
Regards,
Uroš

Related

click event to update the gridview in ASP

It convenient to do the query by :
<asp: AccessDataSource >
I wish to update the query string and update the result by a click event.
<asp:Button id="Button1"
Text="To Inquiry"
OnClick="doInquiry"
runat="server"/>
How I can achieve to update the query string and do the query in in the "doInquiry" function; BUT, still using the following useful code.
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Northwind.MDB" SelectCommand="SELECT * FROM [Categories]"></asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="AccessDataSource1" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>
Whenever you generate the HTML via the markup and the behavior is different from what you expect, check the generated HTML in your browser. You intended to set the onclick attribute of an HTML tag, however, OnClick is a property pointing to the server-side method, which manages the server-side click event. You intended to use the OnClientClick property.

Dynamic population in gridview

I have a grid view, in which I have 1 column as template field, while other 8 columns are dynamically created.
This template field has a checkbox, while trying to retrieve this checkbox in code-behind file, we get it as null.
When columns are not dynamically created, the checkbox is pretty fine and has a value in code behind.
Below is my code:
<asp:GridView ID="gridResultSet" runat="server" AutoGenerateColumns="false" AllowSorting="true"
OnRowCreated="GridResultSet_RowCreated" OnLoad="GridResultSet_Load" CssClass="reportGrid"
CellPadding="4" OnSorting="GridResultSet_Sorting" OnRowDataBound="GridResultSet_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Reclass">
<ItemTemplate>
<acesec:CheckBox ID="chkReclass" CssClass="CheckBoxListStyle" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="gridrow" />
<AlternatingRowStyle CssClass="gridrow" />
<HeaderStyle CssClass="gridheader" />
</asp:GridView>
System.Web.UI.WebControls.CheckBox chbReclass = gridResultSet.Rows[i].FindControl("chkReclass") as System.Web.UI.WebControls.CheckBox;
Do I need to check something in order to access a template field while creation of dynamic columns?
Any body faced same kind of situation?
Pointers will be highly appreciated.
Regards

Hyperlink in datagrid view

I want to set hyperlink field in datagrid view. When user clicks on that link, a query string should be generated and user should be directed to another page. So how can I set hyperlink to generate query string?
<asp:GridView ID="Griddata" runat="server" AutoGenerateColumns="False" CellPadding="1"
GridLines="Horizontal" Width="1000px" ShowFooter="True" CssClass="grid" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:HyperLinkField HeaderText="ID" DataTextField="rec_id" DataNavigateUrlFields="rec_id"
DataNavigateUrlFormatString="followme.aspx?record={0} " />
<asp:BoundField HeaderText="Login" DataField="LoginName"></asp:BoundField>
</Columns>
</asp:GridView>
This is a sample GridView defined in ASP.NET
You need to specify the <asp:Hyperlinkfield> in the column definition.
In that field, you need to specify the DataTextfield (is what will be displayed on screen in that column), your URL (DataNavigateUrlFormatString) and your parameter that you want to use in that URL (DataNavigateUrlFields)
Note: I'm binding to this grid from code-behind, not through a SqlDatAdaptor but the result is the same.
You will get something like this:
you can do like...
<ItemTemplate>
<asp:HyperLink ID="Edit" runat="server" Text="Edit" NavigateUrl='<%# Eval("DataKeyName", "~/View.aspx?Id={0}") %>' />
</ItemTemplate>
<a href='page.aspx?id=<#Eval("ID")>'>click</a>

I cannot make the headers of my gridview click able for sorting

I have a gridview that I populate based on a query to a db. I'm trying to add sorting on my gridview but I cannot make my column headers clickable. I have allow sorting set to true, and I have my OnSorting event set. My columns are of a few different types. I know the code I need to have in my code behind, but I cannot click on the headers for some reason. Any help on what I'm missing would be appreciated.
<asp:GridView ID="Grid1" runat="server"
AutoGenerateColumns="False"
OnSelectedIndexChanging="Selected_Row_Changing"
DataKeyNames="ApplicationId"
AllowPaging="True"
OnPageIndexChanging="Grid1_PageIndexChanging"
AllowSorting="True"
OnSorting="Grid1_Sorting"
OnRowCreated="OnRowCreated"
OnRowCommand="Grid1_RowCommand"
OnRowDataBound="Grid1_RowDataBound">
<Columns>
<asp:templatefield ...>
<itemtemplate>
<asp:linkbutton .../>
</itemtemplate>
</asp:templatefield>
<asp:BoundField ... />
<asp:HyperLinkField ... />
<asp:ButtonField ... />
</Columns>
</asp:GridView>
You don't have set the SortExpression, have you?
For example:
<asp:boundfield datafield="CompanyName"
headertext="CompanyName"
headerstyle-wrap="false"
sortexpression="CompanyName"/>
Make sure you're not setting the Header Template but rather set the HeaderText attribute for the TemplateField

Query regarding binding the label in gridview ItemTemplate to String

I have a nested gridview lets call parent gridview as gridview1 and child gridview as gridview2.
For each parent gridview's (gridview1) row i'm adding child gridview (gridview2) depending upon the invoice number that is present on that particular row in the gridview1. Here is a screen shot of that output here.
Inside the child gridview (i.e., gridview2) i have a download linkbutton which i add through item template and software title which i databind to gridview2 after filtering the output that i get through List<>. But as you can see the download linkbutton is being rendered first and then software titles next. But i want the software titles to be rendered first and download link button as last column in child gridview (i.e., gridview2).
Here is the code in .aspx page for nested gridviews. I got a suggestion from a fellow member of this forum that I can add label before the download link button and associate it with data source. I just could n't understand it. How can one do that ?
<asp:GridView ID="UserTransactionGridView" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" OnRowDataBound="UserTransactionGridView_RowDataBound"
HorizontalAlign="Center" AllowPaging="true">
<Columns>
<asp:BoundField DataField="Date Of Transaction" HeaderText="Date Of Transaction"
SortExpression="Date Of Transaction" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Invoice Number" HeaderText="Invoice Number" SortExpression="Invoice Number"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="totalAmount" HeaderText="Total Amount" ReadOnly="True"
SortExpression="totalAmount" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label Text="Software Title" ID="softwareLbl" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:GridView ID="gridView2" runat="server" HorizontalAlign="Left" GridLines="None"
ShowHeader="false">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px">
<ItemTemplate>
<asp:LinkButton ID="DownloadLbtn" Text="Download" runat="server" OnClick="DownloadLbtn_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:componentConnString %>"
SelectCommand="SelectUserPreviousHistory" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="xyz" Name="userName" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
By the way here is the code where i'm binding child gridview with datasource (which is arraylist that i filter based on the invoice number present on the parent gridview (i.e., gridview1)).
protected void UserTransactionGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gridView2 = (GridView)e.Row.FindControl("gridView2");
System.Data.DataRowView dr = (System.Data.DataRowView)e.Row.DataItem;
gridView2.AutoGenerateColumns = true;
String x = dr[1].ToString();
softwareTitlesList = SoftwareListRetrieve();
ArrayList titles = new ArrayList();
foreach (SoftwareTitles softwareTitle in softwareTitlesList)
{
if (softwareTitle.InvoiceNumber.Contains(x))
titles.Add(softwareTitle.SoftwareTitle);
}
gridView2.DataSource = titles;
gridView2.DataBind();
softwareTitlesList.Clear();
}
}
BTW I'm using Visual studio 2008, asp.net/c# and no LINQ in my web application project.
Please help me.
Thank you in anticipation
PS: If some one doesn't like this question u may delete it after getting answered rather than down voting or flagging it.
1: Add AutoGenerateColumns="false" to your gv2
2: <%#Container.DataItem %> just before linkbutton
<asp:GridView ID="gridView2" runat="server" HorizontalAlign="Left" GridLines="None" ShowHeader="false" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px">
<ItemTemplate>
<%#Container.DataItem %>
<asp:LinkButton ID="DownloadLbtn" Text="Download" runat="server" OnClick="DownloadLbtn_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can add a separate TemplateField instead of adding in same before the existing one but the you get the idea why it is showing next to linkbutton.

Resources