I have a repeater of LinkButtons and on the ItemCommand event. I need to get the Data Object that created the link button.
My DataSource is List so on ItemCommand I need MyObject object = ???
Are you looking for this? This will send the ID to the code behind using CommandArgument so it can be processed.
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("ID") %>' runat="server" CommandName="myCommand">LinkButton</asp:LinkButton>
And in code behind:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "myCommand")
{
string myID = e.CommandArgument.ToString();
}
}
Or you can use CommandArgument='<%# Container.ItemIndex %>'. Then you know the row number and can access the corresponding index in your source.
Object DataItem = (Object) e.Item.DataItem;
And then use the DataItem.
Related
I have a repeater that in it has one dropdown list and one linkbutton.
I want to get the selected value of the dropdown list by CommandArgument in linkbutton, but it just knows default value of dropdown list.
My code :
<asp:Repeater runat="server" ID="Repeater1" OnItemDataBound="Page_Load2"OnItemCommand="list_ItemCommand" >
<ItemTemplate>
<asp:DropDownList ID="dlPricelist" CssClass="width100darsad dropdownlist" runat="server" AutoPostBack="true" ViewStateMode="Enabled" >
</asp:DropDownList>
<asp:LinkButton ID="btnAddToCart" runat="server" class="btn btn-success btnAddtoCardSinglepage" CommandArgument='<%#Eval("id") %>' CommandName="addtocard">اضافه به سبد خرید</asp:LinkButton>
<asp:Label ID="xxxxxx" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:Repeater>
Code behind:
protected void Page_Load2(object sender, RepeaterItemEventArgs e)
{
if (!IsPostBack)
{
string id = Request.QueryString["id"].ToString();
DataSet dsselectcategory = BLLTour.left3join(id.Trim().ToString());
var dlPricelist = (DropDownList)e.Item.FindControl("dlPricelist");
dlPricelist.DataSource = dsselectcategory.Tables[0];
dlPricelist.DataTextField = "pricelistPrice";
dlPricelist.DataValueField = "priceid";
dlPricelist.DataBind();
}
}
protected void list_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "addtocard")
{
foreach (RepeaterItem dataItem in Repeater1.Items)
{
Label xxxxxx = (Label)e.Item.FindControl("xxxxxx");
LinkButton btnAddToCart = (LinkButton)e.Item.FindControl("btnAddToCart");
xxxxxx.Text = ((DropDownList)dataItem.FindControl("dlPricelist")).SelectedItem.Text; //No error
}
}
}
I don't know how I should fix it.
You are using very old technology to show data that's not appropriate.
But if you are serious to use it, you must use FindControll method in ItemTemplate of your repeater control. You should find dropdownlist first, and then cast it to a object to be able to use it's value.
Hi I would like to know how to get the text field and index from a hyperlink in a gridview that has been clicked. Basically, the user would click on the hyperlink in the gridview and when the user has been navigated to the link, the text field and index of the link would be stored into an arraylist. Does anyone have any idea how I can go about doing this?
I have came up with this "pseudo code" for the onrowdatabound event handler in gridview:
ArrayList linksClicked = new ArrayList();
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hl = (HyperLink)e.Row.FindControl("links");
if (hl != null)
{
linksClicked.Add(h1.ToString());
}
}
You should use ItemTemplate with LinkButton. In this button you can keep index or id like CommandArgument, also you easily catch event onClick and add index to your array. Use this sample.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="hyperLinkButton" Text="link" PostBackUrl="youruri.com" runat="server"
CommandArgument="<%# Eval("SomeFieldYouNeedArguementFrom") %>" OnClick="hyperLinkButton_Click" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void hyperLinkButton_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string yourValue = btn.CommandArgument;
// do what you need here
}
I have an hiddenfield field in my gridview but the code behind cant get its value maybe someone could find the problem.
HTML:
<asp:TemplateField HeaderText="TweetID" Visible="false">
<ItemTemplate>
<asp:HiddenField ID="TweetID" runat="server" Value='<%#Eval("TweetID") %>' />
</ItemTemplate>
</asp:TemplateField>
.cs:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
HiddenField tid = GridView1.Rows[index].FindControl("TweetID") as HiddenField;
//Response.Write(tid.Value);
TweetHelper.RemoveTweet( Convert.ToInt32(tid.Value), 1);
}
by the way the response writes nothing.
Based on your code above what you are doing is overkill.
Either make TweetID a Gridview.DataKey.
Or if that's not an option, convert your Delete button to a template field and add TweetID as a CommandArgument to the Delete button.
Your code should work fine.However another way to find the control is
GridViewRow row = GridView1.Rows[e.RowIndex];
HiddenField hdn = (HiddenField)row.FindControl("TweetID");
string value = hdn.Value;
or simply
var tweetid = ((HiddenField)GridView1.Rows[e.RowIndex].FindControl("TweetID")).Value;
It seems like this would be simple, but for the life of me, I can't figure out how it would work.
I have a gridview.
I have a standard button.
How do I use the button click to display the gridview?
Any suggestions?
This is a brief example from the MSDN page: (https://msdn.microsoft.com/en-us/library/bb907626.aspx)
You might to add an asp:TemplateField inside your GridView and through the CommandArgument property in the Button, set the current row index.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>
And in your code, in the RowCommand event:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "AddToCart") {
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here to add the item to the shopping cart.
}
}
Hope this help you.
HTML :
<asp:LinkButton ID="lnk_productImage" runat="server" Text="select"
OnClick="viewProductImage('<%#DataBinder.Eval(Container.DataItem,"Id") %>')"
>
</asp:LinkButton>
CodeBehind:
protected void viewProductImage(object sender, EventArgs e, int id)
{
//Load Product Image
}
I see you're using a repeater, so you probably could use this code:
In your repeater template:
<asp:Repeater ID="_postsRepeater" runat="server" OnItemCommand="_postsRepeater_ItemCommand">
<ItemTemplate><asp:LinkButton ID="_postDeleteLinkButton" runat="server" CommandName="DeletePost" CommandArgument="<%# ((Post)Container.DataItem).ID %>">Delete</asp:LinkButton></ItemTemplate>
</asp:Repeater>
Then handle the repeater's ItemCommand event:
protected void _postsRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "DeletePost") // Replace DeletePost with the name of your command
{
// Get the passed parameter from e.CommandArgument
// e.g. if passed an int use:
// int id = Convert.ToInt32(e.CommandArgument);
}
}
Use CommandArgument property of linkbutton to pass parameters.
CommandArgument property:
Gets or sets an optional argument passed to the Command event handler along with the associated command name property.
LinkButton Members