I need disable the NavigateUrl property on the HyperLink when the value of variable aut is less than zero.
I have tried this in code-behind of my .cs page, without success because the NavigateUrl property on the HyperLink is enabled, although it opens a blank page ( on window popup ) in the browser, when click on ImageUrl
Can anybody help me?
Thanks in advance
My code below :
.cs
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (aut > 0)
{
HyperLink button = (HyperLink)e.Row.FindControl("MMM");
button.Enabled = true;
}
else
{
HyperLink button = (HyperLink)e.Row.FindControl("MMM");
button.Enabled = false;
button.NavigateUrl = "";
}
}
}
.aspx
<asp:TemplateField HeaderText="MMM" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new">
<ItemTemplate>
<asp:HyperLink ID="MMM" runat="server" NavigateUrl='<%# (String.IsNullOrEmpty(Eval("MMM").ToString()) ? String.Format("http://...?sId={0}&s=2", HttpUtility.UrlEncode(Base64ForUrlEncode(Eval("id").ToString()))) : "") %>'
ImageUrl='<%#(String.IsNullOrEmpty(Eval("MMM").ToString()) ? "/Images/bullett/redbul.gif" : "/Images/bullett/forestbul.gif")%>'
ToolTip='<%#(String.IsNullOrEmpty(Eval("MMM").ToString()) ? "Not Exists" : "Exists")%>'
Target="_blank" BorderStyle="None" ForeColor="Transparent" OnClick="if (!confirm('Confirm ?'))return false;window.open(this.href,'playsample','width=500,height=500,left=100,top=100,scrollbars=yes,dependent=yes,toolbar=no,location=no,status=no,directories=no,menubar=no,status=no,resizable=yes');return false;"
Enabled='<%#(!String.IsNullOrEmpty(Eval("MMM").ToString()) ? false : true)%>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
All those properties you're setting on your controls in your codebehind are being overwritten by the code within your <% %> tags in your aspx. For example, whatever you set button.Enabled in your C# code, will be overwritten later on in the page lifecycle by this:
Enabled='<%#(!String.IsNullOrEmpty(Eval("MMM").ToString()) ? false : true)%>'
I'm guessing that all the code like this in your aspx was written by someone else, before you started working on this project? If so, the easiest solution is probably to remove your GridView1_RowDataBound function, and instead modify the code inside the <% %> tags to change the behavior to what you need.
That being said, I will say that the way you're TRYING to do it is actually more correct. If you have the luxury of time, and you aren't afraid to refactor that existing code, you could instead move all that logic inside the <% %> tags to your GridView1_RowDataBound function, and remove the <% %> tags from your aspx so that it would just be:
<asp:TemplateField HeaderText="MMM" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new">
<ItemTemplate>
<asp:HyperLink ID="MMM" runat="server">
</asp:HyperLink>
</ItemTemplate>
Of course, as I said, you would still need to recreate all of that removed logic in your GridView1_RowDataBound function, which I haven't bothered to show here.
Related
I have a Gridview that works like a timetable. There are 3 Columns in the gridview.
Columns are as below
From
Till
Reservate
I want to have a button on the reservate column as long as the spot is free. If the spot is taken I want to display some text instead.
Currently I am using below markup
<asp:TemplateField HeaderText="Kranbeladung - Crane loading">
<ItemTemplate>
<asp:Button ID="rBtn" runat="server" Text="Reservate" />
</ItemTemplate>
</asp:TemplateField>
Any idea how I can achieve that?
Thanks
Greg answer is great but I had to do some changes. With his code I got this in my page:
System.Web.UI.WebControls.Button
instead of the Control itself.
In order to work I returned the HTML of the Cotrol, like that:
protected string CheckAvailability(object obj)
{
//some logic
return "<asp:Button runat='server'>ButtonText</asp:Button>";
}
Since you have a trigger, you would have a method that would test the column. Then populate according to the result. Below is what you would have in your Grid.
<asp:TemplateField HeaderText="...">
<ItemTemplate>
<%# CheckAvailability(Eval("Column")) %>
</ItemTemplate>
</asp:TemplateField>
Then you would do the following in code behind:
protected Control CheckAvailability(object flag)
{
if(flag != null)
{
// Create a button, then return it.
}
else
{
// Create a label, say space available and return.
}
}
I feel like this is a very simple solution that I am overlooking somehow. Basically I have a web page (using asp.net webforms) with a datalist. Inside my datalist, and inside the datalist items I have a div that I want the user to be able to click to cause a page redirect. I would like to do this without use of an anchor or javascript if possible. My datalist:
<asp:DataList ID="DataList1" ..etc >
<HeaderTemplate>
..etc
</HeaderTemplate>
<ItemTemplate>
<table >
<tr >
<td>..etc</td>
<td>..etc</td>
<td ><div id="clr_div" runat = "server"></div></td> <-- this is the div to be clicked
</tr>
</table>
</ItemTemplate>
And then in my code behind
protected void on_item_databound(object sender, DataListItemEventArgs e)
{
System.Web.UI.HtmlControls.HtmlGenericControl div = (System.Web.UI.HtmlControls.HtmlGenericControl)e.Item.FindControl("clr_div");
div.Attributes.Add("onclick", "**What goes here??**");
}
I have tried to put in the 'what goes here' section:
window.location.href = \"default.aspx\" **doesn`t work, the quotes don`t render properly in html
window.location.href = default.aspx **doesn`t work
return Response.Redirect(default.aspx); **doesn`t work
Any help is welcome. Thanks
The answers listed should help you out once you fix your div issues. I have experienced event firing issues in non-fixed divs when using static positioning, so you might want to try specifying position:relative on your containing div in question if you have fixed divs elsewhere.
Difference between static and relative positioning
Also, TMTOWTDI. If you have an empty div, you could just also turn it into a button for an insignificant markup cost
as explained here
Just add an ItemCommand event to your datalist
<asp:DataList ID="DataList1" onitemcommand="on_item_Command" ..etc..
and change this
<div id="clr_div" runat = "server"></div>
to this
<asp:Button id="clr_div" CommandName="nav-to-page" runat="server" CommandArgument='<%# Eval("field to eval if this came from a datasource") %>' />
and in code behind
protected void on_item_Command(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "nav-to-page")
{
string s = (string)e.CommandArgument;
Response.Redirect("NewPage.aspx?" + s);
}
}
I have a grid control called RadGrid1 and with a breakpoint in RadGrid1_ItemDataBound, but when I run my aspx application, the breakpoint is not being triggered.
my code is:
<telerik:RadGrid ID="RadGrid1" runat="server" Width="980px" CssClass="GridDisplay"
AllowAutomaticDeletes="false" AllowAutomaticInserts="false" AllowAutomaticUpdates="true" AllowPaging="true"
AutoGenerateColumns="False" AutoGenerateDeleteColumn="false" AutoGenerateEditColumn="false" ItemStyle-Height="20px"
ClientSettings-ActiveRowIndex="true" EnableViewState = "false" OnDeleteCommand = "RadGrid1_OnDelete"
OnItemCreated = "RadGrid1_ItemCreated" OnItemDatabound = "RadGrid1_ItemDatabound" OnNeedDataSource = "RadGrid1_NeedDataSource">
<telerik:GridTemplateColumn DataField="Confirmed" HeaderText="Confirmed" UniqueName="Confirmed" Visible="true">
<ItemTemplate>
<asp:CheckBox ID="chkVerified" runat="server" AutoPostBack="true"
Checked='<%# bool.Parse(Eval("Verified").ToString()) %>'
Enabled='<%# !!Convert.ToBoolean(Convert.ToInt32(Eval("Verified").ToString())) %>'
ToolTip='<%# Eval("NoConfirmDesc").ToString() %>'
/>
</ItemTemplate>
</telerik:GridTemplateColumn>
aspx.cs
private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{ }
}
In aspx, the handler for Databound event is called RadGrid1_ItemDatabound
OnItemDatabound = "RadGrid1_ItemDatabound"
but in code behind, your method is spelled with capital B in databound
void RadGrid1_ItemDataBound
Make sure you place the breakpoint inside the right method.
Could you make sure AutoEventWireup="true" for the page?
<%# Page ... AutoEventWireup="true" %>
In addition, make sure that there are not spaces between them OnItemDatabound="RadGrid1_ItemDatabound"
One thing I notice in your code is you want to use OnNeedDataSource to bind Data if you use RadGrid.
OnNeedDataSource="RadGrid1_NeedDataSource"
maybe you forgot to register or are not correctly registering the OnItemDataBound Event
Or maybe you are not databinding the control?
RadGrid1.DataSource= mydatasource;
RadGrid1.DataBind();
As last possibility maybe iis express and visualstudio debugger are not working correclty
I suggest to kill iisexpress process and then rebuild the solution then try again
In asp.net I have a table in database containing questions,date of submission and answers.On page load only questions appear.now i want to use any link which on clicking show answers and date specified in table data, either in textbox or label and clicking again on that link answer disappear again like expand and shrink on click.
So what coding should i use for this in C#?
I believe you could handle the ItemCommand event of the Repeater.
Place a LinkButton control for the link that you want the user to click in the Repeater's item template. Set the CommandName property of this to something meaningful, like "ShowAnswers". Also, add a Label or TextBox control into the Repeater's item template, but set their Visible property to false within the aspx markup.
In the code-behind, within the ItemCommand event handler check if the value of e.CommandName equals your command ("ShowAnswers"). If so, then find the Label or TextBox controls for the answers and date within that Repeater item (accessed via e.Item). When you find them, set their Visible property to true.
Note: you could take a different approach using AJAX to provide a more seamless experience for the user, but this way is probably simpler to implement initially.
I think the implementation would look something like this. Disclaimer: I haven't tested this code.
Code-Behind:
void Repeater_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "ShowAnswers")
{
Control control;
control = e.Item.FindControl("Answers");
if (control != null)
control.Visible = true;
control = e.Item.FindControl("Date");
if (control != null)
control.Visible = true;
}
}
ASPX Markup:
<asp:Repeater id="Repeater" runat="server" OnItemCommand="Repeater_ItemCommand">
<ItemTemplate>
<asp:LinkButton id="ShowAnswers" runat="server" CommandName="ShowAnswers" />
<asp:Label id="Answers" runat="server" Text='<%# Eval("Answers") %>' Visible="false" />
<asp:Label id="Date" runat="server" Text='<%# Eval("Date") %>' Visible="false" />
</ItemTemplate>
</asp:Repeater>
I have an ImageButton within a GridView in .aspx on clicking this ImageButton i have to call a function.
This is how i tried and the function was not being called.
Code inside.aspx page:
<GridView ......>
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}") %>'>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton"
width='24' height='24'
ImageUrl="~/images/delete.jpeg"
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');" />
<!--<img src="images/delete.jpeg" alt='edit' border="0" width='24' height='24'/> -->
</asp:HyperLink>
</GridView>
code in .aspx.cs page:
public void DeleteUrlImageButton_Click(object sender, EventArgs e)
{
//code to perform the necessary action.
}
Because you are wrapping your ImageButton inside of a Hyperlink, the browser is probably going to the hyperlink's URL instead of posting back to hit the OnClick function. You should have the DeleteUrlImageButton_Click function call Server.Transfer or Response.Redirect to the appropriate URL and get rid of the Hyperlink.
Sure it won't be fired because it is nested in a Hyperlink. So the imagebutton serves as the text for the hperlink and hyperlink does not cause postback. ImageButton can cause the desired action only if it stands out of the Hyperlink. Try this:
<asp:GridView ....
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton"
width='24' height='24'
ImageUrl="~/images/delete.jpeg"
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');"
PostBackUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}")
%>'/>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
ImageButton can do the job no need for Hyperlink just use the postbackurl and it will redirect you to the page. You can omit the HyperLink.
Button controls (like LinkButton,ImageButton and Button) are designed to cause postback by default.
Edit: Make sure event name and arguments are correct. This is the event I used to test it. y the way don't forget to place the ImageButton in a TemplateField, refer the code above
protected void DeleteUrlImageButton_Click(object sender, ImageClickEventArgs e)
{
TextBox5.Text = "Fired ";
//Response.Redirect( ((ImageButton)sender).PostBackUrl);//uncomment this if the button does not automatically redirects. This line should be the last one
}