How do i disable/hide a button inside that is inside a itemtemplate? i want to hide the save button on load, and then show it and hide the edit button when the edit button is clicked.
The template:
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButtonEdit" runat="server" CommandName="Edit" ImageUrl="loginstyling/images/Edit.png" Visible="true" />
<asp:ImageButton ID="ImageButtonUpdate" runat="server" CommandName="Update" ImageUrl="loginstyling/images/Save.png" Visible="true" OnClick="ImageButtonUpdate_Click" />
<asp:ImageButton ID="ImageButtonDelete" runat="server" CommandName="Delete" ImageUrl="loginstyling/images/Remove.png"/>
</ItemTemplate>
</asp:TemplateField>
Behind:
private void ImageButtonUpdate_Click(object sender, EventArgs e)
{
ImageButtonUpdate.Enabled = false; // not working, dosnt find the button
}
use the sender of the event
protected void ImageButtonUpdate_Click(object sender, EventArgs e){
ImageButton btnupdate= sender as ImageButton;
btnupdate.Enabled = false;
//if you need to get other controls in the same row
GridViewRow row = (GridViewRow)btnupdate.NamingContainer;
var btnedit= (ImageButton)row.FindControl("ImageButtonEdit");
btnedit.Enabled = false; // do enable or disable as you need
}
You Need to retrieve the ImageButton as follows as it is residing in the GridView
private void ImageButtonUpdate_Click(object sender, EventArgs e)
{
//Get the ImageButton that raised the event
ImageButton btn = (ImageButton )sender;
btn .Enabled = false;
}
Edit
Step 1
Provide same Onclick event for Update and Edit i.e
OnClick="ImageButtonUpdate_Click"
Step 2
Use CommandName Attribute
ImageButton btn = (ImageButton )sender;
if(btn.CommandName =="Update")
{
btn.Enabled = false;
}
if(btn.CommandName =="Edit")
{
btn.Enabled = false;
}
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 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;
}
}
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;
}
}
I want to prompt the user for confirmation when he tries to delete a record in a detail view? I have command filed in which showDeletebutton set to true.
I found how to do the confirmation for gridview, but how can I modify to match detail view?
Code:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// loop all data rows
foreach (DataControlFieldCell cell in e.Row.Cells)
{
// check all cells in one row
foreach (Control control in cell.Controls)
{
// Must use LinkButton here instead of ImageButton
// if you are having Links (not images) as the command button.
ImageButton button = control as ImageButton;
if (button != null && button.CommandName == "Delete")
// Add delete confirmation
button.OnClientClick = "if (!confirm('Are you sure " +
"you want to delete this record?')) return;";
}
}
}
}
Anybody?
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"
.....
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity"
SortExpression="Quantity" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="New" Text="New"></asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView
This can be done easily on the markup code. I simply added the js code to the onClientClick property of the delete button:
OnClientClick="return confirm('Are you sure you want to delete this record');"
Or if you want do this in the code behind:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
LinkButton bttn = (LinkButton)DetailsView1.FindControl("lnkDelete");
bttn.OnClientClick = "return confirm('Are you sure you want to delete this record!');";
}
I found the answer to my question.
My answer:
protected void DViewComputer_DataBound1(object sender, EventArgs e)
{
int noRow = DViewComputer.Rows.Count - 1;//get the no of record
if (noRow >0)
{
Button button = (Button)(DViewComputer.Rows[noRow].Cells[0].Controls[2]);
// Add delete confirmation
((System.Web.UI.WebControls.Button)(button)).OnClientClick = "if (!confirm('Are you sure " +
"you want to delete this record?')) return;";
}
}
Anyways thanks for your help guys.
foreach (Control control in cell.Controls)
{
// Must use LinkButton here instead of ImageButton
// if you are having Links (not images) as the command button.
ImageButton button = control as ImageButton;
if (button != null && button.CommandName == "Delete")
// Add delete confirmation
button.Attributes.Add("onclick","your javascript here");
}
Please see the below URL......
http://www.codeproject.com/Articles/32756/ASP-NET-GridView-delete-confirmation-using-asp-Com
This corrects the OP's solution. The code was translated from the code found here: http://forums.aspfree.com/net-development-11/confirm-button-when-deleting-detailsview-120113-2.html
protected void dvEvent_DataBound(object sender, EventArgs e)
{
int commandRowIndex = dvEvent.Rows.Count - 1;
if (commandRowIndex > 0)
{
DetailsViewRow commandRow = dvEvent.Rows[commandRowIndex];
DataControlFieldCell cell = (DataControlFieldCell)commandRow.Controls[0];
foreach (Control ctrl in cell.Controls)
{
if (ctrl is ImageButton)
{
ImageButton ibt = (ImageButton)ctrl;
if (ibt.CommandName == "Delete")
{
ibt.ToolTip = "Click here to Delete";
ibt.CommandName = "Delete";
ibt.Attributes["onClick"] = "if (!confirm('Are you sure " +
"you want to delete this record?')) return;";
}
}
}
}
}
I have this markup:
<asp:DetailsView ID="dvDatabase" OnModeChanging="dvDatabase_ModeChanging">
<HeaderTemplate>
<asp:Button ID="btnView" runat="server" CausesValidation="False" CommandName="Cancel"
Text="View" CssClass="btn btn-primary" Visible="false" />
<asp:Button runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit" CssClass="btn btn-success" ID="btnEdit" />
<asp:Button runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete" CssClass="btn btn-danger" />
</HeaderTemplate>
...
Then I have this C#:
protected void dvDatabase_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
bool isEdit = DetailsViewMode.Edit == e.NewMode;
DetailsView view = (DetailsView)sender;
Button viewButton = (Button)view.FindControl("btnView");
Button editButton = (Button)view.FindControl("btnEdit");
viewButton.Visible = isEdit;
editButton.Visible = !isEdit;
}
I've done some debugging and the Visible property gets set correctly, but I never see the buttons change. I hit the Edit button and I'm in edit mode, but the Edit button is still displayed and the View button is still hidden. I've tried finding the buttons via dvDatabase.FindControl directly, rather than using the object sender variable, but that doesn't work either. I tried to refer to the buttons with variables based on the ID attribute in the markup, but btnView and btnEdit variables/properties don't exist. What's going on?
Edit: I switched to OnModeChanged as per Tim's suggestion, but the buttons still don't change. Here's my C# now:
protected void dvDatabase_ModeChanged(object sender, EventArgs e)
{
DetailsView view = /*(DetailsView)sender*/dvDatabase;
bool isEdit = DetailsViewMode.Edit == view.CurrentMode;
LinkButton viewButton = (LinkButton)view.FindControl("btnView");
LinkButton editButton = (LinkButton)view.FindControl("btnEdit");
viewButton.Visible = isEdit;
editButton.Visible = !isEdit;
}
I tried using the object sender as well as the dvDatabase class variable, but neither seemed to have an effect.
Use the DetailsView's DataBound event instead and only databind the DetailsView if(!Page.IsPostback). You also need to handle the ItemCommand event to call the appropriate ChangeMode method and databind the DetailsView.
protected void dvDatabase_DataBound(Object sender, EventArgs e)
{
var view = (DetailsView)sender;
var btnView = (Button)view.FindControl("btnView");
var btnEdit = (Button)view.FindControl("btnEdit");
switch (view.CurrentMode)
{
case DetailsViewMode.ReadOnly:
btnView.Visible = false;
btnEdit.Visible = true;
break;
case DetailsViewMode.Edit:
btnView.Visible = true;
btnEdit.Visible = false;
break;
case DetailsViewMode.Insert:
btnView.Visible = false;
btnEdit.Visible = false;
break;
default:
break;
}
}