Conditionally show hide asp linkbutton - asp.net

I have below link button and I need to show it only if grid has records.
<asp:LinkButton ID="linkButton1" runat="server" ToolTip="Delete file" Visible='<%# (Convert.ToInt32(Eval("gridViewFileInformation.Rows.Count"))>0) %>' >Delete</asp:LinkButton>
But it shows always.

In your code behind file after databind of "gridViewFileInformation" you can set the Visible property of linkButton1. Similar question
Something like:
ASPX File:
<asp:GridView
runat="server"
ID="gridViewFileInformation"
OnDataBound="gridViewFileInformation_DataBound">
</asp:GridView>
CS File:
protected void gridViewFileInformation_DataBound(object sender, EventArgs e)
{
linkButton1.Visible = (gridViewFileInformation.Rows.Count > 0);
}

Related

How to change the css of a dynamically created link button in repeater

I am loading link button in in a repeater. i want to change the particular link button css class when the user clicks it.
<asp:Repeater ID="moviedaterepeater" runat="server">
<ItemTemplate>
<li>
<asp:LinkButton ID="theatrelinkbutton" runat="server" Text='<%#Eval("datetext") %>' CausesValidation="false" CommandName='<%#Eval("datevalue") %>' CommandArgument='<%#Eval("datetext") %>' OnCommand="moviedate_Command"></asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
how to do it i tried with the itemdatabound
protected void moviedaterepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
//Session["linkbuttonid"] = "ctl00_ContentPlaceHolder1_moviedaterepeater_ctl01_theatrelinkbutton";
////Label lbl = (Label)e.Item.FindControl("Label1");
//LinkButton link = (LinkButton)e.Item.FindControl(Session["linkbuttonid"].ToString());
//link.CssClass = "active";
}
}
but i want to change the value based on the text of the linkbutton .
First change your repeater to this:
<asp:Repeater ID="moviedaterepeater" runat="server" OnItemCommand="moviedaterepeater_ItemCommand">
Then in code behind:
protected void moviedaterepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
LinkButton lnk = (LinkButton)e.Item.FindControl("theatrelinkbutton");
lnk.CssClass = "YourCSSClass";
}
Try this:
LinkButton theatrelinkbutton = (LinkButton)e.Item.FindControl("theatrelinkbutton");
theatrelinkbutton.CssClass = theatrelinkbutton.Text;
You need to add this in ItemDataBound event of your repeater control. Please note you should have this event binded:-
<asp:Repeater ID="moviedaterepeater" runat="server" OnItemDataBound="moviedaterepeater_ItemDataBound">

Dropdownlist disappear when put it in EditItemTemplate

If I put my dropdownlist column in ItemTemplate, it appear but I can't change the value. When I put it in EditItemTemplate like this:
<EditItemTemplate>
<asp:DropDownList DataValueField="COLUMN_NAME" DataTextField="COLUMN_NAME" DataSource='<%#GetDataSourceDesCol()%>' Width="90%" Visible=true ID="ddlDesCol" runat="server">
</asp:DropDownList>
</EditItemTemplate>
Then my ddl is not showing any more. How to fix it?
p/s: Even that I try with a new project and simple code like:
<asp:GridView ID="GridView1" AutoGenerateColumns=false runat="server">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The ddl is not showing too!
All the controls inside the edit item template will be visible only when the grid view is in the edit mode.
So, you need to set the grid to edit mode. Inorder to make your code to work.
Hope this helps..
if you want to put entire grid view in edit mode:
protected void btnEdit_Click(object sender, EventArgs e)
{
GridView1.EditIndex = 1;
}
if you want a particular row to be in edit mode
Just implement the Row_Editing event and do something like this:
protected void Row_Editing(object sender, GridViewEditArgs e)
{
myGridView.EditItemIndex = e.EditItemIndex;
BindData();
}
Bind data will populate the GridView with the data.

How to get Reference to the label in repeater item in code behind

<asp:repeater id="rpt" run="server">
<ItemTemplate>
<asp:LinkButton id="Delete" runat="server" OnCommand="Delete_Command"></asp:linkButton>
<asp:label id="lblMessage" run="server">
</ItemTemplate>
</asp:repeater>
Code Behind:
protected void Delete_Command(object sender, CommandEventArgument e)
{
}
how i get the reference to the "lblMessage" in Delete_Command.
Try this:
protected void Delete_Command(object sender, CommandEventArgs e)
{
LinkButton button = (LinkButton)sender;
Label label = (Label)button.NamingContainer.FindControl("lblMessage");
// do something with the label
}
If you:
Have bound the repeater
Have ViewState enabled
Do not re-bind the repeater earlier in the post back
this should work. If not, please verify that the id of the label is indeed exactly the same as in the ...FindControl("lblMessage");. Also make sure that runat="server" is set on all the controls involved.
Edit: One more thing to check: Search the markup file (the .aspx file) and check if there are any other controls that also use the same event in the code behind. If another control is using the same event handler and that control is not in the repeater, the label will not be found.
means are you want find a lable in Delete_Command event?
in aspx
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:LinkButton ID="Delete" runat="server" OnCommand="Delete_Command"></asp:LinkButton>
<asp:Label ID="lblMessage" run="server">
</ItemTemplate>
</asp:Repeater>
in aspx.cs
protected void Delete_Command(object sender, CommandEventArgs e)
{
foreach (RepeaterItem item in rpt.Items)
{
Label lblMessage = item.FindControl("lblMessage") as Label;
if (lblMessage != null)
{
lblMessage.Text = "";
}
}
}
If You want to make it in your way use following code in
protected void Repeater1_ItemCommand(object source, CommandEventArgs e)
{
(((LinkButton)source).NamingContainer).FindControl("lblName")
}
Another approach.. But something that you can buy
aspx
<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%=Eval("Name") %>' ></asp:Label>
<asp:LinkButton runat="server" CommandName="Delete_Command" Text="sd"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
.cs
protected void Delete_Command(object sender, CommandEventArgument e)
{
if(e.CommandName != null)// Conditional Check
{
Label label = e.Item.FindControl("lblMessage");
// do something with the label
}
}

Open new dynamic window within gridview dataview?

I have a gridview (called grdSearchResults) that contains links to pdfs. The pdf url's are returned from the database but I'm not sure how to add the url to an imagebutton control within the gridview.
Here's my codebehind:
List<SearchResults> search = _searchRepository.GetFactFileSearchResults(results);
grdSearchResults.DataSource = search;
grdSearchResults.DataBind();
And here's my aspx page code:
<asp:TemplateField HeaderText="FILE TYPE" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton runat="server" ID="_imgbtnPreview" OnClientClick="<%# Container.DataItem("DownloadUrl") %>" ImageUrl="~/images/fileType_pdf.png" />
</ItemTemplate>
But this thows an error ("The server tag is not well formed") Any idea how to fix this? Thanks
I assume you want to call a javascript function OnClientClick. But you haven't showed us it so it's difficult to help.
You can also set the ImageButton's ImageUrl in RowDataBound (which i normally prefer):
protected void grdSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView rv = (DataRowView) row.DataItem;
ImageButton imgbtnPreview = (ImageButton)e.Row.FindControl("_imgbtnPreview");
imgbtnPreview.ImageUrl = rv.Row.Field<String>("DownloadUrl");
}
}
Your OnClientClick should be using single quote in the outer
OnClientClick='<%# Container.DataItem("DownloadUrl") %>'
And the image can be
<asp:Image runat="server" ID="x" ImageUrl='<%# String.Format("~/{0}", Eval("ImageUrl")) %>' />

gridview edit requires to click twice

Why is that I need to click the edit link twice, in a gridview control, before my row enters into edit mode?
<asp:ObjectDataSource ID="ods" runat="server" TypeName="Employee"
SelectMethod="GetAll" ></asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" CssClass="styled"
OnRowCommand="gv_RowCommand" DataSourceID="ods"
OnSorting="gv_Sorting" >
<Columns>
...........
</Columns>
<ItemTemplate>
<ItemTemplate>
<div class='actions'>
<asp:Button ID="btnEdit" runat="server" Text=" Edit " ToolTip="Edit Row" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"Id") %>' CausesValidation="False" />
<span style="padding-left:10px"></span>
</div>
</ItemTemplate>
</asp:GridView>
protected override void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.ods.SelectParameters[0].DefaultValue = "";
}
}
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == CRUID.Edit.ToString())
{
this.gv.ShowFooter = false;
}
}
You need to avoid rebinding your gridview on each postback.
If not ispostback then
GridView1.DataSource = dt
GridView1.DataBind()
end if
Otherwise you just overwrite Gridview changes.
Great explanation at this link...
http://www.pcreview.co.uk/forums/gridview-two-clicks-needed-enter-place-editing-t3328887.html
Try handling the RowEditing event to set the EditItem Index:
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex
}
There are some mistakes in your code as i examined. Correct your code as shown below:
<asp:ObjectDataSource ID="ods" runat="server" TypeName="Employee"
SelectMethod="GetAll" ></asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" CssClass="styled"
OnRowCommand="gv_RowCommand" DataSourceID="ods"
OnSorting="gv_Sorting" >
<Columns>
...........
<asp:TemplateField>
<ItemTemplate>
<div class='actions'>
<asp:Button ID="btnEdit" runat="server" Text=" Edit " ToolTip="Edit Row" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"Id") %>' CausesValidation="False" />
<span style="padding-left:10px"></span>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected override void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.ods.SelectParameters[0].DefaultValue = "";
}
}
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
this.gv.ShowFooter = false;
}
}
If on using this code the problem does not solve then there may be some problem in your cssclass which you used with your GridView as I have Checked your code on my machine using ObjectDataSource and it works well using edited code.
Also I want to know that what is CRUID in CRUID.Edit.ToString()
and why you used the following line in Page_Load event
this.ods.SelectParameters[0].DefaultValue = "";
as there are no parameter associated with your SelectMethod="GetAll" method used in ObjectDataSource.
May this answer help you.
I guess there is some conflict with the updatepanels on your page..
Try removing all your Update Panels and try again.. It will work for sure.. Mine worked a few seconds ago.. so thought It would be good to share..

Resources