Add query values to an asp Link Button - c#

I have this Link Button here.
<li><asp:LinkButton ID="MyPDF" OnClick="Download_Click" runat="server" Text="My PDF" NavigateUrl='<%# "./DownloadableProducts.aspx?filename=MyPDF" %>'></asp:LinkButton></li>
And it doesn't work, it says "could not find file". I know it's the query string because when I put the path to the file manually, it works like a charm.
I have also tried this:
<li><asp:LinkButton ID="MyPDF" OnClick="Download_Click" runat="server" Text="My PDF" NavigateUrl='<%# "./DownloadableProducts.aspx?filename=" + Eval("ID") %>'></asp:LinkButton></li>
and it didn't work :(

<li>
<asp:LinkButton ID="MyPDF" OnClick="Download_Click"
runat="server" Text="My PDF"
NavigateUrl="~/DownloadableProducts.aspx?filename=MyPDF">
</asp:LinkButton>
</li>

Try this
<li><asp:LinkButton ID="MyPDF" OnClick="Download_Click" runat="server" Text="My PDF" NavigateUrl="./DownloadableProducts.aspx?filename="+'<%# Eval("ID") %>'></asp:LinkButton></li>

Change it to :
<asp:HyperLink ... NavigateUrl='<%# "DownloadableProducts.aspx?filename=" + Eval("ID") %>' />
The LinkButton Control has no property "NavigateUrl". this control just can do a post back for you, if you need link directly to another location use HyperLink instead.

User the CommandArgument property in the LinkButton

<li><asp:LinkButton ID="MyPDF" runat="server" Text="My PDF" NavigateUrl='<%# GetUrl(Eval("Id")) %>'></asp:LinkButton></li>
and then add into a codebehind something like this:
protected string GetUrl(string id) { return ResolveUrl(string.Format("~/DownloadableProducts.aspx?filename={0}", id)); }

<li>
<asp:LinkButton ID="MyPDF" OnClick="Download_Click" runat="server"
Text="My PDF" NavigateUrl="DownloadableProducts.aspx?filename=<%#Eval("ID") %>" >
</asp:LinkButton>
</li>
this will work

Related

How to give if else condition in asp.net c# inside grid template?

I have written like below lines of code
<asp:TemplateField HeaderText="Marketing Document / URL" SortExpression="DocumentActualName">
<ItemTemplate>
<%# (String.IsNullOrEmpty(Eval("DocumentActualName").ToString() ) ? %>
<asp:LinkButton ID="lnkDownload" runat="server" CommandArgument='<%# DataBinder.Eval (Container.DataItem, "ProductDocument") %>'
CommandName="Download" CausesValidation="false" Text='<%# Eval("DocumentActualName") %>'> </asp:LinkButton>
<% : %>
<a id ="lnkUrl" runat="server" href='<%# Eval("URL") %>' Text='<%# Eval("URL") %>'></a>
</ItemTemplate>
</asp:TemplateField>
It's not working. Please help
You need to bring the databinding tags (<%#) outside of the Page.ResolveUrl method, and use single quotes around the href attribute:
href='<%#Page.ResolveUrl(Eval("URL"))%>'
<a href='<%# String.IsNullOrEmpty(Eval("File").ToString()) ? Eval("URL") : Eval("File") %>'> URL </a>

If condition into Gridview Column design

I have a gridview. its data source is a datatable that is loaded from the database. In this gridview, i have a template column.
<asp:TemplateField HeaderText="Product Type" SortExpression="ProductID">
<ItemStyle CssClass="MP-table-tb-display-item" />
<ItemTemplate>
<div class="MP-table-tb-display-main">
<asp:LinkButton ID="lnkview" CommandArgument='<%# Eval("ProductID") %>' CommandName="Viewproduct"
runat="server" CausesValidation="False" OnClick="lnkview_Click"><h4>
<%# Eval("Name") %>
</h4>
</asp:LinkButton>
</div>
<br />
<div class="MP-table-tb-display">
<p>
<span>KEY</span><%# Eval("[product_type_key]") %></p>
<br />
<a target="_blank" href='<%# Eval("SourceURL") %>'>Source</a>
</div>
</ItemTemplate>
</asp:TemplateField>
In this I want Source hyperlink only show when data available into <%# Eval("SourceURL") %>. If I am not able to get the SourceURL value into RowDatabound Event . Please Guide me.
I plan for this too but this is not working properly.
<a target="_blank" href=' <%= Eval("SourceURL")!=null ? Eval("SourceURL") : "style='display: none'" %> />'> Source</a>
use this instead
<asp:hyperlink Target="_blank" NavigateUrl='<%# Eval("SourceURL") %>' Visible = '<%# Eval("SourceURL") == null ? false : true %>' >
Similarly you could use the <a> tag to control its visiblity. The if condition would go in Style attribue and not in href attribute. Something like this
Style=display:Eval('some_val') == null ? none : block
Try this :
<a target="_blank" href='<%#(String.IsNullOrEmpty(Eval("SourceURL").ToString()) ? "" : Eval("SourceURL"))'%> style='display: none'>Source</a>
Thanks

ASP.NET Server Tags

When I use:
<asp:Label id="lbCatId" runat="server" Text='<%# Eval("VideoId") %>' Visible="true" />
I get:
<span id="ctl00_mainContent_ucCoverFlow_rptVideos_ctl00_lbCatId">5</span>
However when I use:
<asp:Image runat="server" href='Play.aspx?VideoId=<%# Eval("VideoId") %>' ID="iThumbnailFileName" CssClass="content" />
I get:
<img id="ctl00_mainContent_ucCoverFlow_rptVideos_ctl01_iThumbnailFileName" class="content" href="Play.aspx?VideoId=<%# Eval("VideoId") %>"
I would like to know why C# isn't generating the 'VideoId' like it is in the first example.
You are printing the string of the command you want to execute instead of executing it.
Use :
'<%# "Play.aspx?VideoId=" + Eval("VideoId") %>'
Instead of :
'Play.aspx?VideoId=<%# Eval("VideoId") %>'
Try using the ImageUrl property instead. The href attribute probably doesn't know how to interpret databinding syntax.
<asp:Image ID="Image1" runat="server" ImageUrl='Play.aspx?VideoId=<%# Eval("VideoId") %>' ...>
Have you tried:
<asp:Image runat="server" href='<%# "Play.aspx?VideoId=" + Eval("VideoId") %>' ID="iThumbnailFileName" CssClass="content" />
Try to change:
href='Play.aspx?VideoId=<%# Eval("VideoId") %>'
to
href='<%# "Play.aspx?VideoId=" + Eval("VideoId") %>'
'<%# Eval("VideoId","Play.aspx?VideoId={0}") %>'

HyperLink with NavigateUrl with Eval(). Where is the mistake?

First I was changing HyperLink.NavigateUrl in code-behind on Page_Load().
But after I decided to do it in design using Eval() method.
<asp:HyperLink runat="server"
NavigateUrl='<%# String.Format("~/Refuse.aspx?type={0}&id={1}", Eval("type"), Eval("id")) %>' Text="Refuse" />
or
<asp:HyperLink ID="urlRefuse" runat="server"
NavigateUrl='<%# String.Format("~/Refuse.aspx?type={0}&id={1}", Request["type"], Request["id"]) %>' Text="Refuse" />
where id and type - are variables from Request.
But it doesn't work. Only raw text 'Refuse' is shown. Where is my mistake? Thanks in advance.
this is working great
NavigateUrl='<%# Eval("type","~/Refuse.aspx?type={0}") %>'
This worked for me
NavigateUrl='<%# String.Format("{0}.aspx?ID={1}", DataBinder.Eval(Container.DataItem, "Category"), DataBinder.Eval(Container.DataItem, "Post_ID")) %>'
Try and ViewSource in your browser, what's being rendered to the client in your href? Is it what you expected?. If you are trying to use variables from the request collection you can't use Eval, you need to use the Request query string parameters.
<asp:HyperLink runat="server"
NavigateUrl='<%# String.Format("~/Refuse.aspx?type={0}&id={1}", Request["type"], Request["id"]) %>' Text="Refuse" />
Try this:
HttpUtility.UrlEncode(Eval("type")
Try this one:
<asp:HyperLink ID="HyperLink2" runat="server" onclick='<%# String.Format("AcceptUser({0},{1})",Eval("UserId"), Eval("TsId")) %>' NavigateUrl="javascript:void(0)" Visible='<%# (bool)Eval("CanDelete") %>'>Accept</asp:HyperLink>
Try this it worked for me:
Eval("type").ToString()

Getting Failed to load viewstate error

First off, I am not dynamically creating any controls. This is the order I take to produce the error:
I have a listview on the page, when I click the edit link under the listview, I display a panel which is hidden by default. The panel has a few buttons on it along with some listboxes. When I click an item in the listbox or click one of the buttons, I get the following error:
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
Again, I am not creating anything dynamically, I am just hiding the panel with the controls by default and then displaying them, so I am not sure why I am getting this error.
Here is some code:
PAGE LOAD
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["Albums"] = null;
Albums = AlbumCollection.GetAlbums(Common.GetUserName(),
ddlAlbumType.SelectedIndex);
lvwAlbums.DataSource = Albums;
lvwAlbums.DataBind();
}
}
When I click the edit link, this is the code that runs:
protected void lvwAlbums_RowEditing(object sender, ListViewEditEventArgs e)
{
this.AlbumId = int.Parse(
this.lvwAlbums.DataKeys[e.NewEditIndex].Values["AlbumId"].ToString());
this.AlbumName=
this.lvwAlbums.DataKeys[e.NewEditIndex].Values["AlbumName"].ToString();
Album album = new Album(this.AlbumId);
ViewState["AlbumId"] = this.AlbumId;
ViewState["AlbumName"] = this.AlbumName;
pnlAlbum.Visible = true; // This panel holds the controls
btnEditAlbum.Visible = true;
btnCancel.Visible = true;
EditAlbum(this.AlbumId);
this.lvwAlbums.EditIndex = e.NewEditIndex;
AlbumCollection.GetAlbums(Common.GetUserName(),ddlAlbumType.SelectedIndex);
}
If I click the cancel button, I get the error, but it also happens if click another button on the panel such as Add/Remove... Here is the code for the Cancel button:
pnlAlbum.Visible = false;
this.lvwAlbums.EditIndex = -1;
AlbumCollection.GetAlbums(Common.GetUserName(), ddlAlbumType.SelectedIndex);
Here is the aspx/html for the ListView:
<asp:ListView ID="lvwAlbums"
runat="server"
GroupItemCount="5"
DataKeyNames="AlbumId,AlbumName"
OnItemEditing="lvwAlbums_RowEditing"
OnItemCommand="lvwAlbums_ItemCommand"
OnItemDeleting="lvwAlbums_RowDeleting"
OnSelectedIndexChanging="lvwAlbums_SelectedIndexChanging"
OnPagePropertiesChanging="lvwAlbums_PagePropertiesChanging">
<EditItemTemplate>
<td>
<div>
<asp:TextBox ID="txtAlbumName" runat="server"
Text='<%# Eval("AlbumName").ToString().Trim() %>' />
<asp:LinkButton ID="lnkView" runat="server" Text="View" CommandName="View"
CommandArgument='<%# Eval("AlbumId") %>'>
</asp:LinkButton>
|
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit"
CommandArgument='<%# Eval("AlbumId") %>'>
</asp:LinkButton>
|
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
CommandArgument='<%# Eval("AlbumId") %>'>
</asp:LinkButton>
<br />
<span>Songs:
<%# Eval("total") %></span>
</div>
</td>
</EditItemTemplate>
<LayoutTemplate>
<asp:DataPager runat="server" ID="ItemDataPager" PageSize="20"
PagedControlID="lvwAlbums">
<Fields>
<asp:NumericPagerField ButtonType="Link" NumericButtonCssClass="pager" />
</Fields>
</asp:DataPager>
<table>
<tr>
<td>
<table>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder"></asp:PlaceHolder>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<asp:Literal ID="litAlbumName" runat="server"
Text='<%# Eval("AlbumName").ToString().Trim() %>' />
<br />
<asp:LinkButton ID="lnkView" runat="server" Text="View" CommandName="View"
CommandArgument='<%# Eval("AlbumId") %>'>
</asp:LinkButton>
|
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit"
CommandArgument='<%# Eval("AlbumId") %>'>
</asp:LinkButton>
|
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
CommandArgument='<%# Eval("AlbumId") %>'>
</asp:LinkButton>
<br />
<span>Songs:
<%# Eval("total") %></span>
</td>
</ItemTemplate>
</asp:ListView>
Here is the markup for the Panel:
<asp:Panel ID="pnlAlbum" runat="server" Visible="false">
<asp:ListBox ID="lstAvailableSongs" runat="server" SelectionMode="Multiple">
</asp:ListBox>
<asp:Button ID="btnAddAll" runat="server" Text="Add All" OnClick="btnAddAll_Click" />
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
<asp:Button ID="btnRemove" runat="server" Text="Remove" OnClick="btnRemove_Click" />
<asp:Button ID="btnRemoveAll" runat="server"
Text="Remove All"OnClick="btnRemoveAll_Click" />
<asp:ListBox ID="lstSelectedSongs" runat="server" SelectionMode="Multiple">
</asp:ListBox>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />
<asp:Button ID="btnEditAlbum" runat="server"Text="Save"
ValidationGroup="CreateAlbum" OnClick="btnEditAlbum_Click" />
<asp:Button ID="btnSaveAs" runat="server" Text="Save As" ValidationGroup="CreateAlbum"
OnClick="btnSaveAs_Click" />
</asp:Panel>
Here is some extra info:
I put an update panel around one of the listboxes in the panel and when I clicked the edit link under a listview item, I received the following error:
Microsoft JScript runtime error: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'ctl00_ctl00_InnerContent_MainContent_UpdatePanel4'. If it is being updated dynamically then it must be inside another UpdatePanel.
Putting an UpdatePanel around the whole asp.net panel resolved the issue above, but I still get the Failed to load viewstate error when clicking on Cancel or Add, etc...
First off, you probably need to rebind the ListView after setting the EditIndex. (honestly, I haven't used ListView at all, but this is how the other repeater controls work) What does "EditAlbum()" do?
Your code is a little odd... why do you have the same controls in your EditItemTemplate as in the ItemTemplate? Ie, the Edit button should only be in the ItemTemplate... Then EditItemTemplate should have a Save or Cancel button.
Bottom line... your control tree is different on LoadViewState than it is when SaveViewState was called. One thing you can do is override these methods and then put a breakpoint there to manually look at the Controls collection in the debugger. You will probably see that the controls inside the ListView are different. But try my first suggestion before you do this.
Question for you:
in your Page_Load you have
Albums = AlbumCollection.GetAlbums(Common.GetUserName(), ddlAlbumType.SelectedIndex);
but in lvwAlbums_RowEditing(..) and in btnCancel_Click(...) you have
AlbumCollection.GetAlbums(Common.GetUserName(), ddlAlbumType.SelectedIndex);
shouldn't these be (Albums = ...)
Albums = AlbumCollection.GetAlbums(Common.GetUserName(), ddlAlbumType.SelectedIndex);

Categories

Resources