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;
}
}
Related
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;
};
}
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;
}
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 had placed div with condition.. on my execution,if condition is also displayed, i need to remove it..
**My New Code**
<asp:TemplateField HeaderStyle-Width="90px" ItemStyle-Width="0">
<ItemTemplate>
<div style="cursor: pointer; padding-top: 02px;" onclick="ShowfllDetails(<%#Eval("StudentID")%>);">
if(<%# (Eval("StatusName").Equals("Processed")) %>)
{
//should not show the upload button
}
else
{
<u>Upload </u> //show the upload button
}
</div>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#(Eval("StatusName").Equals("Processed") ? "images/add_btn.png" : "")%>' />
</ItemTemplate>
I'm getting displayed the if condition i need not to display it..
Thank-You.
I have done such a functionality inside Gridview, I assume you are doing the same. In place of tag you can use linkbutton set commandArgument and Commandname properties. AFter that fire Gridview_Rowcommand event. when ever you will click on linkbutton this event will fire and you can set the status in database or somewhere in session that this link is clicked against student id
<asp:LinkButton ID="LinkButton1" runat="server" Text="Upload" CommandName="Upload"
CommandArgument='<%#Eval("StudentID")%>'></asp:LinkButton>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Upload")
{
// get student id against clicked link button
int studentid = Convert.ToInt16(e.CommandArgument);
// -- set status in database it is clicked
}
}
after this bind your grid and on rowdatabound find control and set the visibility to true or false (Processed/Not Processed)
Bind your "StatusName" database field to a label and set the visibility to false of Label so that it should not display.
Now take idea from following code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
Label lblAssignto = (Label)e.Row.FindControl("lblassignto");
LinkButton addevent = (LinkButton)e.Row.FindControl("lnkBtnAddEvent");
LinkButton showevent = (LinkButton)e.Row.FindControl("lnkBtnShowEvent");
if (string.IsNullOrEmpty(lblAssignto.Text))
{
addevent.Visible = false;
showevent.Visible = false;
}
else
{
addevent.Visible = true;
showevent.Visible = true;
}
}
}