Dropdown not Updating lbl in Updatepanel - c#

Situation:
Checkbox inside a updatepanel
multiline textbox inside a different updatepanel.
if the user checks the checkbox, the multiline text box gets a name... this works fine.
HTML:
<asp:UpdatePanel ID="upTraveling" runat="server"
UpdateMode="Conditional" ChildrenAsTriggers="False">
<ContentTemplate>
<asp:CheckBox ID="cbRUTraveler" runat="server" Text="I am a Traveler" AutoPostBack="True"
oncheckedchanged="cbRUTraveler_CheckedChanged1" />
</ContentTemplate>
</asp:UpdatePanel>
<td>
<asp:UpdatePanel ID="upTravelers" runat="server" ondatabinding="cbRUTraveler_CheckedChanged1"
UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="tbTravelers"
Class="textwidth" runat="server" TextMode="MultiLine"
placeholder="FName LName, FName LName" required="required">
</asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="cbRUTraveler" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
C#:
protected void cbRUTraveler_CheckedChanged1(object sender, EventArgs e)
{
tbTravelers.Text =
RequesterBPL.RequesterTraveling(cbRUTraveler.Checked, tbTravelers.Text);
going = cbRUTraveler.Checked;
}
I also have 2 other updatepanels on the same page... a dropdown list in one updatepanel and a label in another updatepanel. When a user selects a value in the dropdown list... it's suppose to trigger a name placement in the label.
HTML:
<td>
<asp:UpdatePanel ID="upManager" runat="server"
ondatabinding="ddlTeam_SelectedIndexChanged"
UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlTeam"
EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lblManager" runat="server" > </asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</td>
C#:
protected void ddlTeam_SelectedIndexChanged(object sender, EventArgs e)
{
//upManager.Update();
lblManager.Text =
ManagerBPL.ReturnManagerName(ddlTeam.SelectedIndex);
}
However, when a user makes a selection in the dropdown, nothing happens.
until the user checks the checkbox which has nothing to do with the label and the dropdown. Once the user checks (or unchecks) the checkbox... the label gets populated with the selection from the dropdown.
All controls are in a table for structure. I have the scriptmanager in place.
From what I've been reading on the net this maybe a bug... If not a bug does anyone see where I may be going wrong...?
Thanks

Honestly you could do this all Client-Side. A simple example:
$(function () {
$('#<%= drpContent.ClientID %>').blur(function () {
var content = $('#<%= drpContent.ClientID %> option:selected').val();
$('#lblContent').text(content);
}
});
I've provided the initial example with the value declared. The second example is with a text representation.
$(function () {
$('#<%= drpContent.ClientID %>').blur(function () {
var content = $('#<%= drpContent.ClientID %> option:selected').text();
$('#lblContent').text(content);
}
});
An example with a Fiddle. The example though is simple, is far easier then dealing with an Update Panel. That particular control can be a nightmare to deal with, it can be quite painful. Though you asked about the server issue, this option is more viable and more common.
Update Panel (Drawback):
Creates pandomonium between Client / Server Side Events.
Stores your entire page in memory, then recreates (Performance Heavy)
Often breaks Page-State quickly in the Asp.Net Page Life Cycle.
Hopefully this helps.

Related

AsyncPostBackTrigger for Checkbox control not firing

Context of the problem:
I have a checkbox and a button. The button is disabled and colored grey upon the page load. I want the checkbox to enable and re-color the button if checked, and revert the change if unchecked. Think of it as a User Agreement page, where the user accepts the terms and needs to click on the checkbox to proceed.
There is a grid view and other components above the page that would break upon a postback, so searching online I found that I could use an UpdatePanel. Thought this would be more simple than writing jQuery for the checkbox, but it isn't working
Code:
ASPX
<asp:UpdatePanel ID ="upCheckbox" runat="server">
<ContentTemplate>
<asp:CheckBox ID="checkLabel" runat="server" OnCheckedChanged="checkLabel_CheckedChanged"/><asp:Label ID="AknowledgementLabel" runat="server" Text="Info is correct & Enable button"></asp:Label>
<br /><br />
<asp:Button ID="btnSubmit" Text="Submit Application" CssClass="jqbutton" OnClick="btnSubmit_Click" runat="server"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="checkLabel" EventName="CheckedChanged"/>
</Triggers>
</asp:UpdatePanel>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
btnSubmit.Enabled = false;
btnSubmit.ForeColor = System.Drawing.Color.Gray;
}
protected void checkLabel_CheckedChanged(object sender, EventArgs e)
{
if (checkLabel.Checked)
{
btnSubmit.Enabled = true;
btnSubmit.ForeColor = System.Drawing.ColorTranslator.FromHtml("#336699");
}
else
{
btnSubmit.Enabled = false;
btnSubmit.ForeColor = System.Drawing.Color.Gray;
}
}
Problem:
The checkbox does not work and the submit button remains disabled. Not sure if I am using Update Panels for the appropriate purpose or if am missing something. Is it possible to accomplish this the way that I am trying to, or should I move on to jQuery?
You are missing AutoPostback="true" in your CheckBox control. By default it is set to false. With autopostback missing your event won't reach server side.
<asp:UpdatePanel ID ="upCheckbox" runat="server">
<ContentTemplate>
<asp:CheckBox ID="checkLabel" runat="server" AutoPostback="true" OnCheckedChanged="checkLabel_CheckedChanged"/>
<asp:Label ID="AknowledgementLabel" runat="server" Text="Info is correct & Enable button"></asp:Label>
<br /><br />
<asp:Button ID="btnSubmit" Text="Submit Application" CssClass="jqbutton" OnClick="btnSubmit_Click" runat="server"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="checkLabel" EventName="CheckedChanged"/>
</Triggers>
</asp:UpdatePanel>

Unable to trigger the code-behind (aspx.cs) method from aspx page using update panel

I got a autocomplete textbox which displays Citynames. Whenever user clicks on a cityname the selected cityname is displayed in a textbox. This textbox value should be sent over to code behind method (aspx.cs) for fetching more details of the selected city name so that the resultant details are displayed in a gridview.
Now for passing the selected value I have added a textbox which copies the selected cityname value and enclosed it in a update panel. When ever the text box selection changes the idea is to trigger the code-behind method:
This is the code in aspx page:
$(document).ready(function () {
$('#txtName').on('change', function () {
$('#selectedItem').html(this.value);
}).change();
$('#txtName').on('autocompleteselect', function (e, ui) {
$('#selectedItem').val(ui.item.value);
});
});
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<label>Alternate Names: </label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Label ID="countLabel" runat="server"></asp:Label>
<br />
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="selectedItem" runat="server" OnTextChanged="selectedItem_TextChanged"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="selectedItem" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
</form>
This is the code in aspx.cs page:
protected void selectedItem_TextChanged(object sender, EventArgs e)
{
MessageBox.Show(selectedItem.Text);
}
But this method ain't getting triggered. Could somebody please help me with identifying the mistake i'm doing.
First, MessageBox.Show is not WEBforms code but WINforms. You should not mix those together. If you want to show results on a webpage, use javascript alert or a Modal.
Next item is this: $('#selectedItem').html(this.value);. It should be used with val()
$('#selectedItem').val(this.value);
Third if yo want to trigger a PostBack on a TextChange, use AutoPostBack=true
<asp:TextBox ID="selectedItem" ClientIDMode="Static" runat="server"
OnTextChanged="selectedItem_TextChanged" AutoPostBack="true"></asp:TextBox>
However a PostBack will not be triggered by changing the text from txtName in selectedItem also. the textbox needs to lose focus/blur itself to trigger the PostBack. So either just put txtName in the UpdatePanel and place the TextChanged event on that, or remove the TextChanged from selectedItem, place a Button in the UpdatePanel and click that with jQuery.
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="selectedItem" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="showResults" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
$(document).ready(function () {
$('#txtName').on('change', function () {
$('#selectedItem').val(this.value);
$('#Button1').click();
});
});
</script>
And then in code behind
protected void Button1_Click(object sender, EventArgs e)
{
showResults.Text = selectedItem.Text;
}

UpdatePanel controls unavailable - Object reference not set to an instance of an object

I have an UpdatePanel with a Label control, Label1, and a button outside it, Button1, and another Label control outside the UpdatePanel, Label2. When the button is clicked, I want the Label text to be updated in Label1:
ASPX page
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" AsyncPostBackTimeout="0" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
<asp:ContentTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
</asp:ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" />
<asp:Label ID="Label2" runat="server"></asp:Label>
</form>
Code-Behind
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label2.Text = "some text";
Label1.Text = "some text";
}
This should be straight-forward - I should be able to update the Label1 text with a button click event. The Label2 line succeeds (it obviously won't appear without a page postback, though), where the Label1 line fails with "Object reference is not an instance of an object". Why is Label1 null, when it is right there on the page, just that it is inside an UpdatePanel? How am I supposed to instantiate controls that should already be on the page and accessible, just like Label2 is?
Your async trigger must be inside the update panel. It may not be finding it because it is not inside of the update panel. Furthermore, because you are doing an async postback, only what is inisde of the update panel will get refreshed; thus you are in essence "reseting" Label 1.
This is why your code behind cannot find Label 1. Do this:
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" />
<asp:Label ID="Label3" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label ID="Label2" runat="server"></asp:Label>
</form>
This will help you to see. The labels 1 and 3 will always get updated now, but since label 2 is outside of the update panel, it will not because the Page doesn't see this on postback.
Code Behind:
protected void Button1_Click(object sender, EventArgs e)
{
Label3.Text = "label 3";
Label2.Text = "label 2";
Label1.Text = "label 1";
}
The result:
Code has <asp:ContentTemplate> and </asp:ContentTemplate> instead of <ContentTemplate> and </ContentTemplate> tags in the UpdatePanel. I corrected this and it works now. The controls became out of scope since the code couldn't find the real ContentTemplate or anything in it.

How to take fileuploader value in button click function .button in update panel file uploader in outside of update panel

I have put asp button in update panel and asp file uploader in outside of update panel in server side button click function i am geting file uploader empty.give me any idea..my code is below
<asp:FileUpload runat="server" ID="DecFormUpload"/>
<asp:UpdatePanel runat="server" ID="UpdateDecForm" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="DecFormUploadClick" OnClick="DecFormUploadClick_Click" OnClientClick="return DecFormUploadClick_Save();" runat="server" Text="Upload" />
</ContentTemplate>
</asp:UpdatePanel>
c# code
protected void DecFormUploadClick_Click(object sender, EventArgs e)
{
if(DecFormUpload.HasFile)//my problem is getting false here
{
}
}
This issue is due to that FileUpload needs a full postback, add the following just after your :
<Triggers>
<asp:PostBackTrigger ControlID="DecFormUploadClick" />
</Triggers>
Hope it helps

Why is my first drop down getting refreshed in a cascaded setup?

I am trying to setup a cascaded drop down box where the first one will result in the second one's value being changes and so on. I have some markup like this:
<form id="ddlSelections" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DDL1" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="DDL2" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:DropDownList ID="DDL1" OnSelectedIndexChanged="OnDDL1Change" AutoPostBack="true" runat="server" />
<asp:DropDownList ID="DDL2" OnSelectedIndexChanged="OnDDL2Change" AutoPostBack="true" runat="server" />
<asp:DropDownList ID="DDL3" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
And my code-behind is something like this:
protected void Page_Load(object sender, EventArgs e)
{
populateDDL1();
}
private void populateDDL1()
{
DDL1.Items.Clear();
// -- populate from SQL
}
protected void OnDDL1Change(object sender, EventArgs e)
{
DDL2.Items.Clear();
// -- populate from SQL
}
This is working except that everytime I click on the first drop down box, the values in the second drop down box get populated but the values in the first drop down box are being re-populated i.e. it looks like populateDDL1() is being called which of course will not be called unless Page_Load is called! I am failing to understand why this is the case. Any suggestions on where I am going wrong?
You are populating the first dropdownlist on every page load.
You need to do that only on the first load.
Wrap your populateDDL1 call in if (!IsPostBack), which is a backward way of saying to only populate it the first time the page loads.
To test this, put a breakpoint on that line in Page_Load.
Just because it's an ajax-y UpdatePanel doesn't mean that the server-side methods don't run.

Categories