I'm not very familiar with ASPX, not enough to get my head around this.
Our developers have used this code in a gridview control which opens a new tab and sends the user to a "details" page.
<asp:LinkButton Text='<%# Bind("OrderId") %>' CommandArgument='<%# Bind("FormId") %>' ID="lnkOrderId" runat="server" OnClick="lnkOrderId_Click"></asp:LinkButton>
This code produces HTML that looks like this:
<a id="gvGridView_lnkOrderId_2" href="javascript:__doPostBack('gvGridView$ctl04$lnkOrderId','')">20109</a>
To be able to use a Jquery fancybox control, we need the code to produce the code like this:
<a id="gvGridView_lnkOrderId_2" href=”http://domain.com/Details.aspx?OrderId=20109&FormId=0”>20109</a>
I can see we need to use asp:hyperlink, but what I tried with the variable doesn't work.
Is this possible to do?
I use a TemplateFieldand direct render the link, here is how:
On the GridView aspx page I use:
<asp:TemplateField >
<ItemTemplate >
<%#LinkToGoto(Container.DataItem)%>
</ItemTemplate>
</asp:TemplateField>
and on code behind I make the link as:
protected string LinkToGoto(object oItem)
{
// read the data from database
var cOrderId = (int)DataBinder.Eval(oItem, "OrderId");
// format and render back the link
return String.Format("go to order", OrderId);
}
Related
On client side button click event, I want to get control id that are place in Item template of Grid View. I tried this code but it doesn't work. Thanks
function buttonClicked(sender, args) {
var gv = $find('<%= GridView1.ClientID %>');
var textbox = $GridView1.findControl(gv.get_element().parentNode, "Textbox");
}
Here is the Gridview
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1"runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="upTest" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="KurzDS" DataKeyNames="Id" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="Textbox" runat="server" Text="Textbox"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="Textbox1" runat="server" Text="Textbox1"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn" Text='btn' CommandArgument='<%# Eval("Id")%>' CommandName="btn" runat="server" CausesValidation="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
Thanks for including the GridView example. Now that I can see what you are attempting, I have a much better answer for you.
First, make a slight change to the button template, change out CommandArgument for OnClientClickand since you are using this button client side instead of posting back to the server, you can simplify it like this:
<asp:Button ID="btn" Text='btn' OnClientClick='<%# Eval("ID", "YourJavascriptFunction({0} - 1); return false;") %>' runat="server" CausesValidation="false" />
I have the click event call your JavaScript function and it sends in a parameter of the server side resolved id. Notice I subtract 1 first though. This is because the server side ASP.Net Eval function give the ID starting at 1. But, each of the ids that get generated for your text input elements start with a zero base.
Now look at the JavaScript function below.
// Clicking the first button sends in a 0, second sends in a 1, etc.
function YourJavascriptFunction(id) {
// each of the TextBox1 elements has an ASP.Net server side
// generated id that ends with GridView2_Textbox1_0,
// GridView2_Textbox1_1, etc.
let selectId = "input[id$='GridView2_Textbox1_" + id + "']";
// The text we use in the querySelector function states
// find the DOM element with a tag of "input" where the id
// ends with . . . The $ in id$= is the part that says the
// value must "end with"
let textBox1 = document.querySelector(selectId);
// Now that we have TextBox1 from the same row as the button,
// getting the value is easy.
alert(textBox1.value);
}
I left off a jQuery example as this querySelector command works in almost every browser including IE8 and above, so you shouldn't need jQuery for something this simple.
Let me know if I can help further.
I need to pass an argument (which comes from my database) from a radgrid view column to my javascript (which opens a dialog box window). However, I can't put the "bind("Id")" as a parameter from where I call the javascript as href.
In simpler words, I am looking for a way to pass <% Bind("Id")%> to the javascript, OpenMyWindow, call instead of the hardcoded, 111, right now.
<telerik:GridTemplateColumn UniqueName="Meet" DataField="Subject" HeaderText="Meet">
<ItemTemplate>
<div style="text-align: center">
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Bind("Subject") %>' href="javascript: OpenMyWindow(111);" Width="30%">
</asp:LinkButton>
</div>
</ItemTemplate>
</telerik:GridTemplateColumn>
When I try "OnClick" instead of "href", my popup dialog box closes instantly and doesn't stay opened.
Try using "OnClientClick" and return false from your javascript method to prevent post back.
Alternatively. You could use a method passing in the DataIten. Then output an anchor tag created anyway you like:
<%# formatOpener(Container.DataItem) %>
With code behind of:
protected string formatOpener(object item)
{
ObjectType myObj = (ObjectType)item;
return String.Format("<a href=\"javascript:OpenMyWindow({0});\" width=\"30%\"/>{1}</a>", myObj.ID, myObj.subject);
}
I think you don't need a LinkButton, you could achieve it with an asp:HyperLink (which renders as a a tag):
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("Subject") %>' NavigateUrl='<%# "javascript: OpenMyWindow(" + Eval("ID").ToString() + ");" %>'></asp:HyperLink>
Also, don't use Bind if you don't need it, for displaying purposes always use Eval.
User HyperLink control instead and try setting the NavigateUrl property of the HyperLink this way:
<asp:HyperLink ID="hlLink" runat="server" Text='<%# Bind("Subject") %>' NavigateUrl='<%#Eval("Id", "javascript: OpenMyWindow({0});")%>'>
hope it helps./.
I finally figured out and solved this issue. Actually, somebody told me on the other post that, CommandArgument is completely a server-side property and doesn't render any html attribute. So I can't change any button's attribute and fire click on it. I finally made "Id" come through the code behind and made it work.
aspx code
<telerik:GridButtonColumn UniqueName="Subject" DataTextField="Subject" HeaderText="Meeting">
<HeaderStyle Width="30%" />
<ItemStyle HorizontalAlign="Center" />
</telerik:GridButtonColumn>
Code Behind
var subjectLink = meetingRow["Subject"].Controls[0] as LinkButton;
subjectLink.Attributes.Add("onClick", "javascript: return OpenMyWindow('" + meetingId + "')");
I'm new to ASP.NET and C# and also new to this forum but here is my problem.
I'm trying to create a image gallery with ASP.NET Webforms(requirement) and C#. I'm using a Listview to show the thumbnails and when you click on the small picture the big version should show above in a img tag for example. But I cant find any "OnClick" code for a hyperlink. Do I have to do that with JavaScript?
Thanks for all help.
here is some of the code:
<ItemTemplate>
<asp:HyperLink ID="ImageHyperLink" runat="server" ImageUrl='<%# Eval("Name","~/files/thumbs/{0}") %>' NavigateUrl='<%# Eval("Name","~/files/{0}") %>'></asp:HyperLink>
</ItemTemplate>
Try taking a look at LinkButton
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton(v=vs.110).aspx
<asp:LinkButton runat="server" id="LinkButton1" OnClick="OnClickAction"></asp:LinkButton>
This will allow you to do a server-side method in much the same way an <asp:Button> will do. However will look just like a hyperlink.
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="http://stackoverflow.com/">HyperLink</asp:HyperLink>
You will not get on click event for HyperLink, But for navigating on to the another page.
You will get one property which is NavigateUrl
By using this property you can redirect to any page by giving url.
Can anyone tell me how to get this to work please? It is a LinkButton in a row in a Gridview (in an ItemTemplate)
<asp:LinkButton ID="lbOrder" runat="server" PostBackUrl='EditOrder.aspx?OrderID=<%# Eval("OrderID") %>' Text='<%# Eval("OrderID") %>'></asp:LinkButton>
It appears okay on screen. When I click it - the OrderID is not being passed.
On the EditOrder page the querystring looks like:
http://mysite/Orders/OrderID.aspx?OrderID=<%# Eval(\"OrderID\")%>
I have tried plenty of permutations of single inverted commas and double inverted commas - but I can't get the OrderID to appear in the queryString correctly.
Is a post-back really necessary? I suggest you to use asp:HyperLink instead.
<asp:LinkButton ID="lbOrder" runat="server" NavigateUrl='<%# String.Format("~/EditOrder.aspx?OrderID={0}", Eval("OrderID"))%>' Text='<%# Eval("OrderID") %>'></asp:LinkButton>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl=""
onclick="javascript:w= window.open(
<%# Eval("booking_id","hideFromStartborrow.aspx?booking_id={0}")%>,
'mywin','left=20,top=20,width=500,height=500,toolbar=0,resizable=0');">
new Window</asp:HyperLink>
I want to send it to another one, but it does not.And want to make the page smaller.
You can try this Hyperlink
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# String.Format("hideFromStartborrow.aspx?booking_id={0}", Eval("booking_id")) %>' onclick="javascript:w= window.open(this.href,'DownloadImage','left=20,top=20,width=500,height=500,toolbar=0,resizable=0');return false;">Open</asp:HyperLink>
OnClientClick will be executed in the client side and only if it returns true,it will be posted back to server..If its false then nothing will happen.. Inorder to deal directly with server side use onClick..Here you are using both javascript and server side code so i would recommend onclientclick..