I am rather new at asp.net.
I´m trying to use ImageButton to link to another page. ImageButton is inside a Repeater and the code is following:
<ul id = "ulMap">
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<li>
<asp:ImageButton ID="ImageButton1"
CommandArgument='<%#Eval("Nav_ID") %>'
runat="server"
ImageUrl="~/Icons/Ny_mappa.png"
onclick="ImageButton1_Click" />
<br />
<asp:LinkButton ID="lnkButton"
CommandArgument='<%#Eval("Nav_ID") %>'
runat="server"
onclick="LinkButton3_Click"
Text='<%#Eval("Nav_Name") %>'></asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
And the code behind is the following:
protected void LinkButton3_Click(object sender, EventArgs e)
{
Guid guid = new Guid(((LinkButton)sender).CommandArgument);
var query = from n in dc.Nemanet_Navigations
where n.UserId == userGuid && n.Nav_pID == guid
orderby n.Nav_Name ascending
select n;
Repeater1.DataSource = query;
Repeater1.DataBind();
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Guid guid = new Guid(((ImageButton)sender).CommandArgument);
var query = from n in dc.Nemanet_Navigations
where n.UserId == userGuid && n.Nav_pID == guid
orderby n.Nav_Name ascending
select n;
Repeater1.DataSource = query;
Repeater1.DataBind();
}
Well, for the LinkButton this works fine but not for ImageButton. Protected void ImageButton1_Click never happens. Can anybody help?
I thin you need to handle the Repeater_ItemCommand event to get this working properly. It'll probably save a bit of code as well.
MSDN Repeater ItemCommand
Also, if you're linking to another page do you need to postback to the page then perform some kind of redirection or would it make more sense to have that logic in the new page?
Instead of OnClick, you should you should OnCommand. Like this.
<ul id = "ulMap">
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<li>
<asp:ImageButton ID="ImageButton1"
CommandArgument='<%#Eval("Nav_ID") %>'
runat="server"
ImageUrl="~/Icons/Ny_mappa.png"
OnCommand="ImageButton1_Click" />
<br />
<asp:LinkButton ID="lnkButton"
CommandArgument='<%#Eval("Nav_ID") %>'
runat="server"
OnCommand="LinkButton3_Click"
Text='<%#Eval("Nav_Name") %>'></asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
And this (notice the CommandEventArgs instead of EventArgs and ImageClickEventArgs):
protected void LinkButton3_Click(object sender, CommandEventArgs e)
{
Guid guid = new Guid(e.CommandArgument);
var query = from n in dc.Nemanet_Navigations
where n.UserId == userGuid && n.Nav_pID == guid
orderby n.Nav_Name ascending
select n;
Repeater1.DataSource = query;
Repeater1.DataBind();
}
protected void ImageButton1_Click(object sender, CommandEventArgs e)
{
Guid guid = new Guid(e.CommandArgument);
var query = from n in dc.Nemanet_Navigations
where n.UserId == userGuid && n.Nav_pID == guid
orderby n.Nav_Name ascending
select n;
Repeater1.DataSource = query;
Repeater1.DataBind();
}
You can't handle server-side events for repeater child controls like this.
Think of it this way - if there is 100 buttons, you need 100 event handlers for each button. ImageButton1_Click will only work for the ImageButton with the id of 1.
The answer is to wire up the ItemCommand event for your repeater.
Like this:
<asp:repeater id = "Repeater`" runat = "server" onItemCommand = "SomeEvent">
Then you handle the "SomeEvent" for all buttons that trigger this event:
protected void SomeEvent ( Object src, RepeaterCommandEventArgs e ) {
var whoClickedMe = ((ImageButton) e.CommandSource );
}
Here's a good article on doing this.
Related
I am having trouble with a LinkButton firing the OnClick event where the LinkButton is in a nested repeater.
Here is my HTML Markup
<asp:Repeater ID="RepeaterGenres" runat="server" OnItemDataBound="LoadTitles">
<ItemTemplate>
<asp:HiddenField ID="hdnGenre" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "GenreID") %>' />
<%# DataBinder.Eval(Container.DataItem, "MovieGenre") %>
<asp:Repeater ID="RepeaterMovies" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "MovieName") %>
<asp:LinkButton ID="lbMovieInfo" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "MovieID") %>' OnClick="LoadMovieTitle">
Access Info
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Here is my method to load the Genres. (have removed some of the declarations to simplify).
string sqlString = "SELECT * FROM Movies_Genres";
sqlCmd = new SqlCommand(sqlString, myConnection);
sqlReader = sqlCmd.ExecuteReader();
if (sqlReader.HasRows)
{
RepeaterGenres.DataSource = sqlReader;
RepeaterGenres.DataBind();
}
Here is my method to load the Movie Titles within each Genre.
protected void LoadTitles(object sender, RepeaterItemEventArgs args)
{
if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
{
// Genre from HiddenField
HiddenField GenreID = (HiddenField)args.Item.FindControl("hdnGenre");
// Repeater
Repeater childRepeater = (Repeater)args.Item.FindControl("RepeaterMovies");
string sqlString = "SELECT * FROM Movies_Titles " +
"WHERE Genre = #Genre";
sqlCmd = new SqlCommand(sqlString, myConnection);
sqlCmd.Parameters.AddWithValue("#Genre", GenreID.Value);
sqlReader = sqlCmd.ExecuteReader();
if (sqlReader.HasRows)
{
childRepeater.DataSource = sqlReader;
childRepeater.DataBind();
}
else
{
childRepeater.DataSource = "";
childRepeater.DataBind();
}
}
}
Everything up to this point works fine. I get my Genre groups and within each Genre I get the movie titles that are flagged against that Genre, and also the Linkbutton against each movie title. It's the LinkButton that is NOT firing the OnClick event.
Here is the code i'm using for the OnClick event.
protected void LoadMovieTitle (Object sender, EventArgs e)
{
LinkButton linkb = (LinkButton)(sender);
string args = linkb.CommandArgument;
Response.Redirect("movieinfopage.aspx?id=" + args);
}
The solution I am hoping to get help with is what do I need to do in order for those LinkButtons to fire their OnClick event when clicked, and access LoadMovieTitle. When I put a break in this method, it does not break, telling me that the OnClick event is not finding or recognising it.
You need to add OnItemCommand to repeater and CommandName to LinkButton:
<asp:Repeater ID="RepeaterMovies" runat="server" OnItemCommand="RepeaterMovies_OnItemCommand">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "MovieName") %>
<asp:LinkButton ID="lbMovieInfo" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "MovieID") %>' CommandName="MovieDetail">
Access Info
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
Server side:
protected void RepeaterMovies_OnItemCommand(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("MovieDetail"))
{
LinkButton linkb = (LinkButton)(sender);
string args = linkb.CommandArgument;
Response.Redirect("movieinfopage.aspx?id=" + args);
}
}
See:
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/data-access/custom-button-actions-with-the-datalist-and-repeater/custom-buttons-in-the-datalist-and-repeater-cs
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.
Here is my part of Shop.aspx code:
<% foreach(var item in items){ %>
...
<asp:Button id="btnBuy" runat="server" class="btn" Text="Buy" OnClick ="btnBuy_Click" CommandArgument='<%#Eval("item.id") %>' />
<% } %>
I've got a loop where I create few buttons to my shopping items, and I need them to have id of my item
protected void btnBuy_Click(object sender, EventArgs e)
{
int itemId = Convert.ToInt32(btnBuy.CommandArgument);
}
On click i need to have id of the item/button clicked, to save them later in my database.
Problem -
When i click on button btnbuy.CommandArgument is "".
It's wrong: you wrote c#in asp style. You have to use a Repeater and then you can manage ItemCommand for every button click.
Aspx
<asp:Repeater ID="myRepeater" runat="server" OnItemCommand="myRepeater_ItemCommand">
<ItemTemplate>
<asp:Label id="myLbl" runat="server" Text='<%# ((Item)(Container.DataItem)).ProductName %>'/>
<asp:Button id="btnBuy" runat="server" CssClass="btn" Text="Buy" CommandName="Click" CommandArgument='<%# ((Item)(Container.DataItem)).ProductId %>' />
</ItemTemplate>
</asp:Repeater>
c# (eg. OnLoad)
List<Item> myCollection = ...; // get list of items
myRepeater.DataSource = myCollection;
myRepeater.DataBind();
...
protected void myRepeater_ItemCommand(Object sender, RepeaterCommandEventArgs e)
{
if(e.CommandName == "Click")
{
int idRecord = Convert.ToInt32(e.CommandArgument);
// do something using idRecord or
// get sender properties: ((Button)e.CommandSource).Text
}
}
i need your help to this case i can't find any solution in the web
I have datalist that build like this :
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Name") %>
<asp:Button ID="Button1" runat="server" Text="Button" />
<%# DataBinder.Eval(Container.DataItem, "Enabled") %>
<asp:Button ID="Button2" runat="server" Text="Button" />
<asp:ImageButton ID="ImageButton1" runat="server" />
</ItemTemplate>
I want to enable the button just if the Enabled value that i get from the DB is equal to 1 , i have try to make this code below but didn't success.
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
int EnableDisable = Convert.ToInt32(((DataRowView)e.Item.DataItem).Row.ItemArray[1]);
if (EnableDisable != 1)
{
Button BT = e.Item.FindControl("ImageButton1") as Button;
BT.Enabled = true;
}}
any idea, can you help me ?
Thank you so much.
I don't see in your code where you disable the button when necessary. Have you tried this:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
int EnableDisable = Convert.ToInt32(((DataRowView)e.Item.DataItem).Row.ItemArray[1]);
ImageButton BT = e.Item.FindControl("ImageButton1") as ImageButton;
BT.Enabled = (EnableDisable == 1);
}
change this line
Button BT = e.Item.FindControl("ImageButton1") as Button;
by
ImageButton BT = e.Item.FindControl("ImageButton1") as ImageButton;
I'm creating a really simple "tag cloud" function for a project database. I want the user to be able to check various tags and, then, with an autopostback on the checked box(es), display the associated projects that have those tags.
It works, but the checkboxes are resetting after postback. I want the checkboxes to remain checked (or unchecked, as the case may be) so the user can select multiple tags.
I've searched and seen that some say to put the databinding event within an if(!isPostBack). I've tried that, but it doesn't seem to matter.
aspx code:
<asp:DataList ID="TagDataList" runat="server" OnDataBinding="databindthesucker" DataKeyField="TagName">
<ItemTemplate>
<asp:CheckBox AutoPostBack="true" ID="TagCheckbox" runat="server" Text='<%#Eval ("dZTagName") %>' />
<asp:HiddenField ID="TagNameHidden" runat="server" Value='<%#Eval ("dZTagName") %>'/><br />
</ItemTemplate>
</asp:DataList>
<asp:DataList ID="ProjectDataList" runat="server" DataKeyField="projectID">
<ItemTemplate>
<asp:HyperLink ID="ProjectLink" runat="server" NavigateUrl='<%# "/Project/Default.aspx?PID=" + Eval ("projectID") %>' Text='<%# Eval ("projectID") + ": " + Eval ("projectName") %>'> </asp:HyperLink><br />
</ItemTemplate>
</asp:DataList>
and then the cs...
protected void Page_Load(object sender, EventArgs e)
{
displaytags(sender, e);
}
protected void displaytags(object sender, EventArgs e)
{
var qrygettags = (from t in db.TagTables
select new { t.TagName }).Distinct().ToList();
TagDataList.DataSource = qrygettags;
TagDataList.DataBind();
CheckBox TagCheckbox = (CheckBox)TagDataList.Items[0].FindControl("TagCheckbox");
}
protected void databindthesucker(object sender, EventArgs e)
{
foreach (DataListItem item in TagDataList.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
CheckBox TagCheckbox = (CheckBox)item.FindControl("TagCheckbox");
HiddenField TagNameHidden = (HiddenField)item.FindControl("TagNameHidden");
string Tagstring = TagNameHidden.Value.ToString();
if (TagCheckbox.Checked)
{
var qrygetprojects = (from p in db.projects
join pt in db.TagTables on p.projectID equals pt.projectID
where pt.TagName == Tagstring
select new
{
pt.projectID,
p.projectName
}).ToList();
ProjectDataList.DataSource = qrygetprojects;
ProjectDataList.DataBind();
}
}
}
}
Any ideas?