trigger button inside a repeater control - c#

I have a button inside a repeater control and I would like to disable it. I tried something like this...
if (Session["USER_ID"] == null)
{
//disable download button and
}
else
{
//enable download button
}
This is the button I want to enable and disable btnTEST
<asp:Repeater ID="Repeater1" runat="server"
OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Image ID="image" ImageUrl='<%# Eval("image_src")%>' runat="server" />
<asp:Button ID="btnTEST" runat="server" Text="Click Me!" CommandName="testme" Enabled="False" />
</ItemTemplate>
</asp:Repeater>
I am unable to use this:
btnTest.Enabled = True;
It doesn't work for some reason.

Since the button is inside the Repeater-control it will be dynamically generated as many times as there are items in the datasource databound. So you have to enable/disable the button on the repeater ItemDataBound-event as so:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Button btn = (Button)e.Item.FindControl("btnTEST");
if (Session["USER_ID"] != null)
{
btn.Enabled = true;
}
}
}
Ps. No need to disable since the button is disabled by default.

Because the button is in a repeater. You need to disable the button in the itemdatabound. I suggest you to add something like this in the code behind:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Repeater1.ItemDataBound += (s, ev) =>
{
if (ev.Item.ItemType != ListItemType.AlternatingItem && ev.Item.ItemType != ListItemType.Item)
return;
var btnTest= ((System.Web.UI.WebControls.Button) ev.Item.FindControl("btnTEST"));
btnTest.Enabled = Session["USER_ID"] != null;
};
}

Related

button click event in repeater control to show data in same repeater in asp.net c#

I have a Button inside a repeater on whose click textbox should be visible to user to enter, but i have a list of button and clicking on specific button textbox should open specifically for that button only,
Currently when i click button all the textboxs gets visible to user.
Here Is The Code....
<asp:Repeater ID="rpt">
<div align="right" id="reply">
<asp:LinkButton ID="lnkbtnreply" OnClick="lnkbtnreply_Click" Text="Reply"></asp:LinkButton>
</div>
<asp:TextBox ID="" placeholder="Enter Your Reply Here" Visible="false">
</asp:TextBox>
</asp:Repeater>
Code Behind:
protected void lnkbtnreply_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in rptcomment.Items)
{
Panel replypic = (Panel)item.FindControl("replypic");
Panel replywrite = (Panel)item.FindControl("replywrite");
replypic.Visible = true; replywrite.Visible = true;
}
}
I have Found The Answer. In Case You Guys Are Still Searching For It Have A Look :
Here is the Code Behind:
protected void rptcomment_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Panel replypic = (Panel)e.Item.FindControl("replypic");
Panel replywrite = (Panel)e.Item.FindControl("replywrite");
if (e.CommandName == "img_Click") // check command is cmd_delete
{
// get you required value
string CustomerID = (e.CommandArgument).ToString();
replypic.Visible = true;
replywrite.Visible = true;
}
}
}

Prevent DataList to Refesh for ClickEvents

I have a DataList Control with HeaderTemplate as LinkButtons.
The markup is
<asp:DataList runat="server" ID="dlYears" OnItemDataBound="dlYears_ItemDataBound">
<HeaderTemplate>
<asp:LinkButton ID="lblYear1" runat="server" OnClick="Year_Click"></asp:LinkButton>
<asp:LinkButton ID="lblYear2" runat="server" OnClick="Year_Click"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblValue" runat="server"Text='<%# String.Format("{0:f2}",DataBinder.Eval(((IDataItemContainer)Container).DataItem, "Value")) %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
Actually this datalist contains year numbers as Headers as 2016, 2015.
Binding as
dlYearlys.DataSource = ds;
dlYearlys.DataBind();
On the ItemDataBound I'm styling the values of totals as
protected void dlYearlyTotals_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int i = e.Item.ItemIndex;
if (val != ((Label)e.Item.FindControl("lblValue")).Text)
e.Item.ForeColor = System.Drawing.Color.Red;
}
}
On clicking the headers I'm displaying the another datalist values.
protected void Year_Click(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
BindAnotherDataList((Convert.ToInt16(btn.Text)));
}
This causing the page to refresh and the year values where I'm changing the colour as red is no more being displayed.
So how can I make dlYears Datalist control not to refresh and retain their colour on clicking of headers
Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BinddlYear();
// Binding Some other controls
}
}

How to disable button in row databound event in gridview?

i want to disable button in row data bound . when its text or value is 'Waiting for Approval'. im getting this error . Object reference not set to an instance of an object.// button.Enabled = true;
protected void GridCategoryWise_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
Button button = (Button)e.Row.FindControl("btnReportedlink");
string Id =((DataRowView)e.Row.DataItem)["ReportLinks"].ToString();
if (Id == "Waiting for Approval")
{
button.Enabled = false;
}
else
{
button.Enabled = true;
}
}
my aspx
<asp:TemplateField HeaderText="Reportd Link" ItemStyle-HorizontalAlign="center" >
<ItemTemplate>
<button onclick="window.open('<%#Eval("ReportLinks")%>', '_blank');" title='<%#Eval("ReportLinks")%>' id="btnReportedlink" runat="server"> Link</button>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
Why are you using the HTML <button /> element ? use <asp:button /> web server control from asp.net for a better control over reading and disabling the server controls.
Use the OnClientClick property to specify additional client-side script that executes when a Button control's Click event is raised.
<ItemTemplate>
<asp:button onclientclick="javascript:window.open('<%#Eval("ReportLinks")%>', '_blank');"
text='<%#Eval("ReportLinks")%>' id="btnReportedlink" runat="server"/>
</ItemTemplate>
With the above setup, now you will be able to access the button in row data bound event with NO more object references error.
use DataBinder work fine
protected void GridCategoryWise_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
Button button = (Button)e.Row.FindControl("btnReportedlink");
string Id = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ReportLinks"));
if (Id == "Waiting for Approval")
{
button.Enabled = false;
}
else
{
button.Enabled = true;
}
}

Setting Button in listview Visibility to False if query result is "0"

I am building a web application in ASP.net and I have a little problem.
I have a LISTVIEW to display data from a data source, and in that listview I have included a BUTTON in every row to be visible if the result of the query in the Page_load is 0.
The Query works, but I don't know how to select the button in the query.
I have tried
ListView1.FindControl("hiddenButton").visible = false;
this is the buttons code
<asp:Button ID="hiddenButton" runat="server" CommandArgument ='<%# Eval("ProfileId") %>' Text="Add Friend" CssClass="btn btn-info pull-right" OnClick="addFriend_Click" Width="105px" allign="right"/>
But its not working.
You can do this in ItemDataBound event:-
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType==ListViewItemType.DataItem)
{
if (YourCondition)
{
Button hdn = (Button)e.Item.FindControl("hiddenButton");
hdn.Visible = false;
}
}
}
You need to associate this event handler in your mark-up(if not already done):-
<asp:ListView ID="ListView1" OnItemDataBound="ListView1_ItemDataBound">
</asp:ListView>
You can use ItemDataBound event To set Buttons visible to True/False
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Button hiddenButton=(Button) dataItem.FindControl("hiddenButton");
hiddenButton.Visible = false;
}
}

AJAX.NET Subscribe to Reorderlist ItemCommand or DeleteCommand?

I would like to subscribe to the ItemCommand event of a Reorderlist I have on my page. The front end looks like this...
<cc1:ReorderList id="ReorderList1" runat="server" CssClass="Sortables" Width="400" OnItemReorder="ReorderList1_ItemReorder" OnItemCommand="ReorderList1_ItemCommand">
...
<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="delete.jpg" CommandName="delete" CssClass="playClip" />
...
</cc1:ReorderList>
in the back-end I have this on Page_Load
ReorderList1.ItemCommand += new EventHandler<AjaxControlToolkit.ReorderListCommandEventArgs>(ReorderList1_ItemCommand);
and this function defined
protected void ReorderList1_ItemCommand(object sender, AjaxControlToolkit.ReorderListCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.CommandName == "delete")
{
//do something here that deletes the list item
}
}
}
Despite my best efforts though, I can't seem to get this event to fire off. How do you properly subscribe to this events in a ReorderList control?
this works:
<cc2:ReorderList ID="rlEvents" runat="server" AllowReorder="True" CssClass="reorderList"
DataKeyField="EventId" DataSourceID="odsEvents" PostBackOnReorder="False"
SortOrderField="EventOrder" OnDeleteCommand="rlEvents_DeleteCommand">
...
<asp:ImageButton ID="btnDeleteEvent" runat="server" CommandName="Delete" CommandArgument='<%# Eval("EventId") %>' ImageUrl="~/images/delete.gif" />
...
</cc2:ReorderList>
code behind:
protected void rlEvents_DeleteCommand(object sender, AjaxControlToolkit.ReorderListCommandEventArgs e)
{
// delete the item
// this will give you the DataKeyField for the current record -> int.Parse(e.CommandArgument.ToString());
//rebind the ReorderList
}
Since your ImageButton's CommandName="delete" you should be hooking up to the DeleteCommand event instead of ItemCommand.

Categories

Resources