asp:label change visibility after hiding it - c#

I've got an asp:Label and a asp:DropDownList that I want to be able to switch back and forth between visible and invisible when clicking on some buttons. Right now, my code looks like
aspx file
<asp:Label AssociatedControlID="statusFilter" id="statusFilterLabel" runat="server" CssClass="filterLabel">Status
<asp:DropDownList ID="statusFilter" runat="server" CssClass="filterInput" AutoPostBack="true" OnSelectedIndexChanged="anyFilter_SelectedIndexChanged" AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value=" 0"><All></asp:ListItem>
</asp:DropDownList>
</asp:Label>
<asp:Button Text="ALL" ID="AllTabButton" CssClass="tabButton" runat="server" OnClick="AllTab_Click" />
<asp:Button Text="Arrived" ID="ArrivedTabButton" CssClass="tabButton" runat="server" OnClick="ArrivedTab_Click" />
code behind
protected void AllTab_Click(object sender, EventArgs e)
{
AllTabButton.CssClass = "tabButtonClicked";
ArrivedTabButton.CssClass = "tabButton";
statusFilter.Visible = true;
statusFilterLabel.Visible = true;
}
protected void ArrivedTab_Click(object sender, EventArgs e)
{
AllTabButton.CssClass = "tabButton";
ArrivedTabButton.CssClass = "tabButtonClicked";
statusFilter.Visible = false;
statusFilterLabel.Visible = false;
}
The only problem is that if I try to set Visible=true after setting Visible=false it would give me an error Unable to find control with id 'statusFilter' that is associated with the Label 'statusFilterLabel'.
I tried doing some other things instead of using Visible, like setting the style: statusFilter.Style.Add("display", "block") and setting the cssclass: statusFilter.CssClass = "displayBlock"but the resulting error always showed up.
An asp:Panel would work, but I'm avoiding using that because I want my asp:Label and asp:DropDownList to line up with several other labels and dropdownlists; putting in a panel would make them not line up properly.
I'm guessing there is something I'm missing, something I just don't get, but I can't seem to figure out what that is. If anybody has any clue as to what's happening, I would really appreciate the help!

It's not able to always find the control on postback because it's a child of statusFilter. Move the input field outside of the label:
<asp:Label AssociatedControlID="statusFilter" id="statusFilterLabel" runat="server" CssClass="filterLabel">Status
</asp:Label>
<asp:DropDownList ID="statusFilter" runat="server" CssClass="filterInput" AutoPostBack="true" OnSelectedIndexChanged="anyFilter_SelectedIndexChanged" AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value=" 0"><All></asp:ListItem>
</asp:DropDownList>

Related

My panel fails to show if the user clicks a specific drop down list item

I am attempting to get a <div> to appear when a specific ListItem is selected.
In my code behind I have:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (reportedBefore.SelectedItem.Text=="yes")
{
reportedBeforePanel.Visible = true;
}
else
{
reportedBeforePanel.Visible = false;
}
}
I referred to this article here initially, which stated I needed a few things:
You need to Enable the AutoPostBack of the dropdownlist for raising the OnSelectedIndexChanged event on server side.
AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged
Admittedly, I did not have an AutoPostBack before. After adding it, I am afraid for some reason the requested div still does not show.
<asp:DropDownList ID="reportedBefore" CssClass="larger-drop-2" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="Select" Value="Select"></asp:ListItem>
<asp:ListItem Text="No" Value="No"></asp:ListItem>
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="Unsure" Value="Unsure"></asp:ListItem>
</asp:DropDownList>
<asp:Panel ID="reportedBeforePanel" runat="server" Visible="false">
<div id="showDiv">
<label for="yesDetails">
Please provide details
</label>
<asp:TextBox ID="yesDetails" CssClass="third-w-form" runat="server"/>
</div>
</asp:Panel>
Would someone be so kind to help me out here?
The problem is in the following if-condition:
reportedBefore.SelectedItem.Text=="yes"
By this, you are doing a case-sensitive string comparison (this is the default in .NET), but the values in your dropdownlist are written in a different way ("Yes" vs. "yes").
In order to fix this, either perform a case-insensitive string comparison
string.Compare(reportedBefore.SelectedItem.Text, "yes", true) == 0
or change the casing in the if-statement.
C# is case sensitive, so it's "Yes" not "yes":
reportedBeforePanel.Visible = reportedBefore.SelectedItem.Text == "Yes";
Alternatievely you can use this:
reportedBeforePanel.Visible = reportedBefore.SelectedItem.Text.Equals("yes", StringComparison.InvariantCultureIgnoreCase);

Stop DropDownList SelectedIndexChanged Event firing on FormView command

I have a dropdown list inside an editItemTemplate. The Dropdownlist onSelectedIndexChanged event fires on change like I want. However it also will fire when I submit the form.
********UPDATE ************
Per Saechel comment below, I started to investigate further. Here it describes it can be done. http://blog.programmingsolution.net/net-windows-application/selectionchangecommitted-and-selectedindexchanged-events-system-nullreferenceexception-while-closing-windows-form/
BUT, I tried it and it didn't even fire the event.
I checked http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist(v=vs.110).aspx and they don't list the event.
My goal is to let the user enter the values as a % of the Total or actual as actual tons and then toggle back and forth. The end value that will be inserted into the DB will be in tons.
I want to either:
a) not fire the "onSelectedIndexChanged" event
b) Some how determine if I am on the same index and have an if statement to skip everything
c) A better way of going about this that I don't know of.
I tried verifying via the ViewState the Dropdown's current index and skip the code if it was the same but couldn't find the value. Maybe help me figure out how to get that value? Searched around and could not find how do do it.
I tried: `var vs = ViewState["ddl_units"].ToString(); and some other variations as I needed to find the control via FindControl
.aspx:
<asp:Panel ID="pnlRecycledMaterialsReceivedForm" runat="server" Visible="false">
<asp:FormView ID="fvAddRecycledMaterialsReceived" runat="server" SkinID="annualReportFormview" EnableViewState="false"
HeaderText="Selected Recycled Materials Received Detail" DataKeyNames="RecycleDetailId" DefaultMode="Insert"
DataSourceID="odsRecycledMaterialsReceivedDetail" OnDataBound="fvAddRecycledMaterialsReceived_DataBound"
OnItemCommand="fvAddRecycledMaterialsReceived_ItemCommand" OnItemInserted="fvAddRecycledMaterialsReceived_ItemInserted"
OnItemUpdated="fvAddRecycledMaterialsReceived_ItemUpdated" OnItemDeleted="fvAddRecycledMaterialsReceived_ItemDeleted">
<EditItemTemplate>
<asp:TextBox ID="tbxRecycledTotalWasteQuantity" runat="server" Text='<%# Bind("TotalWasteQuantity") %>' Width="64px"></asp:TextBox>
<asp:TextBox ID="tbxRecycledWasteCommercialQuantity" runat="server" Text='<%# Bind("CommercialQuantity") %>' Width="64px"></asp:TextBox>
<asp:TextBox ID="tbxRecycledWasteResidentialQuantity" runat="server" Text='<%# Bind("ResidentialQuantity") %>' Width="64px"></asp:TextBox>
<asp:DropDownList ID="ddl_Units" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_Units_SelectedIndexChanged">
<asp:ListItem Value="" Text="" Enabled="false" />
<asp:ListItem Text="Tons" Value="1" />
<asp:ListItem Text="Percent" Value="9" />
</asp:DropDownList>
<asp:LinkButton ID="lbtnWasteReceivedUpdate" runat="server" Text="Update" CommandName="Update"
ValidationGroup="RecycledWasteReceivedDetail" Font-Bold="True" />
<asp:LinkButton ID="lbtnWasteReceivedInsertCancel" runat="server" Text="Cancel" CausesValidation="False" CommandName="Cancel" />
</EditItemTemplate>
</asp:FormView>
</asp:Panel>
.cs:
protected void ddl_Units_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox tbxRecycledTotalWasteQuantity = (TextBox)fvAddRecycledMaterialsReceived.FindControl("tbxRecycledTotalWasteQuantity");
TextBox tbxRecycledWasteResidentialQuantity = (TextBox)fvAddRecycledMaterialsReceived.FindControl("tbxRecycledWasteResidentialQuantity");
var d_TotalWasteQuantity = Convert.ToDecimal(tbxRecycledTotalWasteQuantity.Text);
ResidentialQuantity = Convert.ToDecimal(tbxRecycledWasteResidentialQuantity.Text);
DropDownList ddl_units = (DropDownList)fvAddRecycledMaterialsReceived.FindControl("ddl_units");
if (ddl_units.SelectedIndex.ToString() == "2")
{
//2 = percent
//Take tb value and convert to percent
//300/700 * 100
tbxRecycledWasteResidentialQuantity.Text = ((ResidentialQuantity / d_TotalWasteQuantity) * 100).ToString();
//ResidentialQuantity = ResidentialQuantity * (d_TotalWasteQuantity / 100);
}
else
{
//Else 'tons' was chosen. Convert value(%) to tons.
//700 * (43/100) = 301
ResidentialQuantity = d_TotalWasteQuantity * (ResidentialQuantity / 100);
tbxRecycledWasteResidentialQuantity.Text = ResidentialQuantity.ToString();
//tbxRecycledWasteResidentialQuantity.Text = "Tons";
}
}
Try using the SelectionChangeCommitted Event.
This will not trigger on form load but triggers on selectionchange

Update panel with textbox values when button clicked

I am still learning the basics of C# so any help would be appreciated. I have a series of asp:TextBox's. In the code behind, I am getting the value of these boxes. Is there a way to have a panel hidden until a user clicks submit then have the values display?
Here is the HTML for one of the boxes and the panel:
<asp:TextBox ID="txtTitle" runat="server></asp:TextBox>
<asp:Panel ID="PDFPanel" runat="server"></asp:Panel>
The button:
<asp:Button ID="btn_Submit" runat="server" Text="Button" OnClick="btnSubmit"/>
and the code-behind for it:
string Title = txtTitle.Text;
public void btnSubmit(Object sender, EventArgs e)
{
}
There are about 50 fields, so I am not showing all of it but if I can get direction on one I can replicate for the rest. Please let me know if I need to show any additional code
I am sorry if this is simple, but like I said, I am still an entry level developer. Thanks in advance!
Unless I've misunderstood what you're asking, this should be fairly simple.
<asp:Panel ID="PDFPanel" runat="server" Visible="False">
<div>
<asp:Literal id="litTitle" runat="server" />
</div>
</asp:Panel>
then in your click method:
litTitle.Text = txtTitle.Text;
PDFPanel.Visible = true;
Set the Panel's visibility to false by default
<asp:Panel ID="PDFPanel" runat="server" Visible="false">
<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
</asp:Panel>
then on the Button's click event set the visibility to true
public void btnSubmit(Object sender, EventArgs e)
{
PDFPanel.Visible = true;
// do something else...
}

Drop Down List and Selected Value

I'm having a very hard time figuring out what I'm doing wrong here. In my Edit Item Template I have the following code for my drop down list:
<asp:DropDownList ID="dd_is_active" runat="server" AppendDataBoundItems="true"
DataValueField="Enabled">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled") %>' />
Here is my aspx.cs code:
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
e.Values["SUB_last_modified_date"] = DateTime.Now.ToString();
e.Values["SUB_last_modified_by_user_id"] = HttpContext.Current.User.Identity.Name;
e.Values["SUB_last_modified_by_user_name"] = Session["UserName"].ToString();
e.Values["Enabled"] = ((DropDownList)(sender as ListView).InsertItem.FindControl("dd_is_active")).SelectedValue;
e.Values["Category_ID"] = ((DropDownList)(sender as ListView).InsertItem.FindControl("dd_category")).SelectedValue;
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
e.NewValues["SUB_last_modified_date"] = DateTime.Now.ToString();
e.NewValues["SUB_last_modified_by_user_id"] = HttpContext.Current.User.Identity.Name;
e.NewValues["SUB_last_modified_by_user_name"] = Session["UserName"].ToString();
}
It seems something is either missing from my .cs code or I have the values of 1 and 0 bound incorrectly in the html code. This exact same code works for the Insert Item Template, but the Update (or Edit Item Template) is not working correctly.
When I try to edit an item in my table I get an error stating the input string is in an incorrect format. I know it's trying to bind the Text of "Yes" or "No" but I need to ind to the Values of either "0" or "1". Any help is greatly appreciated!
I think your syntax is wrong for HiddenField value.
Instead of this
<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled") '>' />
It should be
<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled")%>' />

FindControl on control in invisible Panel generates Null reference error

The following code produces a null reference error on this line href.NavigateUrl = "foo.aspx?id=" + id; when I change the DropDownList selection, but not when I enter the id as a QueryString parameter. It seems like it has something to do with the order of events, but I'm not sure what, or how to fix it.
.aspx
<asp:Panel ID="Panel1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:DropDownList>
</asp:Panel>
<br />
<asp:Panel ID="Panel2" runat="server" Visible="false">
<asp:LoginView ID="LoginView1" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="superadmin">
<ContentTemplate>
<asp:HyperLink runat="server" ID="HyperLink1">HyperLink</asp:HyperLink>
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
</asp:Panel>
.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] != null)
{
if (!String.IsNullOrEmpty(Request.QueryString["id"]))
{
Panel1.Visible = false;
SetHref(Request.QueryString["id"]);
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SetHref(DropDownList1.SelectedValue);
}
protected void SetHref(string id)
{
Panel2.Visible = true;
HyperLink href = (HyperLink)LoginView1.FindControl("HyperLink1");
href.NavigateUrl = "foo.aspx?id=" + id;
href.Text = href.NavigateUrl;
}
I've found a couple of work-arounds: setting the default visbility of Panel2 to true is one, and moving the HyperLink outside of Panel2 and changing its visibility directly is the other, but neither of those do exactly what I want because there are other controls that I am trying make visible/invisible along with the HyperLink.
Any thoughts?
Instead of using the following line:
Panel2.Visible = true;
Try using:
Panel2.Attributes["style"] = "display:none";
I think the contents of your panel might not be rendering when you set Visible to false.

Categories