control not found message is displayed - c#

Error Message displayed as "AddFavoriteRadWindow not found"
My code:
protected void btnAddReport_Click(object sender, ImageClickEventArgs e)
{
this.form1.Controls.Add(AddFavoriteRadWindow); // working fine
}
protected void btnOk_Click(object sender, EventArgs e)
{
if (txtReportFavorite.Text != string.Empty)
{
// code for inserting into db..
AddFavoriteRadWindow.Visible = false; // not working
}
}
"AddFavoriteRadWindow not found" message is displayed when I want to hide the rad window

You need to get the instance of your added controls from your Control Collection. Try
(this.form1.FindControl(AddFavoriteRadWindow.ID) as RadWindow).Visible = false;
You may put a check in place against null. Something like.
if((this.form1.FindControl(AddFavoriteRadWindow.ID) as RadWindow) != null)
(I am not sure about your class name, I have used RadWindow but you can replace that with your class name)
EDIT: You should pass the string id of the control in your FindControl method to get that specific control back

Related

Is it possible to tell if the Text change on an entry was from code or from the UI?

I am working on a Xamarin project and I need to be able to tell if the changes that occur to the text in an Entry view are from the code or from the UI, is this possible in Xamarin? or is there a known work around to do this.
I know about the OnTextChanged event but this only tells you that the Text property has changed, and gives you access to the old and new value of the Text property. It does not differentiate between different causes of text change.
You can get some idea from this thread, check if the entry is focused to differentiate between different causes of text change:
public MainPage()
{
InitializeComponent();
myEntry.TextChanged += MyEntry_TextChanged;
}
private void MyEntry_TextChanged(object sender, TextChangedEventArgs e)
{
var entry = sender as Entry;
if (entry.IsFocused)
{
//change from UI
Console.WriteLine("change from UI");
}
else{
//change from code
Console.WriteLine("change from code");
}
}
Update: The better way to solve op's problem:
You can set a flag yourself that tells your code to ignore the event. For example:
private bool ignoreTextChanged;
private void textNazwa_TextCanged(object sender, EventArgs e)
{
if (ignoreTextChanged) return;
}
Create a method and use this to set the text instead of just calling Text = "...";::
private void SetTextBoxText(TextBox box, string text)
{
ignoreTextChanged = true;
box.Text = text;
ignoreTextChanged = false;
}
Refer: ignoreTextChanged
you can use EntryRenderer to detect keypress event and use that flag to detect the change by code or by UI.
Here are the step:
- Exetend your entry control with new event OnTextChangeByUI
- Write custom render for both platform
e.g for android it will be something like this
public class ExtendedEntryRender : EntryRenderer
{
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Control != null)
{
Control.KeyPress += ((Entry)Element).OnTextChangeByUI;
}
}
}

How to used the e.SelectedControl from the Devexpress Tooltip event

In the Devexpress ToolTipControllerGetActiveObjectInfoEventArgs Event there is a parameter passed to the function.
There is a SelectedControl member variable which points to the DevEx grid control object.
From here I want can get the active GridView (this is because I have several grids coming in here).
Can someone give me some sample code to get from the SelectedControl to the GridView?
private void MyToolTipController_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)
{
If you want to get the view under the mouse point then you can use GridControl.GetViewAt method.
Here is example:
private void MyToolTipController_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)
{
var gridControl = e.SelectedControl as GridControl;
if (gridControl != null)
{
var view = gridControl.GetViewAt(e.ControlMousePosition);
//Your code here.
}
}
Also, if you want to get the focused view then you can use GridControl.FocusedView property.
Here is example:
private void MyToolTipController_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)
{
var gridControl = e.SelectedControl as GridControl;
if (gridControl != null)
{
var view = gridControl.FocusedView;
//Your code here.
}
}

ComboBox.SelectedItem not working as expected C# asp.net

I have a combo box I added items to in the page load method and within a button click I am trying to set either the text that is typed or the item selected from the dropdown. I am using ComboBox.SelectedItem.ToString() which works when something is typed in but once the user selects something from dropdown the string is empty that is passed through.
protected void Page_Load(object sender, EventArgs e)
{
if (ScriptManager.GetCurrent(Page) == null)
{
Page.Form.Controls.AddAt(0, new ScriptManager());
}
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value1);
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value2);
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value3);
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value4);
DiffComboBoxUrls.Items.Add(XMLConstants.PolicyStoreApplications.Value5);
}
protected void ChangeAppSettings_Click(object sender, EventArgs e)
{
string FOPath = FODirectory.Text;
string PolicyStoreName = DiffComboBoxUrls.SelectedItem.ToString(); //this comes back as "" when an item is selected and works when something is typed in
.....
}

Passing TextBox Text to Navigated Page

I am trying to pass data from a textbox in one page to a text block in the navigated page. I have some code but I am finding an error when running it here is my coding.
From the page I want to send the data from:
private void button1_Click(object sender, RoutedEventArgs e)
{if (txtSID.Text != null)
{
string StudentID = txtSID.Text;
var url = string.Format("/BookingConf.xaml?StudentID={0}", StudentID);
NavigationService.Navigate(new Uri(url, UriKind.Relative));
}
Code from the Navigated Page:
protected override void OnNavigatedTo(NavigatingEventArgs e)
{
String StudentID;
if (NavigationContext.QueryString.TryGetValue
("studentID", out StudentID))
{// load event data, and set data context
ReferanceST.Text = StudentID;
}
}
The issue is that when I run the application I get an error on the 'OnNavigationTo(NavigationEventArgs e)' saying no suitable method found to override.
In order to fulfil this i placed the 'if' statement but it made no difference.
Please help me resolve this issue. Thank you.
The OnNavigatingTo takes the NavigationEventArgs, not the NavigatingEventArgs.
Change your line to:
protected override void OnNavigatedTo(NavigationEventArgs e)
The error is happening because you miss named the override method.
The error is the smoking gun
"no suitable method found to override"
To fix this
protected override void OnNavigationTo(NavigatingEventArgs e)
{
should be
protected override void OnNavigatedTo(NavigatingEventArgs e)
{
MSDN Reference

Intercept button click on page load

I need to write a code that can intercept a click of some button (asp button) than execute some code, and if a method return true, call the original click.
So the points are:
1- I don´t know how to save the original click.
2- Identify the button that was clicked.
Ex:
protected void Page_Load(object sender, EventArgs e)
{
var b = getButtonThatWasClicked();
var originalClick = b.Click;
if(callSomeMethod(b))
originalClick(null,null);
}
EDIT:
Ok managed to get the button who made the click doing this...Now i need to prevent the original Click to get called. The method bellow didn't worked. Even overriding the original click to a new Handler the old Handler got called and executed. I thing ASP.NET read it and make something like a call stack of events to get called.Even if the handler change the old still in the stack.
public void ButtonsTestMethod()
{
var listOfButtons = listaDeBotes.Where(b => b.CommandName != "");
foreach (var button in listOfButtons)
{
if (Request.Form[button.UniqueID] != null)
{
var buttonFromRequest = Request.Form[button.UniqueID];
if (buttonFromRequest == null)
continue;
if (button.CommandName != "xxx")
{
//validate things
//if(TemPermissao(Tela,GetAcaoDoBotao(botao.CommandName)))
//if(!canexecuteSomething())
button.Click += new EventHandler(defaultClick);
}
}
}
}
void defaultClick(object sender, EventArgs e)
{
//show error
}
protected void Page_Load(object sender, EventArgs e)
{
//other code
ButtonsTestMethod();
}
I don´t know if its possible but would appreciate some help =/
Thanks.
To get the control name, you can try the following in the page load:
protected void Page_Load(object sender, EventArgs e)
{
if( IsPostBack )
{
string senderControl = Request.Params["__EVENTTARGET"].ToString();
//senderControl will contain the name of the button/control responsible for PostBack
}
}
The first argument in the button click event handler is the sender. You can cast that sender as a Button object and then you should be able to identify which button that was based on that object. That way, you can eliminate having that function to figure out which is clicked.
void GreetingBtn_Click(Object sender, EventArgs e)
{
Button clickedButton = (Button)sender;
if(clickedButton.Text == "bla")
{
//Do stuff
}
}

Categories