Need some help with some coding adding to my asp,net page.... as im very new to vb.net, i would love some help from you experts.
What i want to do is when the page loads for the first time (not during postback) i want to set the default value within my checkbox to equal = "ALL" and then i want the gridview to be updated using the value from the checkbox.
so currently when the page loads... the users needs to select and item from the checkboxlist to then update the gridview, but i want to set the default value to ALL
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class PatientList
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If TextBox2.Text = "ALL" Then GridView1.DataBind()
End Sub
Shared Function aspx() As String
Throw New NotImplementedException
End Function
Private Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
End Sub
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(9).Text = "1" Then
e.Row.BackColor = System.Drawing.Color.Cornsilk
Else
End If
End If
e.Row.Cells(9).Visible = False
End Sub
Private Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged
Dim name As String = ""
For i As Integer = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected Then
name &= CheckBoxList1.Items(i).Text + ","
End If
Next
TextBox2.Text = name
End Sub
End Class
sam233
Member
36 Points
218 Posts
On PageLoad - set default value in CheckBoxList
Jan 17, 2013 02:33 PM|LINK
Need some help with some coding adding to my asp,net page.... as im very new to vb.net, i would love some help from you experts.
What i want to do is when the page loads for the first time (not during postback) i want to set the default value within my checkbox to equal = "ALL" and then i want the gridview to be updated using the value from the checkbox.
so currently when the page loads... the users needs to select and item from the checkboxlist to then update the gridview, but i want to set the default value to ALL
below is my asp.net code
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Master.Master" CodeBehind="PatientList.aspx.vb" Inherits="Chaplaincy.PatientList" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <form id="form1" runat="server"> <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </asp:ToolkitScriptManager> <asp:UpdatePanel ID="updatepanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label1" runat="server" Text="Faith Group:" CssClass="newsSummary"></asp:Label> <asp:TextBox ID="TextBox2" runat="server"> </asp:TextBox> <asp:PopupControlExtender ID="TextBox2_PopupControlExtender" runat="server" Enabled="True" ExtenderControlID="" TargetControlID="TextBox2" PopupControlID="Panel1" OffsetY="22"> </asp:PopupControlExtender> <asp:Panel ID="Panel1" runat="server" Height="116px" Width="245px" BorderStyle="Solid" BorderWidth="2px" Direction="LeftToRight" ScrollBars="Auto" BackColor="#CCCCCC" Style="display: none"> <%--<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="FaithGroup" DataTextField="DESCRIPTION" DataValueField="DESCRIPTION"> </asp:DropDownList>--%> <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="FaithGroup" DataTextField="DESCRIPTION" DataValueField="DESCRIPTION" AppendDataBoundItems="True"> <asp:ListItem value="ALL" Selected="True">ALL</asp:ListItem> </asp:CheckBoxList> </asp:Panel> </ContentTemplate> </asp:UpdatePanel> <asp:Label ID="Label2" runat="server" CssClass="newsSummary" Text="Surname:"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label3" runat="server" CssClass="newsSummary" Text="Unitno:"></asp:Label> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <asp:ImageButton ID="ImageButton1" runat="server" Height="21px" ImageAlign="Bottom" ImageURL="~/Images/search.png" Width="27px" /> <asp:SqlDataSource ID="FaithGroup" runat="server" ConnectionString="<%$ ConnectionStrings:NCRSDBConnectionString %>" SelectCommand=" select DESCRIPTION FROM ChaplaincyPatientList group by DESCRIPTION order by DESCRIPTION"></asp:SqlDataSource> <div style="height: 450px; overflow-y: scroll"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="IDENTIFIER" DataSourceID="NCRSDB" GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" PageSize="20" AllowSorting="True"> <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle> <Columns> <asp:BoundField DataField="IDENTIFIER" HeaderText="Patient Number" ReadOnly="True" SortExpression="IDENTIFIER"/> <asp:BoundField DataField="FORENAME" HeaderText="First Name" SortExpression="FORENAME" /> <asp:BoundField DataField="SURNAME" HeaderText="Surname" SortExpression="SURNAME" /> <asp:BoundField DataField="DESCRIPTION" HeaderText="Faith" SortExpression="DESCRIPTION" /> <asp:BoundField DataField="RATING" HeaderText="Rating" SortExpression="RATING" /> <asp:BoundField DataField="disdate" HeaderText="Latest Discharge" SortExpression="disdate" /> <asp:BoundField DataField="deathmarker" HeaderText="deathmarker" SortExpression="deathmarker" Visible="False" > <ItemStyle HorizontalAlign="Center" /> </asp:BoundField> <asp:BoundField DataField="SOURCE" HeaderText="Referral Source" SortExpression="SOURCE" /> <asp:BoundField DataField="datedeath" HeaderText="Date of death" SortExpression="datedeath" /> <asp:BoundField DataField="Vlist" HeaderText="Vlist" SortExpression="Vlist" > </asp:BoundField> </Columns> <PagerStyle CssClass="pgr"></PagerStyle> </asp:GridView> </div> <asp:SqlDataSource ID="NCRSDB" runat="server" ConnectionString="<%$ ConnectionStrings:NCRSDBConnectionString %>" SelectCommand="SELECT c.IDENTIFIER , c.FORENAME , c.SURNAME , c.DESCRIPTION , c.RATING , ct.disdate , ct.deathmarker , ct.datedeath , c.source , p.count as Vlist FROM [AR_20120209_ChaplaincyPatientList] c left outer join ChaplaincyTemp ct on c.identifier = ct.identifier left outer join PSAG.dbo.PICS_Bedspace_interim1 AS p on c.IDENTIFIER = LEFT(p.[Reg No], 7) collate database_default where (c.description IN (select Param from [dbo].[fn_MVParamChar](@description,',')) OR @description = 'ALL') and (c.surname like @surname + '%' or @surname IS NULL or @surname = '') and (c.identifier like @unitno + '%' or @unitno is null or @unitno = '') order by c.id desc"> <SelectParameters> <asp:ControlParameter ControlID="CheckBoxList1" Name="description" PropertyName="SelectedValue"/> <asp:ControlParameter ControlID="TextBox1" Name="surname" PropertyName="Text" DefaultValue=" " /> <asp:ControlParameter ControlID="TextBox3" DefaultValue=" " Name="unitno" PropertyName="Text" /> </SelectParameters> </asp:SqlDataSource> </form> </asp:Content>vb.net code
Imports System.Collections.Generic Imports System.Linq Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Public Class PatientList Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If TextBox2.Text = "ALL" Then GridView1.DataBind() End Sub Shared Function aspx() As String Throw New NotImplementedException End Function Private Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand End Sub Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then If e.Row.Cells(9).Text = "1" Then e.Row.BackColor = System.Drawing.Color.Cornsilk Else End If End If e.Row.Cells(9).Visible = False End Sub Private Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged Dim name As String = "" For i As Integer = 0 To CheckBoxList1.Items.Count - 1 If CheckBoxList1.Items(i).Selected Then name &= CheckBoxList1.Items(i).Text + "," End If Next TextBox2.Text = name End Sub End ClassPashBash
Member
371 Points
81 Posts
Re: On PageLoad - set default value in CheckBoxList
Jan 25, 2013 11:47 AM|LINK
In your code for PageLoad event you are checking the wrong control.
Your code is checking TextBox2.Text
But your TextBox2.Text is nothing
You need to check CheckBoxList1.Items[0] (sorry not sure if this is correct for VB)
In other words check your CheckBoxList for the first item and if it is "selected" or "checked" then bind data
For your PageLoad code
try
if (this.IsPostBack = false && CheckBoxList1.Items[0].Selected) Then GridView1.DataBaind()
Sorry if code for VB is not correct, I haven't used VB in a long time, I write in C#