How to set the url on a page? - c#

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>

Related

Change URL of Hyperlink in ASP.net gridview itemTemplate dynamically

I am creating a UI that contains an itemTemplate w/in a gridView. inside the ItemTemplate I need a link that will be populated dynamically based on the table values I am binding to the table. In other words, sometimes the link will point to a file on my server, and sometimes it will point to another URL. In essence, I need to be able to check a flag on the table I'm binding to the gridview, and then update each rows itemTemplate based on data in the table for the corresponding row.
So far, I have this markup:
<asp:GridView ID="grdVDocuments"
runat="server"
DataSourceID="sqlDS_wwso"
EnableModelValidation="True"
AutoGenerateColumns="False"
OnRowDataBound="grdVDocuments_RowDataBound"
CssClass="documents_DataTable" AllowPaging="True"
>
<Columns>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<a href="/<%# Eval("fileName") %>" target="_blank" id="lnkContent">
<img src="images/orange_download_cropped.png" alt="" border="0"/></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="displayName" HeaderText="displayName" SortExpression="displayName" />
<asp:BoundField DataField="fileName" HeaderText="fileName" SortExpression="fileName" />
<asp:BoundField DataField="category" HeaderText="category" SortExpression="category" />
<asp:BoundField DataField="sub_category" HeaderText="Sub-Category" SortExpression="sub_category" />
<asp:BoundField DataField="datePosted" HeaderText="datePosted" SortExpression="datePosted" />
</Columns>
</asp:GridView>
and this code-behind, which is bombing because it can't identify the hyperlink
protected void grdVDocuments_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink myHyperLink = e.Row.FindControl("lnkContent") as HyperLink;
myHyperLink.NavigateUrl = "<someOtherURL>";
}
}
The data table has a flag on called isFile which is "bit" dataType. When isFile=1 the URL needs to be ".../", otherwise the URL in the hyperlink of the itemtemplate for each row needs to be set to another field in my table that holds a URL; i.e. "someOtherURL".
Any help is appreciated ;)
Thanks!
You need to use a server control inside the ItemTemplate. So, you need to add runat="server" attribute to the a tag. But then, it is an HtmlAnchor element, and you need to cast as HtmlAnchor instead of Hyperlink like (code-behind):
...
var myHyperLink = e.Row.FindControl("lnkContent") as HtmlAnchor;
myHyperLink.HRef = "<someOtherURL>";
...
Or, you can use <asp:Hyperlink> tag in you aspx markup (instead of html anchor a)

Hyperlink in Gridview which is autogenerated in C#

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}&param2={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.

ASP.NET non-html escaped string to GridView

I have GridView with my DAO as data source rendering some person data (name, surname...). I want to add to rendered table simple link to expanded view with more informations. Ithought I could add html link with POST argument to each row. But every html I try to pass to GridView gets escaped. Can I somehow unescape it? Or is there any other simple way?
It is my private, very quick project, I donĀ“t need any robust solution. Just the simplest and quickest one. Thanks.
It sounds like you want to use an ItemTemplate in your gridview to render HTML controls within the grid:
asp:GridView ID="myGrid" runat="server">
<Columns>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<!--Any HTML can go here, below is a label -->
<label><%# DataBinder.Eval (Container.DataItem, "lastname") %><label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Asp.net table display using grid view and deleting one row in the website

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.

Problems with grid view in ASP.NET

I am displaying a table with user email ID and name in a GRID view in a ASP.NET web Page.
I want a send mail button or link next to each row. SO that when I click the send mail button the useremail in that row will be copied and the page will be redirected to send mail page. How to do it?
BTW i am using MYSQL
You can use a HyperLink field:
<asp:HyperLinkField DataTextField="email" HeaderText="Mail To:" DataNavigateUrlFields="email" Target="_blank" DataNavigateUrlFormatString="SendMail.aspx?email={0}" SortExpression="email" />
hope Thisa helps
Probably it would be like this
<asp:GridView runat="Server" id="GrdVw_Email">
<asp:BoundField DataField="Email" HeaderText="Email"/>
<asp:BoundField DataField="Name" HeaderText="Name"/>
<asp:BoundField DataField="Email" DataFormatString="email.aspx?id={0}" DataText="SendEmail"/>
</asp:GridView>
i just put the code from my mind, but you should get the idea

Categories

Resources