I have an aspx page. On pageLoad a call a method that loads a user control
protected void Page_Load(object sender, EventArgs e)
{
LoadUC();
}
This loads the user control onto the page (into a placeholder) passing a few generic values.
private void LoadUC()
{
ucTS ctrl = (ucTS)Page.LoadControl(_ucTSPath);
ctrl.ParentId = 0;
ctrl.addNew = false;
phFG.Controls.Add(ctrl);
}
The user control (which contains a repeater) loads the initial placeholder as well as another placeholder on the initial page:
protected void Page_Load(object sender, EventArgs e)
{
LoadItems();
}
private void LoadBOMItems()
{
List<Item> dtItem;
if (ParentId == 0)
{
if (ViewState["ItemFG"] == null)
{
dtItem = //Gets list of items
}
else
{
dtItem = (List<Item>)ViewState["ItemFG"];
}
ViewState["ItemFG"] = dtItem;
}
else
{
if (ViewState["Items" + ParentId] == null)
{
dtItem = //get list of items
}
else
{
dtItem = (List<Item>)ViewState["Items" + ParentId];
}
ViewState["Items" + ParentId] = dtItem;
}
if (dtItem.Count > 0)
rptTSItem.DataSource = dtItem;
rptTSItem.DataBind();
}
}
in the binding, I bind the repeater, but I am also adding more of the same user control.
The problem comes when I click add a new item to the repeater. The initial click, everything saves fine and a new row is added. The second click the user control is not found on the initial page and so the save method is not fires. The 3rd click, everything is fine, the 4th, the user control is not found. This keeps happening. Why is my usercontrol not always found? I have tried doing a postback check in multiple places, but that doesn't seem to work.
I am still not 100% sure why the event would fire properly every OTHER time as opposed to just the first time, but the solution was simply adding an ID to the user control.
private void LoadUC()
{
ucTS ctrl = (ucTS)Page.LoadControl(_ucTSPath);
ctrl.ParentId = 0;
ctrl.addNew = false;
ctrl.ID = "someID"
phFG.Controls.Add(ctrl);
}
and whenever I re-added the user control from within the user control, I pass the same ID.
Related
I have a web page which is contained a Data Filter and a report.The Data Filter is a user control. The report is loaded inside the main page so totally i have two pages. one user control and one web page.
Now i am going to gather the data by clicking a button inside the user control then i can use it to filter the table, but it seems that during the post back it goes first to the Page_Load method of the main, not the user control so the report is constructed before filtering.The BtnPreviewReport_Click must be executed earlier than the page_Load.
What should i do ?
User control
protected void BtnPreviewReport_Click(object sender, EventArgs e)
{
Date = Year.Text + "/" + Month.Text + "/" + Day.Text;
}
Main Page
protected void Page_Load(object sender, EventArgs e)
{
string date = UserControls1.Date;
Response.Write(date);
}
Output : Nothing
I am not sure why the ButtonClick event should be run earlier than page load.
But here's a simple way to solve your question:
private bool isPageLoaded = false;
private bool isButtonClicked = false;
private void ButtonClick()
{
isButtonClicked = true;
doTheFirstThing();
if( isPageLoaded )
{
doTheSecondThing();
}
}
private void PageLoad()
{
isPageLoaded = true;
if( isButtonClicked )
{
doTheSecondThing();
}
// else let the button click handle the SecondThing()
}
I have to radio buttons that reveal or hide panels.
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedIndex == 0)
{
newPanel.Visible = true;
updatePanel.Visible = false;
}
else if (RadioButtonList1.SelectedIndex == 1)
{
newPanel.Visible = false;
updatePanel.Visible = true;
}
}
Outside of these panels is a drop down list which effects a data source (gridview). The second radio button reveals the panel that holds this data source. The code for changing that happens when a different item selected from the drop down menu is as follows:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//change WHERE clause for SQL statement that effects grid view based on drop down menu selection
SqlDataSource4.SelectParameters["userIdSelected"].DefaultValue = userNameDropDown.SelectedItem.Value;
}
This functions correctly and updates changes the data source that is being displayed.
However, if I currently have the first radio button chosen (that reveals a different panel) and then change the name from the drop down list, when I go to the second radio button the panel that opens shows the data source that was previous. It does not correctly reflect the change to the drop down menu.
Edit: I have tried adding the change to the radio button change but I still have the same behavior:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataSource4.SelectParameters["userIdSelected"].DefaultValue = userNameDropDown.SelectedItem.Value;
if (RadioButtonList1.SelectedIndex == 0)
{
newPanel.Visible = true;
updatePanel.Visible = false;
}
else if (RadioButtonList1.SelectedIndex == 1)
{
newPanel.Visible = false;
updatePanel.Visible = true;
}
}
I am trying to assign a ViewState value in my application with a SelectedIndexChanged function. Once it's assigned the postback will use the value to change some data and then set the value to zero but I can't seem to get it to work correctly. The controls are all created dynamically on Page_Load.
Page Load
protected void Page_Load(object sender, EventArgs e)
{
CreateAttributeControls();
TempProductVariantId = 0;
}
Create Attribute Controls
public void CreateAttributeControls()
{
...
var ddlArtistArtworks = new DropDownList();
ddlArtistArtworks.ID = "ddlArtistArtworksTest";
divAttribute.Controls.Add(ddlArtistArtworks);
ddlArtistArtworks.Items.Clear();
ddlArtistArtworks.SelectedIndexChanged += new EventHandler(ArtistArtwork_SelectedIndexChange);
ddlArtistArtworks.AutoPostBack = true;
...
}
ArtistArtwork_SelectedIndexChange
protected void ArtistArtwork_SelectedIndexChange(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
TempProductVariantId = int.Parse(ddl.SelectedValue);
}
TempProductVariantId ViewState Save
public int TempProductVariantId
{
get
{
if (ViewState["TempProductVariantId"] == null)
return 0;
else
return (int)ViewState["TempProductVariantId"];
}
set
{
ViewState["TempProductVariantId"] = value;
}
}
When I load the page everything is fine. I change the DropDownList's value, It posts back, and the value is not set. Change it again the value is set and continues to change as I change the value of the DropDownList.
Any guidance on this would be greatly appreciated.
Note: I have tried changing when CreateAttributeControls() is called. In OnPreRender for example. I was given this to understand the lifecycle of the page Life Cycle
That's because you are essentially recreating the dropdown on every postback..
try this
public void CreateAttributeControls()
{
...
DropDownList ddlArtistArtworks;
if (!IsPostBack)
{
ddlArtistArtworks = new DropDownList();
ddlArtistArtworks.ID = "ddlArtistArtworksTest";
divAttribute.Controls.Add(ddlArtistArtworks);
ddlArtistArtworks.Items.Clear();
ddlArtistArtworks.AutoPostBack = true;
}
else
{
ddlArtistArtworks = (DropDownLise)divAttribute.FindControl("ddlArtistArtworksTest");
}
ddlArtistArtworks.SelectedIndexChanged += new EventHandler(ArtistArtwork_SelectedIndexChange);
...
}
For dynamically added controls, the event handler has to be linked up everytime so that has to be done outside the if-block, unconditionally.
[edit]
calling the follows directly is not possible in my code:
void control_Changed(object sender, EventArgs e)
This function is loop through a Collection of DropDownListBox, and each DropDownListBox has different Select_Change function. Also they are not in the same page, they collection of DropDownListBox is come from different user control of the page.
I saw a lot of solution are simply calling the function that the event should trigger..
But this will not be working on my case.
I have a code that will mapping the data to a collection of dropdownlistbox and select the proper dropdownlistbox item for each dropdownlistbox.
So, is kind of like this:
foreach (Control aControl in aControlCollection){
if (aControl.GetType() == typeof(RadComboBox))
{
bool FoundItem = false;
RadComboBox aComboBox = (aControl as RadComboBox);
foreach (RadComboBoxItem aComboItem in aComboBox.Items)
{
Debug.WriteLine("aComboItem " + aComboItem.Text + " Value" + aComboItem.Value);
if (aComboItem.Value.ToLower() == _dataObject.ToString().ToLower())
{
//aComboBox.SelectedIndex = aComboBox.Items.IndexOf(aComboItem);
aComboItem.Selected = true;
FoundItem = true;
~~~FIRE EVENT HERE~~~~~
//break;
}
else {
aComboItem.Selected = false;
}
}
if (!FoundItem)
{
RadComboBoxItem aComboItem = new RadComboBoxItem();
aComboItem.Value = _dataObject.ToString();
aComboItem.Text = _dataObject.ToString();
aComboBox.Items.Add(aComboItem);
aComboBox.SelectedIndex = aComboBox.Items.IndexOf(aComboItem);
}
}
}
}
Normally in the page, when user select the a first dropdownbox, the 2nd dropdownbox that follows will generate the proper dropdownlist item according to the first dropdownbox (from the first dropdownbox selectindexchange event).
So I wonder if there is anyway I can fire the DropDownListBox programmatically?
Just to make it even more clear, the above function is call by iterates all DropDownListBox on the page, so they can be link into different function.
Combobox_SelectedItem(null, null);
You can forge any arguments you desire into the parameters, if needed.
If you were to use the traditional void control_Changed(object sender, EventArgs e) code...
if (aComboItem.Value.ToLower() == _dataObject.ToString().ToLower())
{
//aComboBox.SelectedIndex = aComboBox.Items.IndexOf(aComboItem);
aComboItem.Selected = true;
FoundItem = true;
control_Changed(aComboItem, new EventArgs());
}
void control_Changed(object sender, EventArgs e) {
// your code here
}
hi i need to modify my code a little bit. i have a page with a radio button list and a textarea. the textarea is populated when a users makes a radio button selection.
also, when a user makes a radio button selection the url will hold an extention in the url to show which selection index number they have selection. (i.e. ?selected=0)
http://test.com/frm_Articles.aspx?selected=0
http://test.com/frm_Articles.aspx?selected=1
http://test.com/frm_Articles.aspx?selected=2
that way they can copy the url and reference it in other websites as a link. or place it in their favorites.
the problem is, if you grab the url and open a new browser, the page does not pass the value and databind accordingly. no radio buttons or content appear on the page.
must be the postback logic i think???
what's working:
when i launch the website the radio buttons appear and index 0 is set
when i select radio buttons the correct data displays and urls linking to radio button values display in browser (i.e. http://test.com/test.aspx?selected=2)
if i cut and paste pointer urls within the same browser then correct data is rendered
what doesn't work (everything that deal with an false PostBack):
1.when i launch website no data within the textarea apprears even though the radio button is set to 0 index and is visiable.
2. if i cut and paste pointer url into a new browser, text area and radio buttons do not display.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
int selected;
if (Request.QueryString["selected"] != null)
{
if (int.TryParse(Request.QueryString["selected"], out selected))
{
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
}
else
{
int firstart = 0;
RadioButtonList1.SelectedIndex = firstart;
RadioButtonList1.DataBind();
}
}
}
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
//
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
try{
e.Command.Parameters["#URL_FK"].Value = Session["URL_PK"];
}
catch (Exception ex)
{
}
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "test.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}
}
In your code at Page_Load event before this line
RadioButtonList1.SelectedIndex = selected;
you should bind RadioButtonList1. after binding RadioButtonList you can set SelectedIndex.
my SqlDataSource1_Selecting method was the issue. i used another approach and my code worked.