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
.....
}
Related
I am making a small C# application where the user enters information. the information is stored in an object and the object is in turn stored in a list. The information is displayed to the user in a listview.
I want to make it so that when the user clicks on an item in the listview the index of that item is passed to the list which finds the object with the same index and gets its information. The information is then shown in the same textboxes that the user enters his or hers information in.
My problem is that i do not now what method to call when the user selects a row in the listview.
This is what i have:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 1)
{
index = listView1.FocusedItem.Index;
textBox1.Text = manager.FocusedContact(index).FirstName;
textBox2.Text = manager.FocusedContact(index).LastName;
textBox3.Text = manager.FocusedContact(index).Street;
textBox4.Text = manager.FocusedContact(index).City;
textBox5.Text = manager.FocusedContact(index).ZipCode;
}
}
i tried:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
textbox1.Text = "hi";
}
so i know that private void listView1_SelectedIndexChanged is the wrong method, or is there some option for the listview that i forgot to toggle on or off?
You should be able to retrieve the selected index like this:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
var index = listView1.SelectedIndex;
}
If the event is not firing at all, check that the event handler is registered correctly in the form's Designer.cs file. In your case, it should look like this:
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
I want to use this solution to convert URLs to link in a listview label.
private string ConvertUrlsToLinks(string text)
{
string regex = #"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~_-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(regex, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return r.Replace(text, "$1").Replace("href=\"www", "href=\"http://www");
}
I have tried this in the listview databound but its not working.
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Label ProjectPostLabel = (Label)e.Item.FindControl("ProjectPostLabel");
ProjectPostLabel = ConvertUrlsToLinks({0});
}
Thank you
According to your code you are using ASP.NET Web Forms, not MVC.
You must use your ProjectPostLabel instance with Text property - no need to create a new label that is not assign to anywhere.
From your event you must retrieve Url property, not the label control. I have used NorthwindEmployee class with URL property in my ListView. You must cast it to your own class that is used in the list view.
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ProjectPostLabel.Text = ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL);
}
And you must remember that only the last item from your list view will be displayed in the label (unless you expect that behavior). If you want to list of URLs from the list you can write this:
protected void Page_Load(object sender, EventArgs e)
{
ProjectPostLabel.Text = string.Empty;
}
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ProjectPostLabel.Text += string.Format("{0}<br/>", ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL));
}
There are two Dropdown in the asp.net page.
When I select first one (city), the second one (district) is triggered.
I have a save button for the page.
When I click the save button, I write the SelectedValues of dropdowns in to the database.
And when I open the page again, I assign the values to the dropdown's selected values.
The first one is ok when I assign dropdown.SelectedValue = "5" but the second one is not triggered. How can I trigger it?
Thanx.
I have done the same thing. Suppose you have drop down say ddlCountry and ddlCity. You need to load all cities just for the selected country.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
LoadCountriesInDropDown(ddlCountry);
ddlCountry.SelectedValue = "5" //For eg:
LoadCitiesByCountrySelected(ddlCity, ddlCountry.SelectedValue); // selected country value was set here as 5
ddlCountry_SelectedIndexChanged(null, null);
}
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
if(ddlCountry.SelectedItem.Text == "Select")
{
ddlCity.Items.Clear();
}
else
{
LoadCitiesByCountrySelected(ddlCity, ddlCountry.SelectedValue);
}
}
Hope this was what you wanted.
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
I have a page with radio buttons and a textarea that populates data dynamically based on your selection. The radio buttons act as a list of article titles and on selection you see the content of the article.
Within my pageload method, I want to allow users to be able to see a URL in their browser that points to value they've. That way they can link to the article within another source.
Currently, the method I have allows me to link to the button selection if I manually type in the following example URLs:
http://localhost/test/Articles_test.aspx?selected=1
http://localhost/test/Articles_test.aspx?selected=2
I'd like to modify this so that the URL appears in the browser when a radio button selection is made. Plus, on page load defaults to the "0" index if no value parameter was specified.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int selected;
if (int.TryParse(Request.QueryString["selected"], out selected))
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "frm_Articles.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}
Set your radiobutton list to post back on change. Then, in the handler, do a redirect to the appropriate URL:
protected void Page_Load(object sender, EventArgs e)
{
int selected;
if (int.TryParse(Request.QueryString["selected"], out selected))
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "frm_Articles.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}