How can I trigger dropdown while viewing asp.net page - c#

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.

Related

Paging Selected Index Causes DataGrid Refresh

I am making a web page with asp.net, the problem has to be with a asp:datagrid and its page index changing event. The records are displaying and paging correctly but after making a filter and reduce its grid content and trigger the page index again, the grid refreshes and came back all the other records.
This is my PageLoad()
if (!IsPostBack)
{
txtDate1.Attributes.Add("placeholder", "Enter Start Date DD//MM//YY");
txtDate2.Attributes.Add("placeholder", "Enter End Date DD//MM//YY");
this.bindgrid();
}
else
this.bindgridfilter(txtDate1.Text, txtDate2.Text);
My bindGridsmethods()
private void bindgrid()
{
ds = Classes.DBMethods.ShowRecords();
grdRecords.DataSource = ds.Tables[0];
grdRecords.DataBind();
}
private void bindgridfilter(string date1, string date2)
{
dsf = Classes.DBMethods.SearchBetweenDates(date1,date2);
grdRecords.DataSource = dsf.Tables[0];
grdRecords.DataBind();
}
My PageIndexChanging Event
protected void grdRecords_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdRecords.PageIndex = e.NewPageIndex;
grdRecords.DataBind();
}
And my FilterButton Click()
protected void Filter_Click(object sender, EventArgs e)
{
grdRecords.DataSource = null;
bindgridfilter(txtDate1.Text, txtDate2.Text);
}
Now it does the opposite it disappear the grid after making a change of page without a filter, and if it applies a filter now the pagination works correctly
In most web pages, just setting up a drop down list, a grid view, or whatever?
You ONLY want to bind and load the drop down list, or gridview ONE time.
If you re-beind, and re-load the control AGAIN with data, then you will lose your settings.
So, 99% of the time, you want to do such loading ONLY one time,and ONLY on the first page load. Hence, your page load event needs to be this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtDate1.Attributes.Add("placeholder", "Enter Start Date DD//MM//YY");
txtDate2.Attributes.Add("placeholder", "Enter End Date DD//MM//YY");
bindgrid();
}
}
So, now when a button is clicked, or say gv selected index changes, or whatever? Then the information can be used.
If you rebind on each post back, then a gv, or even a simple drop down list will lose its values - hence ONLY load and bind your controls on first "real" page load as per above.
Page load fires EACH time and for each post back. So, any button click or whatever? The page load event will run again. Hence, your general setup code, startup code, and code to load up things like a drop down list, or gridview?
You need to have a if (!IsPostBack) code stub. The last 200 web pages I have created - everyone has such a code stub, and you really for all purposes can't build a working web page without this "setup" or "first load" code stub. If you re-load or re-setup your controls each time, then anything the user enters or does with such controls will be lost.
Of course YOUR buttons etc. and code can and will re-load the grid, but since the page load fires every time with all and any button click (any post-back), then that re-load code will also re-fire, and you don't want that to occur.
Is it correct? PageLoad()
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtDate1.Attributes.Add("placeholder", "Enter Start Date DD//MM//YY");
txtDate2.Attributes.Add("placeholder", "Enter End Date DD//MM//YY");
this.bindgrid();
}
}
PageIndex Event
protected void grdRecords_PageIndexChanging1(object sender, GridViewPageEventArgs e)
{
grdRecords.PageIndex = e.NewPageIndex;
if (txtDate1.Text == "" & txtDate2.Text == "")
{
bindgrid();
}
else
this.bindgridfilter(txtDate1.Text, txtDate2.Text);
}

Why is my gridview rebound after selected index change?

I have a Gridview that is populated through SQLDataSource. The query behind is rather complex and the GridView takes some seconds to get filled; that's why I get annoyed by the fact that every time I select a row, the Gridview disappears for a while and is getting repopulated again. What does fire that rebind?
The selected row index works as Control Parameter for a second Gridview, that displays detail information on that row. There are these 2 events defined for the gridview:
protected void GridView_PURCHTABLE_OnDataBound(object sender, EventArgs e) {
if(DisplayPurchItems.Checked == false)
{
GridView_PURCHTABLE.Columns[4].Visible = false;
}
else
{
GridView_PURCHTABLE.Columns[4].Visible = true;
}
protected void GridView_PURCHTABLE_Selectedindexchanged(Object sender, EventArgs e) {
GridView_Notes.DataBind(); //this is the second gridview
}
Anyone has a clue what might cause the gridview to rebind?
Martin
Check once:Is post back.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//binding grid
}
}

Confused with telerik event handlers

I'm not sure if this is right for getting a value of a multi combo box
when user selects something i need to get it's first column value and pass it to my DB
i'm trying to get a value from the telerik Multicombo box and idon't know wich event handler to use?
here's my code
private void radMultiColumnComboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
if (radMultiColumnComboBox3.MultiColumnComboBoxElement.Rows.Count > 0)
{
radMultiColumnComboBox3.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
var item = radMultiColumnComboBox3.MultiColumnComboBoxElement.Rows[0];
radGridView1.Rows.Add(item);
}
else
{
MessageBox.Show("PLS select a row", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
This is how you get the value of the first column when the user selects a row in the Telerik's MultiColumnComboBox:
void radMultiColumnComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var value = radMultiColumnComboBox1.EditorControl.Rows[radMultiColumnComboBox1.SelectedIndex].Cells[0].Value;
}
You can find the documentation of the SelectedIndexChanged event 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
.....
}

Preserving DropDownList settings

I have a webpage: Menu.aspx, where I have the following controls of relevance:
a DropDownList with some values
a button, which redirects me to a page called Edit.aspx.
In my Edit.aspx page, I have a cancel button, which redirects the user back to the Menu page with the following C# method
protected void btnCancel_click(object sender, EventArgs e)
{
Response.Redirect("Menu.aspx");
}
What I would like to do is when I redirect my user back to Menu.aspx, I preserve the value of the DropDownList that they selected.
Thanks in advance!
EDIT: Ideally, I want to achieve this without a QueryString (if Possible)
EDIT2: I was thinking of something invovling a viewstate or postback but I'm unfamiliar with those.
When you called Response.Redirect("Menu.aspx"); you were already making a new request which the previous page was not part. It's like calling the page anew.
You can put the value in session before coming to Edit.aspx or pass the value back through query string
Response.Redirect("Menu.aspx?dropdownvalue=123");
On Menu.aspx, you'll check if ?dropdownvalue=123 exist and you set selected item to that value
Isn't the most ellegant solution, but you can create a session variable containing the selected value.
For example:
dropDown_SelectedIndex(object sender, EventArgs e)
{
Session["SelectedItem"] = dropDown.SelectedValue;
}
When you do the redirect, on the page load of the Menu.aspx, after you populate the DropDown, check if this session variable exists, then select the item and remove it from the application.
void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//populate drop down here
if(Session["SelectedItem"] != null)
{
dropDown.SelectedValue = Session["SelectedItem"].ToString();
Session["SelectedItem"] = null;
}
}
}
Or you can pass it via query string, but this way you preserve you url

Categories