How to send a value from a LinkButton to the server? - c#

I have many LinkButton such as:
<asp:LinkButton runat="server" onclick="cmdCancellaComunicazione_Click">X</asp:LinkButton>
they call the same server method, cmdCancellaComunicazione_Click. but I need to distinguish them (passing to the server, for example, a value).
How can I do it? I know there is CommandArgument, but I can't set to it a value such as <%= myValue %>

You can use the sender argument of the event-handler. Cast it to LinkButton:
protected void cmdCancellaComunicazione_Click(Object sender, EventArgs e)
{
LinkButton lbtn = (LinkButton) sender;
}
Then you can use it's CommandName, ID or Text to distinguish.

Your code has no ID specified on your LinkButton which seems odd.
You should be able to assign the CommandArgument server side with:
yourLinkButton.CommandArgument = yourValue;
And then it will be read it server side in your OnClick handler.
protected void cmdCancellaComunicazione_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
if (btn.CommandArgument.Equals(something here))
{
// do something
}
else
{
// do something else
}
}
Is this being created in a grid or something that is being bound? If so I would implement the OnDataBinding event for the LinkButton like:
<asp:LinkButton ID="yourLinkButton"
runat="server" OnDataBinding="yourLinkButton_DataBinding"
onclick="cmdCancellaComunicazione_Click">X</asp:LinkButton>
Server side code (I try to avoid inline code whenever possible):
protected void protected void lblID_DataBinding(object sender, System.EventArgs e)
{
LinkButton btn = (LinkButton)sender;
btn.CommandArgument = yourValue;
}
Is there something more to your scenario that you have not included in your question?

In your method, you can cast the sender to a LinkButton and inspect the value there.

OnCommand would be a good option here.
protected void ButtonCommand(object sender, CommandEventArgs e)
{
string theCommand = e.CommandArgument.ToString();
}
Just add the OnCommand and CommandArgument to the LinkButton.
<asp:LinkButton id="LinkButton1" Text="The Text" OnCommand="ButtonCommand" CommandArgument="YourInfo" runat="server"></asp:LinkButton>

Related

How to pass an attribute value from ImageButton to code behind

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

How do capture row selected in gridview and pass username value to text box in the next page?

I would like to select the gridview and redirect to the next page where username in gridview passed to the text box in the next page when the row is being clicked. How do i do that?
My code behind (on the first page):
protected void GridView1_OnRowSelected(object sender, GridViewSelectEventArgs e)
{
var username = Convert.ToString(GridView1.DataKeys[e.NewSelectedIndex].Value);
Response.Redirect("ViewUploads.aspx?USERNAME=" +username);
}
My code on the second page:
protected void TextBoxUsername_TextChanged(object sender, EventArgs e)
{
var username = Request.QueryString["USERNAME"];
}
I assume that you're looking for the SelectedIndexChanged event of the GridView.
protected void GridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
var username = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
Response.Redirect("ViewUploads.aspx?USERNAME=" +username);
}
In the next page(ViewUploads.aspx) you need to assign it to the TextBox' Text:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack && Request.QueryString["USERNAME"] != null)
this.txtUserName.Text = Request.QueryString["USERNAME"];
}
I'm not sure if you have already done that, if you want to use the DataKeys-collection in the first page, you have to set the DataKeyNames property accordingly. For example:
<asp:gridview id="GridView1"
datakeynames="UserName"
onselectedindexchanged="GridView_SelectedIndexChanging"
runat="server">
...
</asp:gridview>

The RowDataBound events of the gridview is not fired after turning to the next page

I use a gridview to display the search result. After clicking search button, the gridview will show page 1, but when I click page 2 link, the gridview disappeared and it was back when I click search button again and show page 2's content.
here is my code
<asp:GridView ID="searchresult" runat="server" AutoGenerateColumns="true" AllowPaging="true" OnRowDataBound="searchresult_RowDataBound" OnPageIndexChanging="searchresult_PageIndexChanging"
HeaderStyle-BackColor="#f9e4d0"
HeaderStyle-Height="20px"
Font-Size="11px"
AlternatingRowStyle-BackColor="#cfdfef"
Width="800px" style="text-align:left">
</asp:GridView>
and code behind
protected void search_Click(object sender, EventArgs e)
{
List<someclass> totalResult = new List<someclass>();
..... //some code to generate the datasource
searchresult.DataSource = totalResult;
searchresult.AllowPaging = true;
searchresult.DataBind();
}
protected void searchresult_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void searchresult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
searchresult.PageIndex = e.NewPageIndex;
DataBind();
}
I have no idea why the page 2 won't show up until I click search button again. when I clicked the page 2 link, the page did postback but the RowDataBound event was not fired
You have to give your grid a datasource. It appears you are only doing this on search_Click, so your grid is only going to have data then. Try something like:
protected void search_Click(object sender, EventArgs e)
{
PopGrid();
}
protected void searchresult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
searchresult.PageIndex = e.NewPageIndex;
PopGrid();
}
protected void PopGrid()
{
List<someclass> totalResult = new List<someclass>();
..... //some code to generate the datasource
searchresult.DataSource = totalResult;
searchresult.AllowPaging = true;
searchresult.DataBind();
}
searchresult_PageIndexChanging event handler will make that functionality work. However I recommend you use a gridview control inside a Panel with vertical scroll bar. My user love it and it is a lot faster moving up and down the gridview without defining any page index changing.
I hope it work for you.

Command Argument not working link button

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
// ...
}

OnClick missing in LinkButton controls?

I'm trying to get the ProductID from the Linkbutton I click on, but I cant find an OnClick choice, just the OnClientClick, but it dont works, are there another way to do it?
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = CategoryAccess.GetAllProducts();
for (int i = 0; i < table.Rows.Count; i++)
{
LinkButton lbDelete = new LinkButton();
lbDelete.Text = table.Rows[i]["ProductName"].ToString();
lbDelete.ID = table.Rows[i]["ProductID"].ToString();
lbDelete.CommandArgument = table.Rows[i]["ProductID"].ToString();
lbDelete.OnClientClick = "btnDelete_Click";
phDelete.Controls.Add(lbDelete);
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
LinkButton _sender = (LinkButton)sender;
string TrixID = _sender.CommandArgument;
DeleteProduct(ProductID);
}
Hmm, I think I understand what you are doing, but going about it in a funny way.
What I usually do i scode the rowcommand event. I add commandargs='<%# Eval("Id") %>' (or something like that) and pass it to the rowcommand event where I then grab e.commandargs and use that value to delete the database row.
here is one of many examples of what I' talking about
http://www.codeproject.com/KB/webforms/GridViewConfirmDelete.aspx
Sorry if this isn't helpful.

Categories

Resources