How do you make a label clickable and call a c# method - c#

Is it possible to add an onClick event to an asp:label and let it call a c# method rather than js?
Something like:
<asp:Label
ID="lblTest"
runat="server"
Text=""
ToolTip="Amount of errors this person is processing"
Style="cursor: help;"
OnClick="lbl_Click"
/>
And on the server side:
protected void lbl_Click(object sender, EventArgs e)
{
lblTest.Text = "Clicked"
}

You can create linkedbutton and make it look like label using CSS like this:
<asp:LinkButton ID="LinkButton1"
runat="server"
CssClass="myclass"
OnClick="LinkButton1_Click">
MyButton
</asp:LinkButton>
and in CSS
a.myclass{ color: #000000; text-decoration: none; }
a.myclass:hover { text-decoration: none; }
and then call it like
public void LinkButton1_Click()
{
lblTest.Text = "Clicked"
}

Related

Webforms: How to disable a button that is inside a Telerik RadGrid in code behind

I am new working with webforms. I learned MVC first. I have a Telerik RadGrid that has a MasterTableView inside and then, a couple of columns inside this MasterTableView. I want to simply disable some buttons in the code behind but Visual Studio keeps telling me that the buttons does not exists. In a Google search I found that the reason is because the buttons are inside the RadGrid. However I didn't find any example to access them.
The buttons are inside the radgrid and they looks like this:
<telerik:GridTemplateColumn HeaderStyle-Width="72px" HeaderText="Acciones" >
<ItemTemplate >
<div style="width: 100px">
<span style="position:relative;" class="grid-buttonColor1">
<i class="material-icons">create</i>
<asp:Button ID="btnEditReportDetail"
CommandArgument='<%# Item.ReportDetailId %>'
OnClick="btnReportDetail_Click"
runat="server"
Style="position:absolute; opacity:0; top:0; left:0; width:100%; height:100%;"
type="button"
causesvalidation="false" />
</span>
<span style="position: relative;" class="grid-buttonColor2">
<button
type="button"
style="background-color: transparent; border: none; padding: 0"
data-toggle="modal"
data-target="#MessageBoxModal"
onclick="ShowMessageBoxWithMessage_<%= ucMessagebox.ClientID%>('Confirmación', '¿Está seguro que desea eliminar la tarea?','DeleteTaskReports','<%# Item.ReportDetailId.ToString() %>')">
<i class="material-icons prefix">delete</i>
</button>
</span>
</div>
</ItemTemplate>
</telerik:GridTemplateColumn>
How can I access those buttons in order to write in the code behind something like: buttonName.Enabled = false;
Please! This is driving me crazy!
Thank you guys!
May be this will help you
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
Button btn = item.FindControl("img1") as Button;
btn.Enabled = false;
}
}
or
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
if ("your Condition")
{
foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
{
((Button)cmdItem.FindControl("btnEditReportDetail")).Visible = false;
}
}
}
You need to use FindControl to find server control that inside of Grid.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Button button = e.Row.FindControl("Button1") as Button;
button.Enabled = false;
}

How does my site know what button I clicked?

'
I have a problem with my site in ASP.NET. My applications contains a few buttons (imagebuttons) that are able to change. For example: When I press an empty button I want to be able to put a picture and a website in it. I've made this possible with a popupbox.
The problem where I'm running into:
When Im trying to give button 2 a image and a website, it gives button 1 a image and website because I told it in:
public partial class Ingelogd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Image1_Click(object sender, ImageClickEventArgs e)
{
if (DropDown.SelectedItem.Value == "Youtube")
{
Btn_1.ImageUrl = "~/Images/Youtube.png";
Btn_1.PostBackUrl = ("http://www.youtube.com");
Btn_1.OnClientClick = "";
}
else
{
}
}
Now I actually want the same for Btn_2 , but then I have to write the exact same code but change Btn_1 to Btn_2. This is impossible to do because I want 19 website ( youtube but also facebook, twitter etc.) Im also going to have 19 buttons ( currently I only have Btn_1 and Btn_2). This would mean I have to make 19*19 = 361 pieces of code. I assume there is a way to make a sub program for this. My teacher also told me I could use cookies for the buttonclick, but I have no idea how to make a cookie with a Imagebutton.
What is the best solution to solve this problem?
I also have the ASP.NET Code here for you who wants to see that code aswell.
<title>Ingelogd</title>
<script type="text/javascript">
<!--
function toggle_visibility(id) {
var e = document.getElementById(id);
if (e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
//-->
</script>
<style type="text/css">
#popupBoxOnePosition{
top: 0; left: 0; position: fixed; width: 100%; height: 120%;
background-color: rgba(0,0,0,0.7); display: none;
}
.popupBoxWrapper{
width: 550px; margin: 50px auto; text-align: left;
}
.popupBoxContent{
background-color: #FFF; padding: 15px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
Dit is de pagina als je ingelogd bent.
</div>
<div id="popupBoxOnePosition">
<div class="popupBoxWrapper">
<div class="popupBoxContent">
<h3>Instellingen</h3>
<p>Kies uit een van de volgende links.</p>
<asp:DropDownList ID="DropDown" runat="server">
<asp:ListItem >Select</asp:ListItem>
<asp:ListItem >Youtube</asp:ListItem>
<asp:ListItem >Facebook</asp:ListItem>
</asp:DropDownList>
<br />
<asp:ImageButton ID="Button1" runat="server"
OnClick="Image1_Click"/>
<br />
<asp:Button ID="Button_afsluiten" runat="server" Height="48px" OnClientClick="toggle_visibility('popupBoxOnePosition');return false;" Text="Afsluiten" />
</div>
</div>
</div>
<asp:ImageButton ID="Btn_1" runat="server" Height="48px" OnClientClick="toggle_visibility('popupBoxOnePosition');return false;"
ImageUrl="" />
<asp:ImageButton ID="Btn_2" runat="server" Height="48px" OnClientClick="toggle_visibility('popupBoxOnePosition');return false;"
ImageUrl="" />
</form>
</body>
</html>
You can try to add attributes to DropDownList and to use it. For example add two new attributes ImageUrl and SiteUrl. Your code should look:
protected void Image1_Click(object sender, ImageClickEventArgs e)
{
Btn_1.ImageUrl = DropDown.Attrubutes["ImageUrl"];
Btn_1.PostBackUrl = DropDown.Attrubutes["SiteUrl"];
Btn_1.OnClientClick = "";
}
I've never used asp, but most of those code is just plain c#, so I should be good.
Make 19 separate functions, one for each button, and dynamically add the Image and Url instead of using ifs:
protected void Image1_Click(object sender, ImageClickEventArgs e)
{
String val = DropDown.SelectedItem.Value;
Btn_1.ImageUrl = "~/Images/" + val + ".png";
Btn_1.PostBackUrl = "http://www." + val + ".com";
Btn_1.OnClientClick = "";
}
If you want 1 function for all buttons as well, you'll have to give all your buttons a Name and check the senders name inside the function.

Adding style to asp.net label

I want to adding style to asp.net label, but it wont work.
ASP.NET Mark up
<asp:Label runat="server" ID="lblCommentText"/>
Generated from the backend: Html mark up
<span id="ctl02_ctl36_CommentText">Only the leave the comment please</span>
............................................
I want to add the following style to the label
{
float:right;
width:70%;
}
I've tried using
cssClass property
Add this lblCommentText.Attributes.CssStyle.Add("float", "right"); to backend
using javascript
document.getElementById('<%= lblCommentText.ClientID%>').Style.display = ("float","right");
and also in-line style to the element
none of them works, can someone help me out ?
Labels are rendered as spans and spans are basically inline elements. You need to make it block or inline-block in order to get the float and width have effect.
.yourclass {
display: inline-block;
float: right;
width: 70%;
}
And then simply use cssclass:
<asp:Label runat="server" ID="lblCommentText" CssClass="yourclass" />
Inline:
<asp:Label runat="server" ID="lblCommentText" style="float:right" />
Using class:
<style>
.styleclass{
float: left;
}
</style>
<asp:Label runat="server" ID="lblCommentText" CssClass="styleclass" />
Using ID;
<style>
#ctl02_ctl36_CommentText {
float: left;
}
</style>
<asp:Label runat="server" ID="lblCommentText" />
If you want to add from code behind then use like below:
lblCommentText .Attributes.CssStyle.Add("float", "right");
lblCommentText.Attributes.CssStyle.Add("width", "70%");
If you want to add from aspx page then create a css class like:
.testClass{float: right;width: 70%;}
and assign like this:
asp:Label runat="server" ID="lblCommentText" runat="server" Text="test data" CssClass="testClass"
asp:label is being converted to span. So, you can set
span { float:left }

Checkbox evenhandler for Checked Changed event dosen't trigger when checked state is modified

The CheckBox dosen't trigger oncheckedchanged event:
<asp:CheckBox ID="ccCritica" runat="server"
style="z-index: 1; right: 15px; top: 100px; position: absolute"
oncheckedchanged="ccCritica_CheckedChanged" />
.
protected void ccCritica_CheckedChanged(object sender, EventArgs e)
{
if(ccCritica.Checked == true)
{
ddSarcinaDep.Enabled = true;
}
}
I've tried with debug and it dosen't call the eventhandler.I'm using Visual Studio 2010 professional, and I'm Developing an ASP.NET Web application.Thanks
Set AutoPostBack="true" in aspx
Use the following code:
<asp:CheckBox ID="ccCritica" runat="server" style="z-index: 1; right: 15px; top: 100px; position: absolute" AutoPostBack="true"
oncheckedchanged="ccCritica_CheckedChanged" />
protected void ccCritica_CheckedChanged(object sender, EventArgs e)
{
if (ccCritica.Checked == true)
{
ccCritica.Enabled = true;
}
}

programmatic creation of a multicolumn radcombobox

Anyone have a link to a code sample or can you provide a snippet to accomplish this task. I'm coming up blank on the Telerik site
I had to put something together a while ago and found snippets of code scattered throughout Telerik's site and blogs. So, I don't want to take "credit" for creating the below code... but here's what I'm using:
Add this to your css
.rcbHeader ul, .rcbFooter ul, .rcbItem ul, .rcbHovered ul, .rcbDisabled ul
{
width: 100%;
display: inline-block;
margin: 0;
padding: 0;
list-style-type: none;
}
.col1, .col2, .col3
{
float: left;
width: 100px;
margin: 0;
padding: 0 0px 0 0;
line-height: 14px;
}
If you want an "item counter" add this javascript function
function UpdateItemCountField(sender, args) {
//set the footer text
sender.get_dropDownElement().lastChild.innerHTML = "A total of " + sender.get_items().get_count() + " items";
}
Here's the code for the RadComboBox
<telerik:RadComboBox runat="server" ID="rcbInvoiceNumber" Height="190px" Width="350px"
MarkFirstMatch="true" EnableLoadOnDemand="true" HighlightTemplatedItems="true"
OnClientItemsRequested="UpdateItemCountField" OnItemDataBound="rcbInvoiceNumber_ItemDataBound"
OnItemsRequested="rcbInvoiceNumber_ItemsRequested" EmptyMessage="Enter Invoice Number"
ChangeTextOnKeyBoardNavigation="true" ValidationGroup="QuickPay">
<HeaderTemplate>
<ul>
<li class="col1">Invoice Number</li>
<li class="col2">PO Number</li>
<li class="col3">Invoice Total</li>
</ul>
</HeaderTemplate>
<ItemTemplate>
<ul>
<li class="col1">
<%# DataBinder.Eval(Container.DataItem, "InvoiceNumber") %></li>
<li class="col2">
<%# DataBinder.Eval(Container.DataItem, "PONumber")%></li>
<li class="col3">
<%# DataBinder.Eval(Container.DataItem, "TotalInvoice", "{0:C}")%></li>
</ul>
</ItemTemplate>
<FooterTemplate>
A total of
<asp:Literal runat="server" ID="RadComboItemsCount" />
items
</FooterTemplate>
</telerik:RadComboBox>
Finally, the code behind
protected void rcbInvoiceNumber_DataBound(object sender, EventArgs e)
{
//set the initial footer label
((Literal)rcbInvoiceNumber.Footer.FindControl("RadComboItemsCount")).Text = Convert.ToString(rcbInvoiceNumber.Items.Count);
}
protected void rcbInvoiceNumber_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
var invs = new VInvoicesCasesTotalCollection()
.Load();
rcbInvoiceNumber.DataSource = invs.ToDataTable();
rcbInvoiceNumber.DataBind();
}
protected void rcbInvoiceNumber_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
{
//set the Text and Value property of every item
//here you can set any other properties like Enabled, ToolTip, Visible, etc.
e.Item.Text = ((DataRowView)e.Item.DataItem)["InvoiceNumber"].ToString();
e.Item.Value = ((DataRowView)e.Item.DataItem)["InvoiceID"].ToString();
}
It should be noted that I've added Telerik.Web.UI to my using statements.
Hopefully this at least helps steer you in the right direction.
I've seen the links on this page and went in search of the answer on their page, becasue I was sure I had seen it before.
The link to mentioned in the comment section of clk's post is indeed a demo, but if you want the basics and quite possibly the easiest documentation to look at it would be found here.
As you mention you are loading you data on the code behind, so what you could do is load the entities first, set the data source next, and then continue to follow the rest of my linked articles code in the page load.
Hope it helps even a little.

Categories

Resources