I'm trying to make this code work:
<asp:CheckBox ID="statusChk" runat="server" Visible="true" Enabled="false" Checked='<%# status("de_cancel") %>'></asp:CheckBox>
What i'm trying to do is to retrieve the answer from the status function (which returns bool when i give a string) that i created in the c# source.
Doesn't give me compile error, but doesn't work. Edit: And btw, this is inside in a GridView
This works:
<asp:Label ID="lblInfo" runat="server" Visible="true" Text='<%# Bind("de_cancel") %>'></asp:Label>
But this is NOT what I'm looking for.
Sorry about my bad English.
I'm assuming that this is in a grid view, or some other repeater.
In that case, try this:
<asp:Label ID="lblInfo" runat="server" Visible="true" Text='<%# status(Eval("de_cancel")) %>'></asp:Label>
this is essentially calling your status() method and passing in the value of de_cancel
You may have to convert de_cancel inside your status method though as Eval returns an object.
Just do this in the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
//Get your string variable and use it as input here
statusChk.Checked = status("de_cancel");
}
protected bool status(string strStatus)
{
if (strStatus == "de_cancel")
{
return true;
}
else
{
return false;
}
}
This will occur when the page loads. There are other event handlers you may want to use - see http://msdn.microsoft.com/en-us/library/6w2tb12s(v=vs.90).aspx
Here is an example of how an event handler (OnSelectedIndexChanged) can be used to set a checkbox when a drop-down value is changed. As the accepted answer points out, you will need to set the AutoPostBack="true" option on the control if you want the event handler method to fire (and the page to refresh) when a control is changed:
Getting a dropdownlist to check a checkbox in asp.net/C#
Also, just looking at your code it looks like you might be using databinding, e.g. with a Repeater or ListView. In this case, please the examples for the ItemDataBound event handler below:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound(v=vs.110).aspx
http://www.codeguru.com/csharp/.net/net_asp/tutorials/article.php/c12065/ASPNET-Tip-Use-the-ItemDataBound-Event-of-a-Repeater.htm
Thanks to Darren tip, i was able to do it. Here what i did to make it work:
<asp:CheckBox ID="statusChk" runat="server" Visible="true" Enabled="false" Checked='<%#status(Convert.ToString(Eval("de_cancel")))%>'></asp:CheckBox>
Thanks everyone. ;)
Related
I have this code
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Command1">
<img src="../images/image1.png" alt="" />
</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
and this code
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Command1")
{
// Never drops here
}
}
The event is being triggered.
But when I debug, LinkButton1's CommandName is not visible in source code.
So, the if statement doesn't work.
Any ideas ?
Edit :
I realised that I have an other error my page that belongs this situation.
Then I used GridView rather than DataList and used the GridView's RowCommand Event and fixed this.
The reason is because your attempting to obtain the CommandName from the DataList not the LinkButton. Your code would work if you did the following:
protected void btnSample_Click(object sender, EventArgs e)
{
// Instantiate:
var command = ((LinkButton)sender).CommandName;
// Do additional logic.
}
I believe your intent is to actually do something on the Click Event. If that isn't your case, you would need to FindControl on an event within your DataList, then instantiate the LinkButton to obtain the CommandName. Hopefully this points you in the proper direction.
I would do a sample within the DataList, but without exact implementation and additional information I wouldn't be able to. It would be similar to the above, just allocating the Control within the current control.
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 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
I have a button as follows:
<asp:Button ID="btnNext" runat="server" Font-Bold="true" Text="Next"
onclick="btnNext_Click" style="text-align: center" Width="80px" />
and a RadNumericTextBox:
<telerik:RadNumericTextBox ID="txtTotal5" runat="server" Width="50px" AutoPostBack="true"
MinValue="0" ontextchanged="txtTotal5_TextChanged"><NumberFormat DecimalDigits="0" /></telerik:RadNumericTextBox>
I need to set the onClientClick property based on whether a telerik RadNumericTextBox has text in it or not. If it does not have a value, the onClientClick property needs to be set as shown below. If there is a value in the box, I want to just go on to the onclick event which directs it to the next form.
protected void txtTotal5_TextChanged(object sender, EventArgs e)
{
if (txtTotal5.Value.ToString() == "")
{
btnNext.OnClientClick = "javascript: return confirm('Please have the employee complete this form.')";
}
else
{
btnNext.OnClientClick = "";
}
}
Now I have used the debugger several times to step through the code, and the value changes as I expect within the function, but even when it sets the OnClientClick property to "", the box still pops up when the button is clicked. Is the value not being passed to the client somehow? Any suggestions would be appreciated. Thanks in advance!
You need to add the OnClientClick to the Attributes collection of the button, like this:
btnNext.Attributes.Add("OnClientClick", "YourJavaScriptFunction();");
I have found a solution for my issue. I left the function above as is, but I basically copied the if-else into my page_load event inside if(IsPostBack).
I have a grid view with some check boxes. So after the grid view get updated, I am trying to see whether one particular check box is checked or not. However, I am getting an error says
Null Reference Exception was unhanded by user code
My code:
<asp:TemplateField HeaderText="FollowUp" SortExpression="FollowUp">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Bind("FollowUp") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkFollowup" runat="server"
Checked='<%# Bind("FollowUp") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
Code-behind file:
protected void GViewSrvcCheck_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
foreach (GridViewRow gRow in GViewSrvcCheck.Rows)
{
CheckBox fllwup = gRow.FindControl("chkFollowup") as CheckBox;
if (fllwup.Checked)//this is the one causes the error
{
}
}
}
What goes wrong here? and how can I over come this problem?
There are two possible problems:
The control couldn't be found
It wasn't a CheckBox
If you'd used a cast instead, you'd know which it was:
CheckBox followUp = (CheckBox) gRow.FindControl("chkFollowup");
It's almost always wrong to use as without a check for nullity afterwards.
I suspect the problem is that the ID actually has something to identify the row within it as well... but with the above change you'd at least be able to tell which error path you were taking.
You'll probably have to change how you find the control - but so long as "not finding the control" is an error, I think it's reasonable to leave it throwing an exception. If the control not being present is a legitimate situation, you should explicitly handle it - but otherwise, showing an error page to the user and logging the exception (e.g. with ELMAH) is fine.