I am having a issue that I just can figure why it is happening
the situation is that I have two dropdownlists, both set up the same way
<asp:DropDownList ID="DocumentLink" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DocumentLink_Changed">
</asp:DropDownList>
<asp:DropDownList ID="PageLink" runat="server" AutoPostBack="true" OnSelectedIndexChanged="PageLink_Changed">
</asp:DropDownList>
Their events look like this
protected void DocumentLink_Changed(object sender, EventArgs e)
{
DropDownList DocumentLink = sender as DropDownList;
LinkParam = DocumentLink.SelectedValue.ToString();
DescriptionParam = DocumentLink.SelectedItem.Text;
}
protected void PageLink_Changed(object sender, EventArgs e)
{
DropDownList PageLink = sender as DropDownList;
LinkParam = PageLink.SelectedValue.ToString();
DescriptionParam = PageLink.SelectedItem.Text;
}
In the case of the DropDown called "PageLink" the event handler fires. However for the "DocumentLink" the event handler does not. In debug I see that page load is fired but the event drops off after the page load and never enters DocumentLink_Changed
As an point of interest if I use a telerik radComboBox in place of the DropDownList using the same set-up it does work.
<telerik:RadComboBox ID="DocumentLink" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DocumentLink_Changed">
</telerik:RadComboBox>
with the event handler like this
protected void DocumentLink_Changed(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
unfortunately I need to use Dropdownlists for my project.
What could be causing this?
UPDATE
I have taken the working dropdownlist and used the LINQ binding for the dropdownlist that did not work. The result was that the PageLink dropdownlist began to behave just like the 'DocumentLink' dropdownlist. This makes me believe that the issue might be in the binding method but they the two are very similar and I do see results in the dropdownlist
this is my binding
if (selectedValue == 3)
{
DropDownList select = lvLinks.InsertItem.FindControl("PageLink") as DropDownList;
List<IPW_Links_get_document_listResult> getList = (from i in lqContext.IPW_Links_get_document_list(0, "my stuff") select i).ToList();
select.DataSource = getList;
select.DataTextField = "DocumentName";
select.DataValueField = "FolderPath";
select.DataBind();
}
if (selectedValue == 2)
{
DropDownList select = lvLinks.InsertItem.FindControl("PageLink") as DropDownList;
List<IPW_Links_get_available_pagesResult> getList = (from i in lqContext.IPW_Links_get_available_pages(PortalId) select i).ToList();
select.DataSource = getList;
select.DataTextField = "TabName";
select.DataValueField = "TabPath";
select.DataBind();
}
do check if you have any statements in the PageLoad event that alters the dropdownlists. If you have a databinding statement or a selection reset statement in the page load event , then make sure it is under the if not ispostback conditional snippet.
Related
I have a custom control which inherits from TextBox.
This allows me to use the TextBox's AutoPostBack property. This property makes the Page_Load method on the parent page fire when I change the value and click out of the text box.
I am setting the value of the rendered text box in JS as follows
var outputData = document.getElementById("CameraScannerTextbox1");
outputData.value = barcode.Value;
When this code runs I am expecting the Page_Load method to run again.
I have tried things like
outputData.focus();
outputData.value = barcode.Value;
outputData.blur();
The code in the Page_Load is
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Label1.Text = CameraScannerTextbox1.Text;
}
}
So basically I am hoping to have whatever is in barcode.Value set on Label1.Text on the server.
All you need is to trigger onchange event for input since ASP.NET adds postback code to onchange attribute. The simplest way is calling onchange manually
var outputData = document.getElementById("CameraScannerTextbox1");
outputData.value = barcode.Value;
outputData.onchange();
For more advanced techniques of simulating onchange event see this and this answers.
You can simply trigger the PostBack yourself with __doPostBack.
<asp:TextBox ID="CameraScannerTextbox1" runat="server" ClientIDMode="Static" AutoPostBack="true"></asp:TextBox>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
<script>
var outputData = document.getElementById("CameraScannerTextbox1");
outputData.value = '123456';
__doPostBack('CameraScannerTextbox1', '');
</script>
</asp:PlaceHolder>
Code behind
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Label1.Text = CameraScannerTextbox1.Text;
PlaceHolder1.Visible = false;
}
}
Not that I placed the javascript in a PlaceHolder that is hiddedn on PostBack, otherwise it will create a PostBack loop. But there are other ways to prevent that also.
I am using ASPxGridLookup control and I set AutoPostBack="false" for that control, but when I changed the value the normal page life cycle is getting executed what is the solution for this.
<dx:ASPxGridLookup ID="ASPxGridLookup1" runat="server" KeyFieldName="ID" AutoPostBack="false">
</dx:ASPxGridLookup>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataTable dtLookup = new DataTable();
dtLookup.Columns.Add("ID");
dtLookup.Columns.Add("Name");
dtLookup.Rows.Add("1", "Dorababu");
dtLookup.Rows.Add("2", "Vivekh");
ASPxGridLookup1.DataSource = dtLookup;
ASPxGridLookup1.DataBind();
}
}
If you set AutoPostBack to false, changing ASPxGridLookup value initiates callback, not postback. Callback is a special kind of postback that skips some of lifecycle events and doesn't update ViewState.
DevEx: Discussion about Page.IsPostBack and Page.IsCallback values
DevEx: The Concept of Callbacks
Difference between a Postback and a Callback
I have a feeling I'm missing something really obvious, I'm not able to capture the selected value of my DropDownList; the value renaubs the first item on the list. I have set the DropListList autopostback property to true. I have a SelectedIndexChangedEvent which is pasted below. This is NOT on the master page.
protected void ddlRestCity_SelectedIndexChanged(object sender, EventArgs e)
{
if (IsPostBack)
{
r_city = ddlRestCity.SelectedValue.ToString();
}
}
Here is the DropDownList control:
<asp:DropDownList ID="ddlRestCity" runat="server"
Width="100px" AutoPostBack="True"
onselectedindexchanged="ddlRestCity_SelectedIndexChanged">
</asp:DropDownList>
Thanx in advance for your help!
My off the cuff guess is you are maybe re-populating the list on a post back and that is causing the selected index to get reset.
Where is your DataBind() call? Are you checking !IsPostBack before the call? For example:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
ddlRestCity.DataSource = ...;
ddlRestCity.DataBind();
}
}
Explanation: If you don't check for !IsPostBack before DataBind(), the list will re-populate before SelectedIndexChanged is fired (because Page.Load fires before child events such as SelectedIndexChanged). When SelectedIndexChanged is then fired, the "selected item" is now the first item in the newly-populated list.
What is r_city?
If it's a textbox, then you need to do something like r_city.text = ...
Also -- you might consider removing your postback check. Usually, that's most useful in the page.onload event, and usually, you're checking for if NOT ispostback...
Staying away from javascript I endeavor to use Check_Clicked event handler to populate my Shipping info if same as Billing info in my FormView. This should be real simple but I have not been able to get the plumbing right.
I am following the example in http://msdn.microsoft.com/en-us/library/4s78d0k1%28v=vs.71%29.aspx#Y617 but would like to use my FormView instead of Form1.
The value that appears on checking the box is System.Web.UI.WebControls.TextBox
<asp:CheckBox id="SameCheckBox" AutoPostBack="True"
Text="Same as billing." TextAlign="Right"
OnCheckedChanged="Check_Clicked" runat="server"/>
protected void Check_Clicked(Object sender, EventArgs e)
{
CheckBox SameCheckBox = (CheckBox)FormView1.FindControl("SameCheckBox");
TextBox BillingFirst = (TextBox)FormView1.FindControl("BillingFirstNameTextBox");
TextBox ShippingFirst = (TextBox)FormView1.FindControl("ShippingFirstNameTextBox");
if (SameCheckBox.Checked)
{
ShippingFirst.Text = BillingFirst.ToString();
}
else
ShippingFirst = null;
}
In addition to the solutions given to me below I will add for others edification; the other problem I had was DropdownList data. Here is what worked for me:
DropDownList BillingState = FormView1.Row.FindControl("BillingStateTextBox") as DropDownList;
DropDownList ShippingState = FormView1.Row.FindControl("ShippingStateTextBox")as DropDownList;
ShippingState.SelectedValue = BillingState.Text;
This line:
ShippingFirst.Text = BillingFirst.ToString();
Should be:
ShippingFirst.Text = BillingFirst.Text;
The ToString() output of a WebControl will be the type name.
Use :
ShippingFirst.Text = BillingFirst.Text;
I´ve read most posts here but i can´t figure out why the "CheckedChanged" Event is not firing. Here is my situation.
I´m using a Repeater to generate Items out of a Database. Each ReapeaterItem should include an UpdatePanel, because i have to Update the Controls inside the UpdatePanel and do not want to reload the complete page. Inside these dynamically generated UpdatePanels (each RepeaterItem has one) i´m adding up to three Checkboxes dynamically (based on the Database). These Checkboxes need to fire the "CheckedChanged" event, because on some conditions i want to enable/disable/check/uncheck Checkbox1, 2 or 3 based on business logic. ... Hope you got this so far. I´m adding all Controls and have the EventHandler Added. But the generated Code does not reflect the Event Handler. I tried OnItemDataBound, OnItemCreated, PreRender, ... Events to add the Eventhandler too, but i was not able to find the CheckBox-Control with the ID.
I´m totally lost with this and on the way to use Buttons instead of Checkboxes. From what i read so far is that with Buttons i can use the CommandName from the Button and the ItemCommand-Event from the Repeater to get a workaround, but then i need to reflect the "Check" on the Page in some way.
btw, every Repeater (8) sits inside an ajaxtoolkit-accordion control.
Here i give you some Code:
aspx-Page
<asp:Repeater ID="RepeaterAccordionPane2" runat="server">
<ItemTemplate>
HTML Stuff<%# DataBinder.Eval(Container.DataItem, "Header")%>HTML Stuff<%# DataBinder.Eval(Container.DataItem, "Beschreibung")%></td>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode=Conditional>
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
HTML Stuff
</ItemTemplate>
</asp:Repeater>
Here is the Page_Load Part
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dvAlleArtikel = new System.Data.DataView(...Database...);
[... some other code here ...]
RepeaterAccordionPane2.DataSource = dvAlleArtikel;
//RepeaterAccordionPane2.ItemCreated +=new RepeaterItemEventHandler(RepeaterAccordionPane2_ItemCreated);
//RepeaterAccordionPane2.PreRender +=new EventHandler(RepeaterAccordionPane2_PreRender);
RepeaterAccordionPane2.DataBind();
int nUpdatePanelIndex = 0;
foreach (Control crInRepeater in RepeaterAccordionPane2.Controls)
{
if (crInRepeater.GetType() == typeof(RepeaterItem))
{
foreach (Control crInRepeaterItem in crInRepeater.Controls)
{
if (crInRepeaterItem.GetType() == typeof(UpdatePanel))
{
LiteralControl litTabelleBeginn = new LiteralControl("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">");
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litTabelleBeginn);
if (dvAlleArtikel[nUpdatePanelIndex]["ArtNr1"].ToString() != "0")
{
CheckBox CheckBox1 = new CheckBox();
CheckBox1.ID = dvAlleArtikel[nUpdatePanelIndex]["ArtNr1"].ToString();
CheckBox1.Text = (dvAlleArtikel[nUpdatePanelIndex]["CheckBoxLbl1"].ToString() == "" ? "leer" : dvAlleArtikel[nUpdatePanelIndex]["CheckBoxLbl1"].ToString());
CheckBox1.AutoPostBack = true;
CheckBox1.CheckedChanged +=new EventHandler(CheckBox1_CheckedChanged);
LiteralControl litNeueTabellenZeileBeginn = new LiteralControl("<tr><td width=10><img src=\"images/helper/spacer.gif\" width=\"10\"></td><td height=\"20\">");
LiteralControl litNeueTabellenZeileEnde = new LiteralControl("</td><td width=\"100\" height=\"20\">" + dvAlleArtikel[nUpdatePanelIndex]["ArtPrice1"].ToString() + " € </td></tr>");
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litNeueTabellenZeileBeginn);
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(CheckBox1);
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litNeueTabellenZeileEnde);
}
[... some other code here...]
LiteralControl litTabelleEnde = new LiteralControl("</table>");
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litTabelleEnde);
nUpdatePanelIndex++;
}
}
}
}
This code is never reached:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
int foo = 0;
}
This is the CheckBox-Code generated:
<input id="AccordionPane2_content_RepeaterAccordionPane2_ctl00_6200" type="checkbox" name="AccordionPane2_content$RepeaterAccordionPane2$ctl00$6200" onclick="javascript:setTimeout('__doPostBack(\'AccordionPane2_content$RepeaterAccordionPane2$ctl00$6200\',\'\')', 0)" />
The Event is generated, but when i click the Checkbox all Content in the UpdatePanel is gone and the CheckedChanged-EventHandler is not fired.
What am i doing wrong?
Thanks to all advice, i´m really stuck.
mk
The first time the page loads you are adding all the checkboxes to the Controls collection, and they get rendered. When you do a postback (through the CheckBox's AutoPostBack) you have a check if(!IsPostBack) that doesn't allow the checkboxes to be added to the Controls collection on the postback. Because of that, you won't see the controls and the page, and when the page lifecycle tries to call the events (which occurs AFTER Page_Load), the controls that created the events are no longer there.
You will need to refactor your Page_Load method so it does two things - 1, regardless of the value of IsPostBack bind the repeaters and create the dynamic controls. 2, if IsPostBack==false, i.e., an initial load, then set the values of the dynamic controls. you don't want to set the values of the dynamic controls when IsPostBack==true because then you will lose the values the user entered.
also, just a note:
if (crInRepeater.GetType() == typeof(RepeaterItem))
can be rewritten as:
if (crInRepeater is RepeaterItem)