Get the this object using eval in a gridview in asp - asp.net

I can't find any answer anywhere. I want to refer to the row object itself in an databinding expression in a gridview, like this:
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label runat="server"
Text = '<%# GetPendingReason(Eval("this")) %>' />
</ItemTemplate>
</asp:TemplateField>
But it doesn't work because "this" doesn't refer to any attribute. Referencing individual attributes works fine, but how do you refer to the current row?

Simply use <%# Container.DataItem %>. Do not use Databinder.

If you want to refer to the current row you do that at codebehind using
GridViewRow row = GridView1.Rows[index];
at any of the GridView event.

Related

Gridview Hyperlinkfield using DataNavigateUrlFormatString with Ampersand

So I have gridview pulling fields from a table and my hyperlinkfield is used to go to a specific page for that row to get more detailed data. Everything seems to work great except when the field used in the hyperlinkfield has an ampersand. I assume it is reading the ampersand as something else and so it doesn't bring up the proper info because the ampersand is in the name in the database.
Hyperlinkfieldcode:
<asp:HyperLinkField HeaderText="Name" Text="{0}" DataNavigateUrlFields="Name" DataNavigateUrlFormatString="item.aspx?name={0}" DataTextField="Name" />
Example:
A clicking on the name "test item" would take you to mysite.com/item.aspx?name=test%20item and this works.
However, clicking on "test & test item" it takes you to mysite.com/item.aspx?name=test%20item%20&%20item which does not work. It just pulls up a page with blank info.
What can I do to fix this?
Update:
I converted the hyperlinkfield to a hyperlink inside a template field, but the url is now coming out weird.the url now comes out like mysite.com/item.aspx?name=System.Int32%5b%5d
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%#Eval("Name") %>' DataNavigateUrlFields="Name" NavigateUrl='<%# "name.aspx?name=" + HttpUtility.UrlEncode({0}.ToString())%>' DataTextField="Name" />
</ItemTemplate>
</asp:TemplateField>
My original answer was incomplete (due to not being able to see the entire code). This answer will contain the missing elements.
The main object is to UrlEncode the data field Name, so that it can be used as (part of) a url link .
1 - First we must ensure that the field "Name", is listed as DataKeyNames for the GridView as follows:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Name" ...
2 - Secondly (if using a navigateURL) create a TemplateField i.e. (asp:TemplateField ) and use Eval() method to access the DataField in conjunction with UrlEncode() .
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink ID="NameLink" runat="server" Text='<%# Eval("Name") %>' NavigateUrl='<%# "name.aspx?name=" + HttpUtility.UrlEncode(Eval("Name").ToString())%>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Option 2 Alternatively you can use a HyperLinkField and DataNavigateURL but you should still designate Name as a DataKeyNames .
Hope this works with no problems. Let me know if any clarification is needed.
Happy coding and Cheers,

Passing more than one command Arguments in gridview in asp.net

I have a TemplateField column in a gridview with a image button inside of it.
i need to use two command arguments within single image button.
here is my code
<asp:TemplateField ItemStyle-Width="30px">
<ItemTemplate>
<asp:ImageButton ID="btnAvaililabitly" runat="server"
ImageUrl="~/CMS/images/available_icon.png"
Width="12px" Height="12px" CommandName="availability"
ToolTip="Rooms Availability"
CommandArgument='<%#Eval("HotelID")%>'/>
</ItemTemplate>
</asp:TemplateField>
please help
You can send comma separated string and collect value on server side
CommandArgument= '<%# String.Format("{0} , {1}", Eval("Name1"), Eval("Name2")) %>'

hide a header of a column with binding data in asp.net

would it be possible to do it like this?
<asp:TemplateField HeaderText="Export" Visible='<%# Bind("isExported") %>'>
</asp:TemplateField>
I tried it and I got this problem:
The TemplateField control with a two-way databinding to field deviceExport must have an ID
You should use <%# Eval("isExported")%> instead of <%# Bind("isExported")%>

asp.net repeater with usercontrol - setting commandargument

I have the following repeater code:
<asp:Repeater ID="repMain" runat="server" OnItemCommand="repMain_ItemCommand" EnableViewState="false">
<ItemTemplate>
<dmg:testcontrol runat="server" MyData=<%#Container.DataItem %>>
</dmg:testcontrol>
</ItemTemplate>
</asp:Repeater>
The testcontrol usercontrol looks like:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="TestRepeater.TestControl" %>
<asp:Literal runat="server" ID="litMain" Text="<%#MyData.MyValue %>"></asp:Literal>
<asp:DropDownList runat="server" ID="dropdownMain"></asp:DropDownList>
<asp:Button runat="server" ID="btnMain" Text="Click Me" CommandName="Update" CommandArgument="<%#dropdownMain.SelectedValue%>"/>
Is it possible for me to send through the dropdownMain.SelectedValue as the CommandArgument?
Just now it is an empty string.
Thanks
Duncan
PS This is related to ASP.NET Repeater not binding after ItemCommand but I thought the two sufficiently different to keep apart.
A bit old question but I just my self found the answer to this one.
CommandArgument="<%#dropdownMain.SelectedValue%>"
Needs to look like this instead with single quotes! All inline codes within a asp.net controls have to be done this way instead.
CommandArgument='<%#dropdownMain.SelectedValue%>'
Why not get the selected value, and use it inside the Command function ?
(why to try to send it as argument, from the moment you can get it inside the command called function and its the same)

NavigateUrl with NavigateUrlFormat populated from AppSettings (dynamically)

I have a gridview with a hyperlink inside an TemplateField. The NavigateUrlFormat will be populated from the Web.Config file, it has the following format:
mysite.com/{0}
Where {0} will be populated from the gridview datasource, however, my current code doesn't work:
<asp:TemplateField HeaderText="WorkOrder #">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("WorkOrderKey", "<%$ AppSettings:DispatchLink %>") %>'
Text='<%# Eval("WorkOrderKey") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
The problem is right after "AppSettings:DispatchLink", it closes the tag when it see "%>". Is there a way around this problem?
Thank you,
Kenny.
You cannot nest <%# and other tags. Use ConfigurationManager.AppSettings["DispatchLink"] (pls. lookup correkt Class/Property in MSDN) instead.
Or: If your are using that HyperLink Control more than once I would suggest that you create an own MYHyperLink derived from System.Web.UI.WebControls.HyperLink and set the Property NavigateUrlFormatString in your Constructor.
Something like this:
public class DispatchLink : System.Web.UI.WebControls.HyperLink
{
public DispatchLink()
{
this. NavigateUrlFormatString = ConfigurationManager.AppSettings["DispatchLink"]
}
}

Resources