Passing Values from asp.net to a c# method - Onclick - c#

I need to send a value to Link_Click (which is a method) by clicking on this link :
<asp:HyperLink ID="TheLink" runat="server" Text='<%# Eval("ID") %>' onclick="Link_Click"></asp:HyperLink>
the Value is: Eval("name")

onclick is a client-side javascript event handler, so you can do it this way:
onclick="<%= "Link_Click(" + Eval("name") + ")" %>
Otherwise, HyperLink navigates to another page, and you need to add the NavigateUrl property to a value like:
NavigateUrl="Some.aspx?name=<%# Eval("name") %>"
if that will work, but I think you need to do it as:
NavigateUrl="<%= "Some.aspx?name=" + Eval("name").ToString() %>"

Related

Add multiple parameters in URL asp.net

I am currently trying to send 2 parameters through an URL in ASP.NET. I have been able to successfully send across 1 parameter. I am now wanting to learn how to send 2 across.
I am wanting to also send another ID as well that links to Favourite_ID similar to what is happening below with Recipe_ID
ASP.NET
<asp:HyperLink Runat ="server" NavigateUrl ='<%#"RecipePage?id=" + DataBinder.Eval(Container.DataItem, "Recipe_ID").ToString()%>' ID="Hyperlink1"><%#DataBinder.Eval(Container.DataItem, "Recipe_Name")%></asp:HyperLink></asp>
<asp:HyperLink Runat ="server" NavigateUrl ='<%#"RecipePage?id=" + DataBinder.Eval(Container.DataItem, "Recipe_ID").ToString()+"&YourId="+DataBinder.Eval(Container.DataItem, "YourId")%>' ID="Hyperlink1"><%#DataBinder.Eval(Container.DataItem, "Recipe_Name")%></asp:HyperLink></asp>
You can place you id name in place of YourId and can add multiple ids like above
<asp:hyperlink
runat="server"
id="hlReceipt"
navigateurl='<%# String.Format("RecipePage.aspx?id={0}&name={1}", Eval("Receipt_ID"), Eval("Receipt_Name")) %>'
text='<%# Eval("Receipt_title") %>'>
</asp:hyperlink>

Pass parameter from Gridview column to Javascript

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 + "')");

Calling a java Script function from GridView with <% Eval("")%> as a function parameter?

I want to call a javascript function with a single parameter from a link click inside the GridView Control I need to provide the value of the parameter with <%Eval("myfield")%> how can i do this?
<ItemTemplate>
<a href="#" onclick='"javascript:return changeview(<%Eval("Id")%>)"'>
<asp:Label style="color:white;" ID="Label1" BackColor='<%# System.Drawing.Color.FromName(Eval("color").ToString())%>' runat="server" Text='<% #Bind("doctorname") %>'>
</asp:Label>
</a>
</ItemTemplate>
OnClientClick='<%# String.Format("javascript:return changeview(\"{0}\")", Eval("Id").ToString()) %>'
You can do like this.
View Content

hyperlink template field onclick pass value to pageB to display its details

I am new to this:
In Visual Studio 2010, asp.net
Webpage A has a gridview with a column of hyperlink of companyid:
<asp:TemplateField HeaderText="Company" ItemStyle-Width="20%" >
<ItemTemplate>
<asp:HyperLink Text='<%# (Eval("Company"))%>' ID="HyperLink1"
Target="_blank" runat="server"
NavigateUrl= WHAT SHOULD I PUT HERE TO NAVIGATE TO PAGE B WHICH IS ALSO IN THE SOLUTION
</ItemTemplate>
</asp:TemplateField>
I want to navigate to another page that's also in the solution file, but I don't know what address to use as it's not some links that's hosted already like "google.ca"
For the new webpage, I don't want any buttons or like that, I just want a page to show the details of a company, using "select * from table where companyid= 'value_from_pageA_hyperlink'. How can I build the page so that it's url could be something like www.somepage/key=?" Or can I set up a global value so that I can pass the companyid in the hyperlink to the other page?
I have been crazed by those.
Use
<asp:HyperLink Text='<%# (Eval("Company"))%>' ID="HyperLink1" Target="_blank" runat="server"
NavigateUrl='~/PageB.aspx?companyId=<%# Eval("CompanyID")%>'/>
"~/" in an ASP.NET URL means that the address is relative to the current application.
You Can Send A Query String And Get it Through request.params
<asp:HyperLink Text='<%# (Eval("Company"))%>' ID="HyperLink1"
Target="_blank" runat="server"
NavigateUrl="page2.aspx?variablename=value"/>
and get it through
request.params["variablename"]
on another page
I think it Should work
http://msdn.microsoft.com/en-us/library/6c3yckfw(v=vs.100).aspx
<asp:TemplateField HeaderText="Company" ItemStyle-Width="20%" >
<ItemTemplate>
<asp:HyperLink Text='<%# (Eval("Company"))%>' ID="HyperLink1" Target="_blank" runat="server"
NavigateUrl='<%# GetCompanyUrl(Eval("Company"))%>'/>
</ItemTemplate>
</asp:TemplateField>
protected string GetCompanyUrl (object companyNum)
{
return "./NewPageName.aspx?companyId=" + companyNum.ToString();
}

want to set image url in listview from a function return in asp.net

<asp:ImageButton ID="Imagebtn" runat="server" ImageUrl=retriveurl('<%# Eval("id") %>') CommandName="viewalbum" CommandArgument='<%# Eval("id") %>' />
<asp:Label ID="product_nameLabel" runat="server"
Text='<%# Eval("product_name") %>' />
i want to set image url from another database table using Eval("id").query can return several row.but i want to set the last url from datareader.
If you want to bind the last url from dataTable you can customize your datasource by calling a separate method which would return you the required url and then the updated datasouce can be binded.

Categories