vb.net label won't show session variable - vb.net

Why does this label show up in my rendered HTML??
<asp:Label ID="Label1" runat="server" Visible='<%# false %>' Text='HERE' />
I can set the visible to false in the code behind and then it will go away, but what I really want is to set visibility depending on a session variable existing, like this:
<asp:Label ID="Label2" runat="server" Visible='<%# isnothing(Session("Login_ID")) %>' Text='THERE' />
I could swear I've don't this before, but today it isn't working.

I think it's because you aren't telling the Page to bind. Other controls can be bound automatically, such as DataGrids, but you have to tell the page to do it manually.
I think I used to do it like this, somewhere where it made sense (e.g., PreRender lifecycle event):
' Me is the Page; call this from the page to bind its controls that use the <%# %> syntax
Me.DataBind()
Or, since this is relatively static, use the following in the aspx (equals sign for assignment, rather than # for binding):
<asp:Label ID="Label2" runat="server" Visible='<%= isnothing(Session("Login_ID")) %>' Text='THERE' />

Related

.aspx page does not load after adding Validators in VB.Net

I have an aspx page with a few FormViews and an updateable GridView that I would like to add validation for. The fields that I am trying to validate are also using the CalendarExtender from the AJAX Control Toolkit by the way. I am trying to use the RequiredFieldValidator and RangeValidator for this. The problem is I don't see any errors from within VisualStudio 2015 while adding the validators. But when I run debug mode and click the button to open a FormView that contains validation I get a 500 error without any useful information to find/fix my problem.
I first tried adding both of the validators at once but after having problems, I am just trying to get the RequiredFiledValidator working. But I still need to know how to get the RangeValidator to work properly. For the RangeValidator, I want to validate a start date of no less than today and an end date that is greater than the start date. I tried using all of the examples that I found on these pages:
[1] https://www.c-sharpcorner.com/UploadFile/17e8f6/requiredfieldvalidator-control-in-Asp-Net/
[2] https://asp.net-tutorials.com/validation/required-field-validator/
[3] http://www.java2s.com/Tutorial/ASP.NET/0160__Validation/UseaspRangeValidatortocheckthevaluerangeinanasptextbox.htm
[4] http://www.informit.com/articles/article.aspx?p=101137&seqNum=5
[5] https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.rangevalidator.minimumvalue?view=netframework-4.8
[6] https://www.tutorialspoint.com/asp.net/asp.net_validators.htm
Here is an example of one of the FormViews that I am trying to validate. In the RangeValidator I just have hard coded Max and Min values but I eventually want to have the Min value to be the same value that is in the begin date text box plus day:
<asp:FormView ID="fvNotification" runat="server" Visible="False" DefaultMode="Insert" GridLines="Both" DataSourceID="SqlUserDetails">
<InsertItemTemplate>
<asp:Table ID="Table1" runat="server">
<asp:TableHeaderRow>
<asp:TableHeaderCell>Notification</asp:TableHeaderCell>
<asp:TableHeaderCell>Filter 1</asp:TableHeaderCell>
<asp:TableHeaderCell>Filter 2</asp:TableHeaderCell>
<asp:TableHeaderCell>Begin Date</asp:TableHeaderCell>
<asp:TableHeaderCell>End Date</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>
<asp:DropDownList ID="ddNotification" runat="server" DataSourceID="SqlNotifications" DataTextField="Name" DataValueField="Name" ></asp:DropDownList></asp:TableCell>
<asp:TableCell>
<asp:DropDownList ID="ddFilterInsrt" runat="server" DataSourceID="SqlFilters1" DataTextField="Filter" DataValueField="Filter" ></asp:DropDownList></asp:TableCell>
<asp:TableCell>
<asp:DropDownList ID="ddFilterInsrt2 runat="server" >
<asp:ListItem>*</asp:ListItem>
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
<asp:ListItem>D</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="TextBoxDateBgnInsrt" runat="server" autocomplete="Disabled" ></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server" BehaviorID="TextBoxDateBgnInsrt_CalendarExtender" TargetControlID="TextBoxDateBgnInsrt" ID="TextBoxDateBgnInsrt_CalendarExtender"></ajaxToolkit:CalendarExtender>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="TextBoxDateEndInsrt" runat="server" autocomplete="Disabled" ></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server" BehaviorID="TextBoxDateEndInsrt_CalendarExtender" TargetControlID="TextBoxDateEndInsrt" ID="TextBoxDateEndInsrt_CalendarExtender"></ajaxToolkit:CalendarExtender>
<asp:RangeValidator runat="server" id="rngDate" controltovalidate="TextBoxDateEndInsrt" type="Date" MaximumValue='09/20/2011' MinimumValue="09/01/2011" errormessage="Please enter a valid date within 2006!" display="Dynamic"/>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" CausesValidation="True" />
<asp:Button ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" OnClick="InsertCancelButton_Click1" />
<asp:Button ID="InsertClearEndDateButton" runat="server" Text="Clear End Date" OnClick="InsertClearEndDateButton_Click"/>
</InsertItemTemplate>
</asp:FormView>
I am expecting the FormView to be visible and validate after clicking the button that make the FormView visible but instead I am getting a 500 error.

ASP.NET Gridview

I formed the table shown below picture 1 with GridView but there is a something which I want and I explain that picture 2 (İ didn't use GridView there).
When the data come from database to column of Durum, if it is Aktif, it shows Aktif Button or if it is Pasif, it shows Pasif Button.
How can do this?
You can do pretty much whatever you want assuming you don't use default column binding but rather you define your own.
<asp:GridView ... AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
this is where your template can do
pretty much anything including conditionals
<asp:Button runat="server"
ForeColor="<%# Bind("column")=="value" ? "Red" ? "Blue" %> />
Note that dynamic binding can be applied to any property. You could have two different buttons then and dynamically bind their Visible property so that they are visible or hidden depending on a value of one of dataset columns.
More on dynamic templates in multiple tutorials, e.g.
https://msdn.microsoft.com/en-us/library/bb288032.aspx
Add this TemplateField Code in your GridView:
<asp:TemplateField HeaderText="Durum">
<ItemTemplate>
// Bind your durum column from database in Eval
<asp:Label ID="lblDurum" Visible="false" runat="server" Text='<% #Eval("durum") %>'></asp:Label>
<asp:Button ID="btnAktif" Visible="false" runat="server" Text="Aktif" />
<asp:Button ID="btnPasif" Visible="false" runat="server" Text="Pasif" />
</ItemTemplate>
</asp:TemplateField>
Code Behind
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button Aktif = e.Row.FindControl("btnAktif") as Button;
Button Pasif = e.Row.FindControl("btnPasif") as Button;
string Durum = ((Label)e.Row.FindControl("lblDurum")).Text;
if (Durum=="Aktif")
Aktif.Visible = true;
else
Pasif.Visible = true;
}
}
Note: Don't forget to add OnRowDataBound in GrindView <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >

Run asp.net required field validation only if at least one of the fields in the group is completed

I have a form on an asp.net (VB) page which has 2 sections.
All of section 1 is always required, but section 2 is optional. However, if section 2 is started, then all fields are required.
I'm stuck as to the logic I need to create this validation.
Here's my code:
<form runat="server">
<div>
<asp:TextBox runat="server" ID="field1a" Text="Name" />
<asp:TextBox runat="server" ID="field1b" Text="City" />
<asp:RequiredFieldValidator runat="server" ID="field1aVal" Text="Name Required" ControlToValidate="field1a" InitialValue="Name" />
<asp:RequiredFieldValidator runat="server" ID="field1bVal" Text="City Required" ControlToValidate="field1b" InitialValue="City" />
</div>
<div>
<asp:TextBox runat="server" ID="field2a" Text="Name" />
<asp:TextBox runat="server" ID="field2b" Text="City" />
<asp:RequiredFieldValidator runat="server" ID="field2aVal" Text="Name Required" ControlToValidate="field2a" InitialValue="Name" />
<asp:RequiredFieldValidator runat="server" ID="field2bVal" Text="City Required" ControlToValidate="field2b" InitialValue="City" />
</div>
<asp:button runat="server" ID="btnSubmit" Text="Submit" OnClick="submitForm" />
</form>
Id say use required fields on section 1, but for section two use a custom validator. Something like this.
<asp:CustomValidator ID="CustomValidator2" runat="server"
Display="Dynamic" EnableClientScript="False"
ErrorMessage="You must select one checkbox or click all offices. " OnServerValidate="OfficeVaildator">
</asp:CustomValidator>
Then in code behind do your logic
Something like this
Sub OfficeVaildator(ByVal source As Object, ByVal args As ServerValidateEventArgs)
If OfficeCheckBoxs.Checked Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
Obviously that is really simple. You would want to check if the user altered section 2 in anyway, and if he did make him complete all fields to return your server validation arguments true. If he didnt alter section 2 or give it any data right a method that checks that and set your validation to true to allow him to proceed.

Make UserControl Visible after Logging In

I have a popup login usercontrol in a masterpage, once logged in I want another usercontrol on a content page to appear automatically, currently it only appears if I manually refresh the page. How can this be accomplished. Thanks.
MasterPage - > login usercontrol
View.aspx - > bookmark usercontrol (should appear after logging in)
login usercontrol in Master Page
<asp:UpdatePanel ID="login" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlLogin" runat="server">
<asp:TextBox ID="txtpassword" runat="server" TextMode="Password </asp:TextBox>
<asp:Button ID="btbookmark" runat="server" Text="Login" OnClick="btbookmark_Click"/>
</asp:Panel>
<asp:Panel ID="pnlData" runat="server">Placeholder for bookmark data. Bookmark data goes here.
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
Bookmark usercontrol on View.aspx
<asp:TextBox ID="txtbookmark" runat="server"></asp:TextBox><br />
<asp:Button ID="btmark" runat="server" />
After logging in how can the Bookmark usercontrol on View.aspx appear. Do I need to use an updatepanel?
You could either register it and set the Visible property to false in the code behind, until the user has logged in, and then you set the property to true. You will need to add code to prevent a lot of processing if the user session object doesn't exist.
aspx:
<%# Register TagPrefix="control" TagName="UserBookmarks" Src="~UserBookmarks.ascx" %>
<control:UserBookMarks ID="UserBookmarksCtrl" runat="server" Visible="false" />
code behind:
// If user logged in
UserBookmarksCtrl.Visible = True
Or, you can dynamically create the control, and add it to your page once a user has logged in (i.e. something like Session["user"] has been set).

Can I edit inside of a modal popup?

I have only found posts in which people using gridviews and modal popups need help with editing. However, I am not using a gridview so editing is not as simple as an edit within a gridview. The modal popup disappears when I click on the edit button. Here is what I have so far, but in the codebehind I get an error saying that my modal isn't declared.
In the image below, the little pencil image next to the close button is what I click on to edit the description. When I click on it, the modal disappears, so I cannot edit the text.
<!-- Descriptions -->
<asp:TabPanel ID="tab2" runat="server" HeaderText="Descriptions">
<HeaderTemplate>Descriptions</HeaderTemplate>
<ContentTemplate>
<ul class="info">
<asp:ListView ID="lvDescriptions" runat="server"
DataSourceID="dsAdminMarketingDescriptions" DataKeyNames="MarketingID">
<ItemTemplate>
<li class="item">
<asp:LinkButton ID="ViewDescriptionButton" runat="server"><%#
Eval("Title")%>
</asp:LinkButton>
<asp:ImageButton ID="DeleteDescriptionButton" runat="server"
Style="float:right;" AlternateText=""
ImageUrl="../../images/delete.png" CommandName="Delete"
OnClientClick="return confirm('Are you sure you want to delete this
description?')" />
<asp:Panel ID="ViewDescriptionPanel" runat="server"
CssClass="DescModalPopup">
<div class="PopupHeader">View Description -- <%#Eval("Title") %>
<asp:ImageButton ID="CancelDescriptionButton" runat="server"
ImageUrl="../../images/cancel.png" AlternateText=""
Style="float:right;"/>
<asp:ImageButton ID="EditDescriptionButton" runat="server"
ImageUrl="../../images/edit.png" AlternateText=""
Style="float:right;" CommandName="edit" AutoPostBack="false" />
</div>
<asp:Label ID="Description" runat="server" style="padding:2px;">
<%# Eval("Data")%>
</asp:Label>
</asp:Panel>
<asp:ModalPopupExtender ID="ViewDescriptionModal" runat="server"
BackgroundCssClass="modalBackground" DropShadow="false"
DynamicServicePath="" Enabled="true"
PopupControlID="ViewDescriptionPanel"
TargetControlID="ViewDescriptionButton"
CancelControlID="CancelDescriptionButton">
</asp:ModalPopupExtender>
</li>
</ItemTemplate>
Protected Sub EditDescriptionButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
ViewDescriptionModal.Show()
End Sub
UPDATE: i changed the codebehind, but I still get an error saying ViewDescriptionModal is not declared.
Protected Sub EditDescriptionButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myControl As Control = FindControl("ViewDescriptionModal")
If (Not myControl Is Nothing) Then
ViewDescriptionModal.Show()
Else
'Control not found
End If
End Sub
UPDATE: I have created a second modal popup for edits and changed the Label to a Textbox to pull in information from the database to edit. I added a submit button, but when I click on it, I get an error saying something about it being potentially dangerous.
Does anyone have any experience with A potentially dangerous Request.Form value was detected from the client
I haven't tested this, but you may have luck if you use two modalpopupextenders, and two panels, one for viewing and one for editing.
<asp:ModalPopupExtender ID="ViewDescriptionModal" runat="server"
BackgroundCssClass="modalBackground" DropShadow="false"
DynamicServicePath="" Enabled="true"
PopupControlID="ViewDescriptionPanel"
TargetControlID="ViewDescriptionButton"
CancelControlID="CancelDescriptionButton">
</asp:ModalPopupExtender>
<asp:ModalPopupExtender ID="EditDescriptionModal" runat="server"
BackgroundCssClass="modalBackground" DropShadow="false"
DynamicServicePath="" Enabled="true"
PopupControlID="EditDescriptionPanel"
TargetControlID="EditDescriptionButton">
</asp:ModalPopupExtender>
<asp:Panel ID="ViewDescriptionPanel" runat="server" ... </asp:panel>
<asp:Panel ID="EditDescriptionPanel" runat="server" ... </asp:Panel><code>
try in your image button autopostback="false"