In my code a user can like a post when they click on the following code:
<li><%# Eval("Likes") %></li>
This will run the function LikePost in the codebehind:
public void LikePost(object sender, EventArgs e)
{
//like post whit given id using a database query
}
but how can I give that function a parameter, because it needs the postid from the post that the user is going to like.
Instead of an HTML link, use an asp:LinkButton which has a CommandArgument property. Something like this:
<asp:LinkButton
ID="LinkButton1"
Text='<%#Eval("Likes")%>'
CommandArgument='<%#Eval("ID")%>'
OnCommand="LikePost"
CssClass="icon fa-heart"
runat="server"/>
Then in your code-behind the signature takes a CommandEventArgs:
public void LikePost(Object sender, CommandEventArgs e)
{
// e.CommandArgument should contain the desired value
}
Related
In my javascript file, I got an ajax to get all list and iterate these data and append <a id='userID' class='btn'>Assign ID<> to my list.
So, how do a add postback to these anchor and redirect it inside my method in the server. Below is my code but didn't work. When I click the achor button, it just redirect/refresh to the same page without doing any changes and didn't show the text.
<a id='uniqueID' class='btn assignID' href='javascript:void(0);' onclick='javascript:__doPostBack('uniqueID','')'>Assign ID</a>
protected void Action_assignID(object sender, EventArgs e)
{
// assign ID action
Response.Write("Pass");
}
You should be changed your button to:
<a id='uniqueID' class='btn assignID' href='javascript:void(0);' onclick="javascript:__doPostBack('uniqueID','Assign ID')">Assign ID</a>
And it's a good idea to implement the IPostBackEventHandler interface in your codebehind as below:
public partial class WebForm : Page, IPostBackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
}
}
public void RaisePostBackEvent(string eventArgument)
{
// do somethings at here
}
}
Hope this help!
The __doPostBack method really doesn't do anything special except, well... perform a POST operation back to the same page with two specific form arguments.
The first parameter is the __EVENTTARGET and the second parameter is the __EVENTARGUMENT.
The magic all happens in ASP.Net where it automagically wires up your controls to event handlers, but since you are creating these entirely in JavaScript the server doesn't know that those controls exist.
However, you can manually grab these values and do something with them.
//Client Side JavaScript:
__doPostBack('my-event', '42');
//Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
var target = Request.Params["__EVENTTARGET"];
var args = Request.Params["__EVENTARGUMENT"];
Target.Text = target; // 'my-event'
Argument.Text = args; // '42'
}
}
On page load I add a new attribute (UploadStatus) to an ImageButton (FileUpload_result) from code behind. Now on button click I want to retrieve the value of the added attribute. How can I do that?
public string UploadStatus = "testing";
protected void Page_Load(object sender, EventArgs e)
{ FileUpload_result.Attributes.Add("UploadStatus", UploadStatus); }
<asp:ImageButton runat="server" ID="FileUpload_result" OnClick="FileUpload_Click" ImageUrl="icon-ok.png" UploadStatus="testing" />
protected void FileUpload_Click(object sender, EventArgs e)
{ // How can I get the value of the added attribute UploadStatus? The value is testing in this case}
In your FileUpload_Click method you can access the attribute like this.
((ImageButton)sender).Attributes["UploadStatus"]
Something like FileUpload_result.Attributes[""UploadStatus"] might get it. https://msdn.microsoft.com/en-us/library/kkeesb2c%28v=vs.140%29.aspx
I have a button, which OnClick stores some text to tables in a database.
All this in asp.net using c# and .NET Framework 4.0.
However, i need that, when click, the button executes the OnClick method, thing is doing right now, but ALSO, navigates to another page, to continue registration.
The registration form is about 9 pages, i know if i pass postbackurl="url" it navigates to another page/form, but then it doesn't executes the OnClick method.
<div style="text-align:right; margin-top:20px"><asp:Button ID="Button1" runat="server" ValidationGroup="Curriculum" postbackurl="Envia2.aspx" OnClick="Button1_Click" CssClass="my_btn" /></div>
So, how can i 'attach' this attribute to the OnClick method?
Or there is another walkaround to this situation?
Thanks in advance!
Button click event cannot perform both at the same time at client side - submit to server and redirect to another page.
Ideally, you do not want to insert each registration step into database. instead, you want to store them in session state. Then insert them into database at the final page.
Here is the example. Make sure you check validation on each step -
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Other { get; set; }
}
// Page1.aspx
<asp:Button runat="server" ID="SubmitButton" Click="SubmitButton_Click"
Text="Continue to Step 2" />
// Page1.aspx.cs
protected void SubmitButton_Click(object sender, EventArgs e)
{
var customer = new Customer
{
FirstName = FirstNameTexBox.Text,
LastName = LastNameTexBox.Text
};
Session["Customer"] = customer;
// Redirect to page 2
Response.Redirect("Page2.aspx");
}
// Page2.aspx
<asp:Button runat="server" ID="SubmitButton" Click="SubmitButton_Click"
Text="Continue to Step 3" />
// Page2.aspx.cs
protected void SubmitButton_Click(object sender, EventArgs e)
{
var customer = Session["Customer"] as Customer;
customer.Address = AddressTextBox.Text;
Session["Customer"] = customer;
// Redirect to page 3
Response.Redirect("Page3.aspx");
}
// Page9.aspx
<asp:Button runat="server" ID="SubmitButton" Click="SubmitButton_Click"
Text="Submit" />
// Page9.aspx.cs
protected void SubmitButton_Click(object sender, EventArgs e)
{
var customer = Session["Customer"] as Customer;
customer.Other = OtherTextBox.Text;
// Save customer to database.
}
If I understand your question, you need to do two things when the user clicks the button:
Call "Button1_Click"
Post to "Envia2.apsx"
The problem is that 1. is itself consists of a form post to the current page (as you may know, ASP.Net server controls, in order to handle events on the server side, trigger a postback behind the scenes). So I could see a few options:
Use OnClick and remove postbackurl -- inside the click handler, redirect to "Envia2.aspx"
Remove both OnClick and postbackurl. Instead, make an Ajax call of some kind to invoke the "Button1_Click" code, and then when complete, call form.post() on the form that "Button1" belongs to.
I have a link button and a set of records. When the link button of a specific record is clicked, i want the id of that record to be passed to the code behind. This is the code that i have used:
<asp:LinkButton ID="Likes" runat="server" OnCommand="LinkButton1_Click"
CommandArgument='<%#Eval("datarow["ID"]") %>' CommandName="Like">
Click</asp:LinkButton>
and in the cs file i have used:
protected void LinkButton1_Click(object sender, CommandEventArgs e)
{
int x = Int32.Parse(e.CommandArgument.ToString());
}
But the command argument is null here. can you please help me?
You would handle the wrong event. I think your LinkButton is inside a another control as #greg84 said, for example, you could handle event ItemDataBound of DataGrid or Repeater .
protected void Control_ItemDataBound(object sender, ControlItemEventArgs e)
{
LinkButton Likes = (LinkButton)e.Item.FindContro("Likes");
// Write code here to handle for like click
// ...
}
Here is a code sample:
myMaster.Master
<asp:LinkButton runat="server" OnClick="anAction_Click">
calls:
myMaster.Master.cs
protected void anAction_Click(object sender, EventArgs e)
{
???
Request.getUrlOfThePageCalling?
???
}
I'm using a master page. How do I get the page that called this action?
You were very close - try Request.Url like this:
this.Request.Url