FindControl on control in invisible Panel generates Null reference error - c#

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.

Related

fileupload control is always empty

I'm using a FileUpload in 3 different parts of my project.
In 2 parts it works but in the other one it doesn't
My FileUpload:
<prj:GridViewExtensionPanel ID="exPanelCustosVariaveis" runat="server">
<prj:GridView ID="GvCustoVariavel" runat="server">
<prj:TemplateField>
<ItemTemplate>
<%#Eval("Ficheiro_Designacao")%>
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="FU_Ficheiro" class="custom-file" runat="server" EnableViewState="true" />
</EditItemTemplate>
</prj:TemplateField>
</prj:GridView>
</prj:GridViewExtensionPanel>
My CodeBehind:
protected void gvCustosVariavel_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GvCustoVariavel.Rows[e.RowIndex];
System.Web.UI.WebControls.FileUpload FU_Ficheiro = (System.Web.UI.WebControls.FileUpload)row.FindControl("FU_Ficheiro");
}
I've loaded the File but in the codeBehind the control is always empty (.HasFile always = false)
P.S: The FileUpload IS NOT inside a panel

asp:label change visibility after hiding it

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>

asp.net radiobuttonlist doesnt fire on selection

I have created a simple radiobuttonlist. And I wrote simple function to do something on selection
here is the radiobuttonlist
<asp:RadioButtonList ID="payment_type" CssClass="rbl_type" runat="server" TextAlign="Right" RepeatDirection="Horizontal" BorderStyle="None" OnSelectedIndexChanged="payment_type_SelectedIndexChanged">
<asp:ListItem Value="0">Serbest Ödeme</asp:ListItem>
<asp:ListItem Value="1">Ön Tanımlı Ödeme</asp:ListItem>
</asp:RadioButtonList>
here is the c# code
protected void payment_type_SelectedIndexChanged(object sender, EventArgs e)
{
if (payment_type.SelectedValue == "0")
{
pnl_serbest.Visible = true;
pnl_on_tanimli.Visible = false;
}
else
{
pnl_serbest.Visible = false;
pnl_on_tanimli.Visible = true;
}
}
but it doesnt trigger anything. what am I doing wrong?
Add
AutoPostBack="true"
attribute to your <asp:RadioButtonList /> tag.

ValidationSummary in MasterPage hide success label

I've got a ValidationSummary and SuccessLabel in the MasterPage
When the SuccessLabel has detail in it, and then the ValidationSummary then fails validation I want it to hide the SuccessLabel and only show the ValidationSummary.
<div id="ApplicationStatus" class="ValidationSummaryContainer">
<asp:Label ID="StatusLabel" CssClass="SuccessSummary" runat="server"
Visible="false"></asp:Label>
<asp:Label ID="WarningLabel" CssClass="WarningSummary" runat="server"
Visible="false"></asp:Label>
<asp:ValidationSummary ID="ErrorValidationSummary" runat="server"
CssClass="ValidationSummary" DisplayMode="List" />
<asp:CustomValidator ID="ErrorCustomValidator" runat="server"></asp:CustomValidator>
</div>
<div id="ApplicationContent" class="ApplicationContentContainer">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
StatusLabel.Text = "Successfully loaded record";
}
}
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
<asp:Textbox ID = "Text1" runat="server"/>
<asp:RequiredFieldValidator id="InputTextBoxRequiredFieldValidator" runat="server"
ControlToValidate="Text1" Visible="false" CssClass="InlineNoWrap" Enabled="true">
</asp:RequiredFieldValidator>
<asp:Button ID = "Button1" runat="server" Text="Submit"/>
</asp:Content>
I'm trying to find a way in JavaScript to catch the validation error and hide the StatusLabel.
I don't want to have to put a javascript function on every button on every page that uses the MasterPage.
Thanks,
Alex
How about something like this:
protected void Submit(object sender, EventArgs e)
{
if (IsValid)
{
StatusLabel.Visible = true;
}
else
{
StatusLabel.Visible = false;
}
}
Your validation code are totally miss lot of fields.
ok now we are going your pint .
Set visible false in your label for page load event
then success time add the label text ,and set visible true
you miss control to validate and validationgroup and display fields
please see this sample

C# Dynamic created dropdownlist not binding after FormView Update

I am having trouble getting my dropdownlist to populate after I update the sqldatasource or change FormView Modes. The dropdown is created from an array in the code behind. I will post the snips of code below. The dropdown binds as expected until these events.
Any assistance in why this does not work would be awesome.
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="tbl_PreRegistration"
Width="100%" CssClass="c2wForm" DefaultMode="Edit">
<EditItemTemplate>
<asp:DropDownList ID="stateDDL" runat="server" OnSelectedIndexChanged="State_DDL_SelectedIndexChanged"
CausesValidation="false" AutoPostBack="true">
</asp:DropDownList>
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" CssClass="button blue" />
<asp:LinkButton ID="btnReset" runat="server" CausesValidation="False"
Text="Cancel" CssClass="button white" OnClick="btnReset1_Click" />
</ContentTemplate></asp:UpdatePanel>
</EditItemTemplate>
</asp:FormView>
CODE BEHIND:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
populateStateDDL("stateDDL", "CA");
}
}
protected void populateStateDDL(string DDL_ID, string getCurrentValue)
{
DropDownList strDDL_ID = (DropDownList)FormView1.FindControl(DDL_ID);
ArrayList states = new ArrayList();
strDDL_ID.DataValueField = "Value";
strDDL_ID.DataTextField = "Text";
strDDL_ID.DataSource = formating.GetAllStates();
strDDL_ID.DataBind();
strDDL_ID.SelectedValue = getCurrentValue.ToUpper();
}
you need to create the control every time, not just when postback = false. the control should be rendered in the Init event so that it can then be wired into viewstate and all that other webforms stuff.
here is some pseudo-code
private DropDownList ctrl;
protected override void Init(EventArgs e)
{
base.Init(e);
ctrl = new DropDownList
{
Id = "name of control",
DataValueField = "Value",
DataTextField = "Text"
};
Controls.Add(ctrl);
}
protected override void Load(EventArgs e)
{
base.Load(e);
if(ispostback) return;
ctrl.DataSource = GetData();
DataBind();
}

Categories