Devxpress ASPxScheduler access control using FindControl - c#

I have a hyperlink in ASPxScheduler VerticalAppointmentTemplate. I want to access the same in runtime code behind.
My Aspx code is as below:
<dxwschs:ASPxScheduler ID="CalendarView" runat="server" Width="100%" ActiveViewType="Day">
<ResourceNavigator EnableIncreaseDecrease="false" />
<Views>
<DayView ResourcesPerPage="1">
<WorkTime Start="08:00:00" End="17:00:00" />
<Templates>
<VerticalAppointmentTemplate>
<asp:HyperLink ID="CalendarViewLink" runat="server" Text="View"></asp:HyperLink>
</VerticalAppointmentTemplate>
</Templates>
</DayView>
</Views>
<OptionsBehavior ShowViewSelector="false" />
<Storage EnableReminders="false" />
</dxwschs:ASPxScheduler>
I have tried using various ways like
ASPxHyperLink calendarViewLink = (ASPxHyperLink)CalendarView.FindControl("CalendarViewLink");//Method 1
ASPxHyperLink calendarViewLink = (ASPxHyperLink)Page.FindControl("CalendarViewLink");//Method 2
ASPxHyperLink calendarViewLink = (ASPxHyperLink)updatepanelid1.FindControl("CalendarViewLink");//Method 3
also tried using rendered id of same link.
ASPxHyperLink calendarViewLink = (ASPxHyperLink)CalendarView.FindControl("ctl00_ContentPlaceHolder1_ContentControl_CalendarView_aptsBlock_AptTemplateContainer700_CalendarViewLink");
Please help me out for find the control. I have to bind the NavigateUrl at runtime.

Are you trying to bind the link during the DataBind event of the ASPxScheduler?
I'm sure there must be a way to get a reference to your link using FindControl method however you will have traverse the whole hierarchy of controls (ASPxScheduler->Views->DayView) to get to the parent control containing your actual link (I guess it has to be the DayView reference against which you would call the FindControl).
However, there is another way to achieve what you need which I suggest you to investigate. Just define the OnInit handler for the actual link itself:
<asp:HyperLink ID="CalendarViewLink" runat="server" OnInit="OnViewLinkInit" Text="View" />
then in codebehind you can set the NavigateUrl for your link using s parameter:
protected void OnViewLinkInit(object sender, EventArgs e)
{
HyperLink link = (HyperLink)sender;
link.NavigateUrl = "url";
}
The above OnInit handler will be called as many times as many links will be displayed.

Related

Creating and showing a Telerik RadNotification programmatically

I'm trying to have a RadNotification pop up in an event handler in my C# code. I tried something similar to this:
The markup,
<asp:button> ... OnClick="OnClick"</asp:button>
and the CodeBehind,
protected void OnClick(...) {
new RadNotification().Show("Some text");
}
But that doesn't work. The Telerik documentation doesn't seem to have much info on creating these notifications in a CodeBehind.
Thanks
This works for me...
Markup
<telerik:RadNotification ID="RadNotification1" runat="server" VisibleOnPageLoad="False" Width="300px" Height="100px" EnableRoundedCorners="true" EnableShadow="True" Title="My Notification" Position="Center" TitleIcon="none" />
<telerik:RadButton runat="server" ID="MyBtn" Text="Show Notification" OnClick="MyBtn_Click" />
Code Behind
protected void MyBtn_Click(object sender, EventArgs e)
{
RadNotification1.Show("Some text");
}
Modify the RadNotification attributes as necessary.
Cheers
When creating controls dynamically in ASP.NET WebForms, you must:
add them to the form (or a container in it), which you do not. It is also adviseable that you provide them with IDs
re-create them upon subsequent postbacks if they are going to be needed
This is not something specific to the Telerik controls, but it also applies to any control. Try adding a textbos with a preset text like this to see the problem.
Thus, Scotty's answer where the notification is simply declared in the markup is perhaps the most straightforward solution.

Populate DropDownList inside Grid Template Column

I have a dropdownlist inside a template column on a obout grid. Currently i am populating the dropdownlist on page load with a sqldatasource. However, i now have to load the dropdownlist dependent on the value of a certain column. For example: If status = 1, i populate the dropdownlist with a list of available options that pertain to status 1.
<obout:Column ID="colStatus" DataField="wf_status_id" Align="center" HeaderText="Status" HeaderAlign="center" Width="130px" Wrap="true" runat="server" AllowGroupBy="true" AllowFilter="true">
<TemplateSettings EditTemplateId="tmpStatusIDEdit" TemplateId="tmpStatusID" />
</obout:Column>
<obout:GridTemplate runat="server" ID="tmpStatusID" >
<Template>
<%# Container.DataItem["Status"]%>
</Template>
</obout:GridTemplate>
<obout:GridTemplate runat="server" ID="tmpStatusIDEdit" ControlID="ddlStatus" ControlPropertyName="value">
<Template>
<obout:OboutDropDownList runat="server" ID="ddlStatus" Width="100%" Height="200" MenuWidth="215" DataSourceID="sdsStatus" DataTextField="wf_status_text" DataValueField="wf_status_id" />
</Template>
</obout:GridTemplate>
public void OnGridRowDataBound(object sender, Obout.Grid.GridRowEventArgs e)
{
if (e.Row.RowType == Obout.Grid.GridRowType.DataRow)
{
DropDownList ddlStatus = new DropDownList();
ddlStatus = (DropDownList)e.Row.FindControl("ddlStatus");
//LOAD DROP DOWN HERE//
}
}
When i attempt to execute this code, it shows that ddlStatus is null everytime. I have a tried a multitude of ways to get this and for some reason cannot seem to get it. Perhaps aother set of eyes or other ideas could help me out. Please let me know what it is i am doing wrong. Thank you ahead of time
UPDATE:DATABOUND EVENT CODE
<obout:OboutDropDownList runat="server" ID="ddlStatus" Width="100%" Height="200" MenuWidth="215" OnDataBinding="ddlStatus_DataBinding" DataSourceID="sdsStatus" DataTextField="wf_status_text" DataValueField="wf_status_id" />
protected void ddlStatus_DataBinding(object sender, EventArgs e)
{
Obout.Interface.OboutDropDownList ddl = (Obout.Interface.OboutDropDownList)(sender);
string statusID = Eval("wf_status_id").ToString();
}
I added the DataSource because i do not see another way to have the databinding event triggered.
I don't really know anything about obout controls but I have to assume they act very similar to the asp.net controls and have just been extended. With that assumption in mind, I will try and answer your question.
Your RowDataBound event has a few coding issues... for example, I am not sure why you are defining a new DropDownList and then trying to overwrite it with the next line. Also it sounds like the next line is returning null anyways.
First off I suggest not using the DataBound event at the row level. Use the control's DataBinding event as it will localize your code a lot better because you can trigger off the specific control's DataBinding and therefore not have to search for it. If your code changes (markup or codebehind) it is also a lot easier to change as it will not affect other things and there is less room for introducing bugs.
So I would make the following changes to address this:
Change your DropDownList definition to implement DataBinding:
<obout:OboutDropDownList runat="server" ID="ddlStatus" Width="100%" Height="200"
MenuWidth="215" OnDataBinding="ddlStatus_DataBinding" />
Then implement the OnDataBinding event (get rid of your DataBound event if you weren't using it for anything else):
protected void ddlStatus_DataBinding(object sender, System.EventArgs e)
{
// This will point to ddlStatus on the current row that is DataBinding so you
// don't have to search for it.
OboutDropDownList ddl = (OboutDropDownList)(sender);
// Now you can fill the DropDownList and set the default value how ever you like
...
}
You can also see this other question I answered a long time ago to see if it helps as it is doing the same thing but with a Repeater but it is pretty much the same thing as a grid:
Populating DropDownList inside Repeater not working
EDIT: Changed the code to use OboutDropDownList in the DataBinding.
you can find plenty of samples on obout knowledge base and examples. These should help you out: http://www.obout.com/combobox/aspnet_integration_grid.aspx, http://www.obout.com/grid/aspnet_ajax_cascading_comboboxes.aspx
(they refer to comboBox, but you can easily adapt them do a ddl)

List all asp.net controls that have a matching onclick attribute after one is clicked

I have a bunch of LinkButtons on an asp.net page and need to set the visibility property of all the other LinkButtons on the page that have the same onclick attribute. I'm looking for a server side solution.
In the click handler I've gotten as far as listing the LinkButtons on the Page recursively but am stumped at how to tell if each LinkButton I find does or doesn't have a matching click handler.
The EventHandler property doesn't seem to contain any good info...
What is the best way to approach this?
You can give your linkbuttons a custom atttribute (for simplicity it can be the same as event handler name) e.g.
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton_Click" tag="LinkButton_Click">LinkButton1</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton_Click" tag="LinkButton_Click">LinkButton2</asp:LinkButton>
Then in your server-side code you can simple compare the attributes
protected void LinkButton_Click(object sender, EventArgs e)
{
// your recursive code retrieving current linkbutton
// ...
if ((sender as LinkButton).Attributes["tag"] == currentLinkbutton.Attributes["tag"])
{
// do your magic
}
}
An ugly solution would be to give all of those buttons the same CommandName attribute. Then you can just search for all link buttons with that attribute. Otherwise I am not sure that there is a great way to track the event handler used by each LinkButton.
Did you try linkbtn.Attributes["OnClick"]

Adding a ASP Control to page from Code Behind within InnerHTML

I'm trying to add a button to the webpage, from the code behind. I have a single empty div on my main page that visible on and off, when needed. However the content I wish to create dynamically as the div content can change dependent on conditions.
I have realised that within my ASP Control I use a / (backslash) which cancels out my HTML. The problem I now have is understanding how I can get around this with code, is there a way to add ASP Controls to the web page? I am open to suggestions outside of the InnerHtml.
I'm creating my Button like so (in my Code Behind):
string buttonout = string.Format("<asp:Button ID=\"helpButton_0\" CommandArgument=\"0\" CssClass=\"HelpButton\" runat=\"server\" Text=\"?\"/>");
innercontent[0] = string.Format("<table><tr><td>Lead Passenger Information</td></tr><tr><td>Here by deafaul we used your detaisl from your profile, if you're not the lead passenger (As in you're booking for someone else) then please change details.</td></tr><tr><td>{0}</td></tr></table>"+ buttonout);
As Said above, The reason this doesn't work is because of InnerHtml hating backslashes, I think.
I do have a solution to this; and that's by adding more divs to the page.
<div id="HelpBoxDiv" runat="server" Visible="false">
<div id="HelpBoxDiv_Top" runat="server" Visible="true">
</div>
<div id="HelpBoxDiv_Bottom" runat="server" Visible="true">
<asp:Button ID="button_HelpBox_false" runat="server" />
</div>
</div>
I would then add my Innerhtml to the _Top Div, instead of the HelpBoxDiv which I am currently doing now. However this solution doesn't teach me anything.
I am hesitant to ask questions here, as I know a lot of question have been asked and I am sure this one has before, but I didn't find a solution. Any help is much appreciated.
Thank you.
I have realised that within my ASP Control I use a / (backslash) which
cancels out my HTML. The problem I now have is understanding how I can
get around this with code, is there a way to add ASP Controls to the
web page? I am open to suggestions outside of the InnerHtml.
You cannot add ASP.Net server control like a literal string.
Instead, you want to add the dynamic server control to the following approach -
ASPX
<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
Code Behind
protected void Page_Init(object sender, EventArgs e)
{
var button = new Button
{
ID = "helpButton_0",
CommandArgument = "0",
CssClass = "HelpButton",
Text = "?"
};
button.Command += button_Command;
PlaceHolder1.Controls.Add(button);
}
private void button_Command(object sender, CommandEventArgs e)
{
// Handle dynamic button's command event.
}

FindControl doesn't work with bind server tag, why?

I have a FindControl in OnLoad event to find my button on page i.e.:
protected override void OnLoad(EventArgs e)
{
DataBind();
control button = Page.FindControl("myButton");
}
on my aspx page I have
<asp:Button runat="server" ID="myButton" />
If I only have this, everything works, when I pass in the OnLoad, button is not null and I can execute what I want. The problem is when I add dynamic text in my aspx:
<asp:Button runat="server" ID="myButton" Text='<%# "Here is my dynamic text pulled from a XML" %>' />
Then the FindControl finds nothing and the button is null.
Adding a binding server tag on aspx isn't suppose to delay anything right? When I inspect the Page object I have some controls in Controls collection, but I'm unable to find myButton.
Any idea on what I am doing wrong?
EDIT
People seem to think that my code example is my real code but it isn't, So I use FindControl because I need to since I have nested controls and I cannot access it directly and I use binding because the dynamic text I'm putting is inside a ContentTemplate which I can override in other page aspx.
The question I asked was more specific to the fact that I have traced the problem where my FindControl returns null because a newly implement behaviour which is the binding.
Improving the code example isn't a solution or an explanation to the fact that if I put a <%# %> tag in my aspx page, the FindControl in the OnLoad event return null.
EDIT 2
the bind tag alone seems to not be the culprit but the DataBind() to populate them. Whether or not I have bind tag, putting DataBind() before the FindControl makes the myButton to be null. Correction in code example was made.
In here MSDN says that :
PreRender : Each data bound control whose
DataSourceID property is set calls its
DataBind method.
It looks like you're not using DataSourceID of your data bound control, but moving your FindControl code to PreRender event might help.
The Page.FindControl() method will only search the imediate collection of controls that are associated with Page. It will not recurse down the entire control tree, so if your button is contained within another control it will not be found. You would need to call the FindControl method on the containing control.
If you want to access a button on your page, you can directly refer to the button as -
this.myButton
And as far as assigning values is concerned, you can do it like this in your server code -
this.myButton.Text = "Dynamic Text";
<%# xyz %> is only used when you are databinding the controls, for e.g. in a DataGrid, GridView, etc.
In your override dont you want to call base.OnLoad(e) in your method first ?

Categories

Resources