<asp:LinkButton class="uibutton normal submit" ID="LinkButtonMAR"
OnClick="MarkClientNoteRead(<%# Eval("NoteID") %>)" runat="server">
Mark as Read</asp:LinkButton>
This is all within a repeater that will return a list of notes. I want to grab the NoteID and throw this into the OnClick="MarkClientNoteRead()" method call.
Is this possible?
You could use the CommandArgument:
<asp:LinkButton class="uibutton normal submit"
ID="LinkButtonMAR"
CommandName="MarkRead"
CommandArgument='<%# Eval("NoteID") %>'
Text="Mark as Read"
OnCommand="MarkClientNoteRead" runat="server" />
protected void MarkClientNoteRead(Object sender, CommandEventArgs e)
{
int NoteID = int.Parse(e.CommandArgument.ToString());
}
Note that you need to handle the Command event instead of the Click event.
Another more convoluted way would be:
<asp:LinkButton class="uibutton normal submit" ID="LinkButtonMAR"
OnClick="LinkButtonMAR_Click" runat="server">Mark as Read
</asp:LinkButton>
<asp:HiddenField ID="NoteID" runat="server" Value='<%# Eval("NoteID")%>' />
Code Behind:
protected void LinkButtonMAR_Click(object sender, EventArgs e)
{
LinkButton thisLinkBUtton = (LinkButton)sender;
// substitute appropriate object for the GridViewRow below
GridViewRow thisGridViewRow = (GridViewRow)thisLinkBUtton.Parent.Parent;
HiddenField hdnNoteID = (HiddenField)thisGridViewRow.FindControl("NoteID");
// run your update code...
}
I've used this, but it bothers me having to use Parent.Parent. Plus, I was needing other bits of data from the same row.
Related
I'am trying to hide/show the edit/save button in a ASP.NET Template.
so i want the edit button too show when no row is selected, and then hide it on click and then make the save button visable instead.
How do I access and update the attribute?
The solution ive tried just gives me "Null"
what i have:
<ItemTemplate>
<asp:ImageButton ID="ImageButtonEdit" runat="server" CommandName="Edit" ImageUrl="loginstyling/images/Edit.png"/>
<asp:ImageButton ID="ImageButtonUpdate" runat="server" CommandName="Update" ImageUrl="loginstyling/images/Save.png" OnClick="ImageButtonUpdate_Click" Visible="true"/>
<asp:ImageButton ID="ImageButtonDelete" runat="server" CommandName="Delete" ImageUrl="loginstyling/images/Remove.png" visible="false" />
</ItemTemplate>
what ive tried behind:
protected void ImageButtonUpdate_Click(object sender, ImageClickEventArgs e)
{
ImageButton test = (ImageButton)GridView1.FindControl("ImageButtonUpdate");
test.Attributes.Add("Visible", "False");
}
Try if this works:
test.Visible = false;
Try using RowDataBoundEvent for GridView, if it works:
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton test = (ImageButton)e.Row.FindControl("ImageButtonUpdate");
test.Visible = false;
}
}
Are you sure that the .FindControl("ImageButtonUpdate");returns a valid object?
Since it returns null check this other post FindControl() return null , it seems to be the same problem you have encountered.
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;
i want to get image url on clicking the image from database in repeater
my database contains(id,url)
my repeater code is:
<asp:Repeater runat="server" ID="repeater" >
<ItemTemplate >
<asp:ImageButton runat="server" Width="200px" Height="200px" ImageUrl='<%#Eval("url") %>' OnCommand="Image_Click" CommandName="ImageClick" CommandArgument='<%#Eval("url") %>' />
</ItemTemplate>
</asp:Repeater>
my .cs code is
protected void Image_Click(object sender, CommandEventArgs e)
{
if (e.CommandName == "ImageClick")
{
string a=e.CommandArgument.tostring();
responce.write(a);
}
}
you can do in ImageClick
((ImageButton)sender).ImageUrl
to get the url of the clicked button
Cast sender to ImageButton and read its ImageUrl property.
Also, is there a reason you are using command instead of handling ImageButtons click event?
Both Vladimir and Guigui answers are prefer way of accessing URL of an image button.
If you also want ID value in addition to URL, you can store multiple values into CommandArgument separated by comma.
<asp:ImageButton runat="server"
ImageUrl='<%#Eval("url") %>'
OnCommand="Image_Command"
CommandName="ImageClick"
CommandArgument='<%# Eval("Id")%> + "," + Eval("url") %>' />
protected void Image_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "ImageClick")
{
string[] commandArgs = e.CommandArgument.ToString()
.Split(',');
string id = commandArgs[0];
string url = commandArgs[1];
}
}
I created LinkButton that located inside of Repeater Control.
CategoryID is a variable in LinkButton Control that have to get value after Repeater Control was bound to data. But CategoryID always get zero.
I have the following ASP and C# code:
<asp:Repeater ID="rpt1" runat="server"
OnItemDataBound="rpt1_ItemDataBound"
OnItemCommand="rpt1_ItemCommand">
<ItemTemplate>
<div>
<%# Eval("Name") %>-<%# Eval("CollectionType")%>
<asp:LinkButton ID="LinkButton1" runat="server" Text="[edit item]"
PostBackUrl='AddItem.aspx?CategoryID=<%# Eval("CollectionID")%>' />
</div>
</ItemTemplate>
</asp:Repeater>
Code behind:
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<GlassesCollection> gc = BL.GetDataBL.GetCollection();
rpt1.DataSource = gc;
rpt1.DataBind();
}
}
Any idea why CategoryID variable doesn't get any value and how can I fix the problem?
A server control parameter cannot contain a mixture of literal text and evaluated expressions.
The code you have will literally be posting back to AddItem.aspx?CategoryID=<%# Eval("CollectionID")%> and it will not be evaluating the code within the angle brackets.
You need to change your parameter like so
PostBackUrl='<%# "AddItem.aspx?CategoryID=" + Eval("CollectionID")%>' />
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