Dotnet, C#, VS2013
I have a method that retrieves the selected item in a combobox (CBSpecies). The method is called when a button is clicked. My problem is that no matter what I select, the item found by the method is always the first one (default set when I populate the combobox). I have a console application with exactly the same method and it works fine.
private void GetSelectedSpecies()
{
//EurostatSpeciesName = "Fish and Chips";
//EurostatSpeciesName = CBSpecies.SelectedIndex.ToString();
//return;
// CBSpecies.SelectedIndex = 3;
String MySpecies = CBSpecies.SelectedItem.ToString();
for (int i = 0; i < MaxSpecies; i++)
if (SpeciesArray[i].SpeciesName == MySpecies)
{
EurostatSpecies = SpeciesArray[i].SpeciesCode;
EurostatSpeciesName = SpeciesArray[i].SpeciesName;
break;
}
}
Added the following note: I think the problem is that I populate and initialize the combobox in the Page_Load method, so when the button does a postback, it resets everything since it reloads the page. This would not happen in the console version. I tried putting the whole setup (populating the species array, then populating the combobox from that, then setting the first combobox item as the default) by using: if (!Page.IsPostBack) as a condition, but then the app throws a null exception when the button is clicked.
Try .SelectedNode and see if that works.
Solved using a suggestion from this post:
https://www.telerik.com/forums/selectedvalue-lost-on-postback-in-dynamically-added-user-controls
The method referred in the question was moved into:
protected void Page_Init(object sender, EventArgs e)
{}
Page_Init is not created automatically, so must be manually added. Once the combobox checking method is put there, the selection is persistent.
Your combobox is a so-called dynamic control. I.e. you're building it in the code-behind instead of in markup.
Dynamic controls must be initialized prior to the viewstate in order to plug them into the ASP.NET Page Lifecycle events.
As you've found out, Page_Init is the correct place to initialize such a control. You can find more info on the subject here
Related
Right I have got a list box which contains a list of tracks and when the track is pressed it moves to a second list box, what I now need to happen is have the first item that's in the second list box move automatic to a text box.
This is my current code for the first move
private void genreListBox_DoubleClick(object sender, EventArgs e)
{
playlistListBox.Items.Add(genreListBox.SelectedItem);
}
I am thinking it should be something like this
presentlyPlayingTextBox.AppendText(playlistListBox.);
But I'm not sure how to add the first line without clicking it.
I have tried this but I get an error for the value.
presentlyPlayingTextBox.Text = playlistListBox.SelectedItem.Value;
Normally you would use something like the 'SelectedIndexChanged' or 'SelectedValueChanged' action of a listbox, otherwise events like DoubleClick will fail if the keyboard is used to change the selection.
Also that event should be triggered on the second listbox when it is changed by the first.
EDIT:
On a standard Listbox, there is no .SelectedItem.Value, only .SelectedItem.
However as a listbox holds objects, not just text, you need to say you want the text value.
presentlyPlayingTextBox.Text = playlistListBox.SelectedItem.ToString();
I don't know if I fully understand your question, but if you're just attempting to move the first item from ListBox_2 to a textbox, would the following work?
presentlyPlayingTextBox.Text = playlistListBox.Items[0]?.Value; // C# >= 6
presentlyPLayingTextBox.Text = ((playlistListBox.Items == null) ? playlistListBox.Items[0].Value : "" ); // C# < 6
You could also create your own delegate/event that would fire when the user did some action.
I am using ScintillaNET to make a basic IntelliSense editor. However, I have a problem when I call _editor.CallTip.Show("random text") in the AutoCompleteAccepted Event.
If I type pr for example, and scroll and select printf in the drop-down list, it goes to my AutoCompleteAccepted event and when I call the CallTip.Show, the rest of the word does not get added (however, without that CallTip code, the rest of the word is filled).
So, if I typed pr then it stays pr and I get my CallTip. How do I make sure the rest of the word gets inserted AND the CallTip shows?
Is the AutoCompleteAccepted Event not the right place to call it? If so, where should I call the CallTip.Show so that it works side-by-side with my AutoComplete?
Finally figured it out! The AutoCompleteAccepted Event isn't the right place to put CallTip.Show
What is going on is the fact that when AutoCompleteAccepted Event is called and you add text to the ScintillaNET control, it takes time for the UI to update and so, when you call to show the CallTip, it interferes with the text being inserted into the control.
The better way to do it is to call CallTip.Show in the TextChanged event, as they you know that the text has been inserted when the AutoCompleteAccepted Event was called.
It would now look something like this:
String calltipText = null; //start out with null calltip
...
private void Editor_TextChanged(object sender, EventArgs e)
{
if (calltipText != null)
{
CallTip.Show(calltipText); //note, you may want to assign a position
calltipText = null; //reset string
}
...
}
...
private void Editor_AutoCompleteAccepted(object sender, AutoCompleteAcceptedEventArgs e)
{
if (e.Text == "someThing")
{
/* Code to add text to control */
...
calltipText = "someKindOFText"; //assign value to calltipText
}
}
That is essentially what can be done to ensure the AutoComplete fills correctly and you get the CallTip to show.
Just note, that the CallTip MAY end up in unintended places, so it is recommended to set the value of where you want the CallTip to show up
I have a problem that I can't resolve on the server side of my project.
I'll explain:
I have a page named Global,this is ASP.NET page.
This page uses a UserControl named CateGories.
Now I have a button on this UC page,that when I press I want to invoke a function on the Global page that makes a connection with my DB.
I decided to use delegates(events)
This is the code.
Global page:
//here i add my function to the event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowCurrentTime.Text = DateTime.Now.ToString();
}
CateGories ClassCat = new CateGories();
ClassCat.MainDel += PopulateLinks;
}
//this is the function that the event will run
public void PopulateLinks(string CategoryName)
{....}
Code of the UC page (CateGories):
//delegation of the event
public delegate void Click(string ButtonName);
public event Click MainDel = null;
//function that invokes when I click a button
protected void News_Click(object sender, EventArgs e)
{
if (MainDel != null)
{
MainDel(News.Text);
}
}
Now everythig should work fine, but there is a problem, when the compiler gets to the
if(MainDel!=null)
...
It doesn't get in the function, there go MainDel is null.
I can't see the problem here, why after I insert function to MainDel, its gets null eventualy...
I'll be happy if someone can help
thanks.
Max.
I think I've encountered this problem before, when working with web applications the way I would a windows application.
The problem lies in that when the page gets reloaded, a new instance of your page class is created so any values from the last server interaction are lost.
MainDel is indeed null for what I can see.
You're creating one instance of CateGories on your Web Form and another one on your User Control. And once you check it for beeing null on the UC the reference is to the not initialized object.
One possible way to do that is creating and adding the User Control programatically to the page before PageLoad() and keeping a reference to it so you can access it's properties.
Another solution could be using Page.FindControl to find the UC and make the subscription to the event.
The method names above may be incorrect, it's been a long time without working with web forms.
I'm trying to build a Listview EDIT/Insert template where I can use a checkbox to enable updating multiple database tables but to little success.. I managed to get the insert working by performing some foul sorcery on the Listview inserting event. But I'd prefer that it works with the Checkbox OnCheckedChanged event as it feels abit more kosher in my mind, and of course the added benefit on it working for the edittemplate..
protected void checktest_clicked(object sender, EventArgs e)
{
//testlabel.Text = testcheck.Checked.ToString(); <-- exists outside of LW
// so it works
//Label hejha = (Label)lwRapport.FindControl("testlabel");
CheckBox trial = (CheckBox)lwRapport.FindControl("upParameter");
if(trial != null)
{
if(trial.Checked == true)
{ testlabel.Text = "finally"; }
if(trial.Checked == false)
{ testlabel.Text = "Nope, not going to happen"; }
}
if (trial == null)
{ testlabel.Text = "not wanted"; }
}
That's my test snippet for checking how the FindControl works and so far I've been quite unsuccessful making it do what I want it to do..
Any Correction on faults / hack / workaround for this matter would be apritiated
EDIT1*
The checkbox is inside of the listview, more precisely in the inserttemplate. The template looks something on the lines like this:
textbox <bind"table1.element">
textbox2 <bind"table1.element2">
checkbox [_]
textbox3 <bind"table2.element">
Observe that the snippet above is just a pseudocode snippet of my layout not the acctual layout. What I'm attempting is to find the checkbox and bind it's checked value to a parameter which passes a couple of checks in the SPROC then executes the UPDATE command
You seem to be not able to find the check box control from the list view. This is because you are searching for the check box inside the list view, and what you should be doing is searching for it inside the selected item.
You can have a look at this. Although it's GridView, I think it will works too.
I am trying to create a popup which will be used to select a month/year for a textbox. I have kind of got it working but however when I try and read from the textbox when I Submit the form it returns an empty string. However visually on the page I can see the result in there when I click the Done button which can be seen in the screenshot.
http://i27.tinypic.com/2eduttx.png - is a screenshot of the popup
I have wrapped the whole textbox/popup inside a Web User Control
Here is the code of the control
Code Behind
ASP Page
and then read from the Textbox on the button click event with the following
((TextBox)puymcStartDate.FindControl("txtDate")).Text
Any suggestions of how to fix the problem?
You may need to read the form posted value rather than the value from the view state. I have the following methods in my code to handle this.
The below code just grabs the values in the request headers (on post back) and sets/updates the controls. The problem is that when using the ASP.NET Ajax controls, it doesn't register an update on the control, so the viewstate isn't modified (I think). Anyways, this works for me.
protected void btnDone_Click(object sender, EventArgs e)
{
LoadPostBackData();
// do your other stuff
}
// loads the values posted to the page via form postback to the actual controls
private void LoadPostBackData()
{
LoadPostBackDataItem(this.txtYear);
LoadPostBackDataItem(this.txtDate);
// put other items here if needed
}
// loads the values posted to the page via form postback to the actual controls
private void LoadPostBackDataItem(TextBox control)
{
string controlId = control.ClientID.Replace("_", "$");
string postedValue = Request.Params[controlId];
if (!string.IsNullOrEmpty(postedValue))
{
control.Text = postedValue;
}
else
{
control.Text = null; // string.Empty;
}
}