I'm sure this is simple, I need to disable a button on my html page if a variable doesn't have a value from inside the code.
<asp:Linkbutton ID="eastMinute" runat="server" CssClass="btn btn-default" Text="Open Once" Width="95%" />
I've found several options for disabling the button if there is no text for form validation but I haven't found a way to actually connect the VB code with my ASPX page...
My thought was:
'Logic for button enable
If (username.Length < 0) Then eastOpen_Click.Enabled = False
Else eastOpen_Click.Enabled = True
But with that I get an argument not specified error.
Based on this from your sample:
<asp:Linkbutton ID="eastMinute" runat="server" CssClass="btn btn-default" Text="Open Once" Width="95%" />
Your button name is eastMinute, so your code should be:
'Logic for button enable
If (username.Length < 0) Then eastMinute.Enabled = False
Else eastMinute.Enabled = True
The eastOpen_Click sounds like an event handler; that is a function and therefore does not have an Enabled property.
You can also write it in one line like:
eastMinute.Enabled = (username.Length < 0)
What I ended up having to do for this to work properly was do the logic for if the value was passed or not and then add an attribute manually to my button using Attributes.Add().
'Logic for button enable
If (username Is Nothing) Then eastOpen.Attributes.Add("disabled", "disabled") Else
Related
I am currently working on a project in vb.net and I am having trouble getting my custom validator to work on a radcombobox. When I run my application and test the validation it is not being invoked, causing my application to break. I am not looking into client side validation, just server side. Below is the code I have been using.
Aspx code
<td>
<telerik:RadComboBox ID="radcomboboxListTimeZone" runat="server" BorderColor="#bbe0ef"
BorderWidth="2px" Width="400px" MaxHeight="200px"
DataSourceID="ObjectDataSourceListTimeZones"
DataTextField="DisplayName" DataValueField="StandardName" SelectedValue='<%#
Bind("TimeZoneStandardName")%>'>
</telerik:RadComboBox>
<asp:CustomValidator ID="CustomValidatorTimeZone" runat="server" ErrorMessage="Please select a
valid Time Zone Standard Name" ControlToValidate="radcomboboxListTimeZone"
OnServerValidate="CustomValidatorListTimeZone_ServerValidate">•</asp:CustomValidator>
</td>
Server side code
Protected Sub CustomValidatorListTimeZone_ServerValidate(ByVal source As Object, ByVal args As
ServerValidateEventArgs)
Dim radcomboboxListTimeZone As RadComboBox =
CType(FormViewTimeZones.FindControl("radcomboboxListTimeZone"), RadComboBox)
Dim selectedValue As String = radcomboboxListTimeZone.SelectedValue
If selectedValue = "Coordinated Universal Time" Then
args.IsValid = False
Else
args.IsValid = True
End If
End Sub
Any help would be much appreciated.
I managed to get the server side custom validation working. To do this I created a validation group
<asp:ValidationSummary ID="ValidationSummaryEditItem" runat="server" ValidationGroup="TimeZone" />
and set
CausesValidation="true"
on the edit button and added
ValidationGroup="TimeZone"
to both the edit button and the custom validator
I have two columns in a GridView where every cell of 1 column is hyperlinked.
<asp:GridView ID="gvBlogList" runat="server" DataKeyNames="Id"
AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLinkField DataTextField="id"
HeaderText="Blog id" NavigateUrl="detailspage.aspx" />
</ItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
How can I get cell value in 2nd page as a session variable?
Thanks!
As per the what I understand from your question, you want to catch the value of the cell that is clicked, store it in session and make it accessible on the page that have redirect to. This is possible and you can try a flow like this :
You can use the GridView's inbuilt method onRowCommand in this case to generate a server side event and you can then identify which cell was clicked and lastly, you can get its value by index. The below code is a demonstration that you can try and manipulate as per your requirement. This is a sample where a Linkbutton is used in Gridview and on the click of it, its value is fetched.
You can also try to use the CommandName and CommandArgument properties if you want to assign that particular control a value that you may use at the server side.
protected void Grd1_RowCommand(object sender, GridViewCommandEventArgs e)
{
LinkButton lnk = (LinkButton)e.CommandSource;
string value = lnk.Text;
}
And for storing it in Session, you just have to make use of Session["yourVarName"] = value;. And this value can be accessed on the other page.
Hope this helps.
I've seen many questions around this one, but none hitting it directly.
I put a listbox on a page and populate it with three items from an Access database. I have a button on that page that will extract several values including the selected item from the listbox. Or I want to anyway.
I can see the item selected in windows (highlighted) when I click the button, but when I try to select it no item is available as selected in the listbox. The ListBox1.SelectedIndex is alway -1.
Here is the code from the page:
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Text="List1" />
<asp:ListItem Text="List2" />
<asp:ListItem Text="List3" />
</asp:ListBox>
Is there a property missing?
Here is the code from the code behind page:
Public Function getDept() As String
Dim dept As String
If ListBox1.SelectedIndex > -1 Then
dept = ListBox1.SelectedItem.Text
Else
dept = "CMS"
End If
Return dept
End Function
Please help, I have until about noon to figure this out.
There may some reasons:
1- Check if your page's viewstate is true.
2- Call your method after Page_Load event.
Where do you call the function?
Consider that you should call it after Page_Load event. Also your viewstate
New to VB.net, and trying to re-factor an 'old-skool' ASP page where all the page logic is happening on the .aspx page itself, to code-behind. Basically, I have a button that has a state, either on or off. If on, I set a hidden field to 1, if off, I set it to 0 (the default when a user visits the page).
The goal is to simply change the message I am sending to the user.
Have the following relevant code in MyPage.aspx:
<asp:HiddenField ID="hfldState" runat="server" Visible="false" Value="0" />
<div id="mainContent">
<asp:Literal ID="lblMessage" runat="server"
Visible="false" />
<asp:DataList ID="dlList" runat="server"
DataSourceID="sdsList"
DataKeyField="Entry No_"
RepeatLayout="Flow">
<ItemTemplate>
<div>
<asp:HyperLink ID="hlCurriculum" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "File Path") %>'
ToolTip='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
Target="_blank"
Style="font-weight: bold;">
</asp:HyperLink>
</div>
</ItemTemplate>
</asp:DataList>
</div>
<asp:SqlDataSource ID="sdsList" runat="server"
ConnectionString="..."
SelectCommand="SELECT [Entry No_], [Title], [File Path] FROM [Table] WHERE ([State] = #State)">
<SelectParameters>
<asp:ControlParameter ControlID="hfldState" Name="State" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
And the following in the Page_Load Sub:
If Page.IsPostBack Then
' Check for results
dlList.DataBind()
If dlList.Items.Count > 0 Then
' Results, display them
lblMessage.Text = "<h3>Results</h3>"
lblMessage.Visible = True
Else
' No results
lblMessage.Text = "<p>No Results</p>"
lblMessage.Visible = True
End If
Else
' user has not clicked anything
lblMessage.Text = "<p>Click button!</p>"
End If
The above code produces the following effect: when I click the button, setting the state to 1 and the page reloads, in Page_Load, I am getting zero results from the If dlList.Items.Count > 0 Then check, and thus am showing the 'No results' message, but the actual asp:DataList on the MyPage.aspx page is returning results... and then if I click the button again, setting it back to 0, in Page_Load, the datalist is now returning results, so I set the text to 'Results', but again, the control on the MyPage.aspx page behaves correctly, and shows no results, as expected. Now keep in mind that the above works perfectly in terms of checking the number of results if I move the If dlList.Items.Count > 0 Then page logic out of Page_Load and back to the MyPage.aspx file, it all works fine (i.e. results when state = 1, none when state = 0)
Any ideas?
I dont see a check in your page load for a postback:
If Not IsPostBack
'code
dList.DataBind() 'here is where you want to bind the data...
end if
The issue related to my ignorance of the page lifecycle in VB.net. Solution was to adjust the visibility of the asp:Literal, asp:DataList, set values etc in Page_PreRenderComplete. Basically, the asp:DataList wasn't being set in Page_Load, as expected, so doing any kind of logic in Page_Load based on the item count didn't make any sense.
Just as the title says. I have code that disables all controls that runat=server. Its as follows
Dim c As Control
For Each c In pc
If c.HasControls Then DisableAllControls(c.Controls)
If c.GetType.ToString.ToLower.IndexOf("webcontrols.dropdownlist") > -1 Then
DirectCast(c, DropDownList).Enabled = False
ElseIf c.GetType.ToString.ToLower.IndexOf("webcontrols.textbox") > -1 Then
DirectCast(c, TextBox).Enabled = False
ElseIf c.GetType.ToString.ToLower.IndexOf("webcontrols.radiobuttonlist") > -1 Then
DirectCast(c, RadioButtonList).Enabled = False
ElseIf c.GetType.ToString.ToLower.IndexOf("webcontrols.radiobutton") > -1 Then
DirectCast(c, RadioButton).Enabled = False
ElseIf c.GetType.ToString.ToLower.IndexOf("webcontrols.button") > -1 Then
DirectCast(c, Button).Enabled = False
End If
Next
But I have a couple of href's in there that i want to disable also. I know they dont runat server, so how can i catch these?
You can add runat="server":
<a runat="server" href="..." ></a>
It is the only way to maintain controls using server side code.
You can add runat="server" to HTML controls too. Without it you won't be able to access the control on the server side and you won't be able to disable it.
That code looks invalid (If c.HasControls Then DisableAllControls(c.Controls) has no matching End If) but perhaps VB.NET has added inline If syntax like that without me realising.
Anyway, as far as disabling all runat="server" controls, you should be able to just do this:
For Each c as WebControl In pc.OfType(Of WebControl)()
' Put your recursive call here as before
c.Enabled = False
Next
Now, to "disable" those other elements, you can either add runat="server" to them (perhaps not even possible if you have generated HTML), or you can use some JavaScript. I'm going to assume by disable you mean hide in the case of <a> tags?
jQuery makes this easy, with a sample script being something like:
$(document).load(function() {
$('a').hide();
});
or:
/* hides all a tags under an element with class="someClass" */
$(document).load(function() {
$('.someClass a').hide();
});
You could then have your code render this script, using something like this in your Page:
Dim script as String = "" /* your javascript here */
Me.ClientScript.RegisterClientScriptBlock(Me.GetType(), "HideTagsScript", script)