In a WebPart using Sharepoint 2010, I´ve got a problem when creating dynamically a LinkButton because his event isn´t fired.
My code is:
Default.aspx
<asp:TextBox ID="formAccountCode" runat="server" MaxLength="8" Columns="8"></asp:TextBox>
<asp:Table ID="idTabela" runat="server" Width="100%" BorderWidth="1px" GridLines="Both"></asp:Table>
<asp:Button ID="btPesquisar" runat="server" onclick="btPesquisar_Click" Text="Pesquisar" />
Default.aspx.cs
protected void btPesquisar_Click(object sender, EventArgs e)
{
LinkButton lkButton = new LinkButton();
lkButton.Text = "Teste Tabela";
lkButton.ID = "link1";
lkButton.Attributes.Add("runat", "server");
lkButton.CommandArgument = "Codigo 1";
lkButton.Command += test;
TableRow tr;
TableCell td1;
td1 = new TableCell();
td1.Controls.Add(lkButton);
tr = new TableRow();
tr.Cells.Add(td1);
idTabela.Rows.Add(tr);
idTabela.DataBind();
}
protected void test(object sender, EventArgs e)
{
formAccountCode.Text = "HI"; // just for test
}
The idea is return a select from DB and create a result with linkbutton to each record returned.
These LinkButtons will be created after an action from the user, because this, it can´t be created in OnInit.
They will connect with another web part.
please move the code for dynamically created button and event handler in CreateChildControls() and call the EnsureChildControls() in Onload
For example:
protected override void CreateChildControls()
{
base.CreateChildControls();
// Create our drop down list, but don't populate it yet
_dropDownList = new DropDownList();
this.Controls.Add(_dropDownList);
}
Related
I have a carouselview binded to a viewmodel, on the previous page (call it first page) user can select 2 arguments and with the help of those, the next view (call it second page) is generated accordingly. However, I can't wrap my head around why my view won't load asynchronously.
So my problem: When I click the button on the first page the UI would freeze for like a solid 2-3 seconds, and then start load (asynchronously?) and once it's done it's all good.
Also I couldn't really figure out a better way to inherit values from first page to second so if someone has an idea please let me know.
Any help on how can I fix this I would really appreciate.
The viewmodel for second page
public class DataSelectionViewModel : BaseViewModel
{
public ObservableCollection<Items> FilteredData { get; set; }
public UserSelectionViewModel()
{
_dataStore = DependencyService.Get<IDataStore>();
LoadData= new AsyncAwaitBestPractices.MVVM.AsyncCommand(FilterData);
FilteredData = new ObservableRangeCollection<Items>();
}
public async Task FilterData()
{
FilteredData.Clear();
var filtereddata = await _dataStore.SearchData(Hard, Subject).ConfigureAwait(false);
foreach (var data in filtereddata)
{
FilteredData.Add(data);
}
}
}
The carouselview in second page
<ContentPage.BindingContext>
<db:DataSelectionViewModel/>
</ContentPage.BindingContext>
...
<!-- DataTemplate for carouselview has radiobuttons, label and button all in a grid -->
<CarouselView ItemsSource="{Binding FilteredData}">
Second Page c#
public partial class SecondPage : ContentPage
{
public Coll(bool hard, string subject)
{
InitializeComponent();
var vm = (BaseViewModel)BindingContext;
vm.Hard = hard;
vm.Subject = subject;
/* had to set "hard" and "subject" here again, otherwise data won't load */
}
protected override async void OnAppearing()
{
var vm = (DataSelectionViewModel)BindingContext;
base.OnAppearing();
await vm.LoadData.ExecuteAsync().ConfigureAwait(false);
}
}
The first page view containing the button
<Button x:Name="Start" Pressed="ButtonClick"/>
First page c# --> Here I also tried doing it with a command and a pressed at the same time, because I couldn't come up with a way to save variables to second page viewmodel, that's why I use pressed here
private async void ButtonClick(object sender, EventArgs e)
{
var vm = (BaseViewModel)BindingContext;
vm.Hard = HardButtonSelected == Hard;
vm.Subject = vm.Subject.ToLower();
await Navigation.PushAsync(new SecondPage(vm.Hard, vm.Subject));
}
I have tried not using the OnAppearing method to get my data, but then it wouldn't bind to the page and it would not show, if I were to previously fill the ObservableCollection with my data and then load the page although I would love to be able to do this because it would allow me to create a loading popup also.
I'm working on a little project where users get match with certain events (ex: Soccer, Tennis, etc). Now I implemented a button that takes the user to a discord chat. The issue I'm having trouble figuring out is, I want each event to have its own specific button link that takes the user to the specific discord chat within the server and I'm not sure how to do that can someone help me. This is what I have so far.
protected async void DiscordChat_Tapped(object sender, EventArgs e)
{
await Launcher.OpenAsync("https://discord.gg/nGHTS56F");
}
Thanks in advance.
place the Discord ID in the ClassId property
<Button ClassId="SomeID" Clicked="DiscordChat_Tapped" ... />
<Button ClassId="SomeOtherID" Clicked="DiscordChat_Tapped" ... />
then in your handler
protected async void DiscordChat_Tapped(object sender, EventArgs e)
{
var btn = (Button)sender;
var id = btn.ClassId;
await Launcher.OpenAsync($"https://discord.gg/{id}");
}
alternately, if your Button is inside a template
protected async void DiscordChat_Tapped(object sender, EventArgs e)
{
var btn = (Button)sender;
// cast the BindingContext to the correct type
var item = (MyClass)btn.BindingContext;
// then use some property from that type
await Launcher.OpenAsync($"https://discord.gg/{item.SomeProperty}");
}
I need to put button that after click moves user to website. Can someone tell me what should I put into vb and in xaml?
<HyperlinkButton Content="Click here to learn about Silverlight" NavigateUri="http://www.silverlight.net" TargetName="_blank" />
You can do it also in C#
private void HyperlinkButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
var task = new Microsoft.Phone.Tasks.WebBrowserTask
{
URL = "http://siteaddress.com",
};
task.Show();
}
I have a simple class inheriting RadGrid. I am adding a button to the RadGrid and a Click Event handler to that button. The button is correctly added in the required position and the click event handler is firing, but radGrid.ExportToExcel() is not doing anything. In fact, upon click and when page posts back, the button disappears. Why is this happening?
I tried to add the button control to the Page.Form control collection, but still nothing happens.
[ToolboxData("<{0}:RadGridDp runat=server></{0}:RadGridDp>")]
public class RadGridDP : RadGrid
{
public RadGridDP()
{
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Button btnExport = new Button();
btnExport.ID = "Export";
btnExport.Text = "Export";
btnExport.Click += new EventHandler(btnExport_Click);
btnExport.CommandArgument = this.ID;
this.MasterTableView.Controls.Add(btnExport);
}
void btnExport_Click(object sender, EventArgs e)
{
Button btnExport = (Button)sender;
string RadGridId = btnExport.CommandArgument.ToString();
RadGridDP radGrid = (RadGridDP)this.Parent.Parent.FindControl(RadGridId);
radGrid.ExportSettings.IgnorePaging = true;
radGrid.ExportSettings.OpenInNewWindow = true;
radGrid.ExportSettings.ExportOnlyData = true;
radGrid.MasterTableView.ExportToExcel();
}
}
When I do same thing in a UserControl and use that UserControl on any page, it works fine. What's the difference?
I found out the solution. Whenever RadGrid Loads, it calls various events in this fashion:
1. Page OnLoad
m. RadGrid OnLoad
x. NeedDataSource
and upon click of the button (added in the manner above), events are called in this fashion
1. Page_OnLoad
m. RadGrid OnLoad
n. btnExport_Click
x. NeedDataSource
(as for strange serial numbers, these events may have other events in between, but the order of occurance is correct)
so, entire Grid is rebound with the data, and hence command to exportPdf is flushed. So nothing happens.
Interestingly, there's no need of adding one extra button, telerik provides its own buttons to do so. which can be customized as well(by implementing ITemplate). This is how am generating Reports now(though not specific to the original question):
[ToolboxData("<{0}:RadGridDP runat=server></{0}:RadGridDP>")]
public class RadGridDP : RadGrid
{
//custom logic
public RadGridDP()
{
this.ItemCreated += new GridItemEventHandler(RadGrid_ItemCreated);
this.Load += new EventHandler(RadGridDP_Load);
this.ItemCommand += new GridCommandEventHandler(RadGrid_ItemCommand);
this.PdfExporting += new OnGridPdfExportingEventHandler(RadGridDP_PdfExporting);
this.GridExporting += new OnGridExportingEventHandler(RadGridDP_GridExporting);
this.ExportSettings.ExportOnlyData = true;
this.ExportSettings.IgnorePaging = true;
// this.ExportSettings.OpenInNewWindow = true;
DoPdfFormatting();
DoExcelFormatting();
}
protected void RadGridDP_PdfExporting(object sender, GridPdfExportingArgs e)
{
e.RawHTML = e.RawHTML.Replace("border=\"1\"", "").Replace("style=\"", "style=\" border:0.5px Solid black; ")
.Replace("<thead>", String.Format("<thead>{0}", TableHeader)).Replace("</tbody>", String.Format("{0}</tbody>", TableFooter));
}
protected void RadGridDP_GridExporting(object sender, GridExportingArgs e)
{
e.ExportOutput = e.ExportOutput.Replace("<thead>", String.Format("<thead>{0}", TableHeader))
.Replace("</tbody>", String.Format("{0}</tbody>", TableFooter));
}
}
so basically i had to handle PdfExporting(for Pdf) and GridExporting(for excel)..
I had to handle Load, ItemCommand and ItemCreated as well. While the former one was required for some conditional logic, later two were required for formatting of the PDF document
i try to Update a simple Label on Close Event of dynamic created RadDock.
Works fine so far, Label gets the correct values but doesnt updates it.
RadDock dock = new RadDock();
dock.DockMode = DockMode.Docked;
dock.UniqueName = Guid.NewGuid().ToString();
dock.ID = string.Format("RadDock{0}", dock.UniqueName);
dock.Title = slide.slideName;
dock.Text = string.Format("Added at {0}", DateTime.Now);
dock.Width = Unit.Pixel(300);
dock.AutoPostBack = true;
dock.CommandsAutoPostBack = true;
dock.Command += new DockCommandEventHandler(dock_Command);
...
void dock_Command(object sender, DockCommandEventArgs e)
{
Status.Text = "Removed " + ((RadDock)sender).Title + " " + ((RadDock)sender).Text;
}
I tried to do this:
RadAjaxManager1.AjaxSettings.AddAjaxSetting(dock, Status, null);
while creating the docks, but on runtime i get a NullReference Excepetion.
On a Button registered with the RadAjaxManager it works to show the value
assigned by dock_command.
protected void Button1_Click(object sender, EventArgs e)
{
Status.Text = Status.Text;
}
UPDATE: The RadAjaxManager was created with integrated Wizzard of VS2008.
Can't select the Docks, because the are generated while runtime.
On Backend its included in AutoCompletion, so the NullReference has nothing to do
with the AjaxManager itself. Like i said, works fine with the Button.
<telerik:RadAjaxManager ID="RadAjaxManager1">
<telerik:AjaxSetting AjaxControlID="Button1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="Label1"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
Questions was solved by Telerik Support here
http://www.telerik.com/community/forums/aspnet-ajax/docking/telerik-asp-net-ajax-ajax-update-label-with-dynamically-created-docks.aspx