I have tried to write a sample program to display the calendar only when the user selects the particular row in a data grid and also retrieve the value in the text box.I have tried possible ways but so far have not been able to get the correct way to have
it worked.Please help
I am aware that the text box vaue can be retrieved by using findcontrol in the row data bound but not able to acheive enabling the calendar only when the user opts for the linkbutton.Thanks
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Partial Class _Default
Inherits System.Web.UI.Page
Public Function GetCustomMadeDataTable() As DataTable
'Create a new DataTable object
Dim objDataTable As New System.Data.DataTable()
'Create three columns with string as their type
objDataTable.Columns.Add("ISBN", GetType(String))
objDataTable.Columns.Add("Title", GetType(String))
objDataTable.Columns.Add("Publisher", GetType(String))
objDataTable.Columns.Add("Year", GetType(String))
Dim dcPk As DataColumn() = New DataColumn(0) {}
dcPk(0) = objDataTable.Columns("ISBN")
objDataTable.PrimaryKey = dcPk
objDataTable.Columns("ISBN").AutoIncrement = True
objDataTable.Columns("ISBN").AutoIncrementSeed = 1
'Adding some data in the rows of this DataTable
Dim dr As DataRow
For i As Integer = 1 To 10
dr = objDataTable.NewRow()
dr(1) = "Title" & i.ToString()
dr(2) = "Publisher" & i.ToString()
dr(3) = "12/12/200" & i.ToString()
objDataTable.Rows.Add(dr)
Next
Session("strTemp") = objDataTable
Return objDataTable
End Function
Protected Sub Cal1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cal As Calendar = DirectCast(sender, Calendar)
Dim text1 As TextBox = DirectCast(DirectCast(cal.Parent.Parent, GridViewRow).FindControl("text1"), TextBox)
'text1.Text = cal1.value.ToShortDateString()
text1.Text = cal.SelectedDate.ToShortDateString()
cal.Visible = False
End Sub
Protected Sub Page_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreLoad
Dim dt As DataTable
dt = GetCustomMadeDataTable()
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
Protected 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
Dim LinkBut As LinkButton = DirectCast(e.Row.FindControl("LinkButton1"), LinkButton)
Dim cal As Calendar = DirectCast(e.Row.FindControl("Cal1"), Calendar)
'If user has selected the particular record enable the calendar for that row alone
End If
End Sub
End Class
Marked as answer by Karthick_pals on Jun 03, 2012 02:36 AM
Karthick_pal...
Member
1 Points
15 Posts
Enable Calendar only when user inputs link button
May 21, 2012 02:56 PM|LINK
Hi,
I have tried to write a sample program to display the calendar only when the user selects the particular row in a data grid and also retrieve the value in the text box.I have tried possible ways but so far have not been able to get the correct way to have it worked.Please help
I am aware that the text box vaue can be retrieved by using findcontrol in the row data bound but not able to acheive enabling the calendar only when the user opts for the linkbutton.Thanks
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Cal_Run.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField HeaderText="Title" /> <asp:TemplateField HeaderText="Year"> <ItemTemplate> <asp:TextBox ID="Text1" runat="server" AutoPostBack="True" ></asp:TextBox> <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" CommandName="Pick">PickDate...</asp:LinkButton> <asp:Calendar ID="Cal1" runat="server" onselectionchanged="Cal1_SelectionChanged" Visible="False"></asp:Calendar> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>Source Code
Imports System.Data Imports System.Configuration Imports System.Collections Imports System.Web Imports System.Web.Security Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports System.Web.UI.HtmlControls Partial Class _Default Inherits System.Web.UI.Page Public Function GetCustomMadeDataTable() As DataTable 'Create a new DataTable object Dim objDataTable As New System.Data.DataTable() 'Create three columns with string as their type objDataTable.Columns.Add("ISBN", GetType(String)) objDataTable.Columns.Add("Title", GetType(String)) objDataTable.Columns.Add("Publisher", GetType(String)) objDataTable.Columns.Add("Year", GetType(String)) Dim dcPk As DataColumn() = New DataColumn(0) {} dcPk(0) = objDataTable.Columns("ISBN") objDataTable.PrimaryKey = dcPk objDataTable.Columns("ISBN").AutoIncrement = True objDataTable.Columns("ISBN").AutoIncrementSeed = 1 'Adding some data in the rows of this DataTable Dim dr As DataRow For i As Integer = 1 To 10 dr = objDataTable.NewRow() dr(1) = "Title" & i.ToString() dr(2) = "Publisher" & i.ToString() dr(3) = "12/12/200" & i.ToString() objDataTable.Rows.Add(dr) Next Session("strTemp") = objDataTable Return objDataTable End Function Protected Sub Cal1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Dim cal As Calendar = DirectCast(sender, Calendar) Dim text1 As TextBox = DirectCast(DirectCast(cal.Parent.Parent, GridViewRow).FindControl("text1"), TextBox) 'text1.Text = cal1.value.ToShortDateString() text1.Text = cal.SelectedDate.ToShortDateString() cal.Visible = False End Sub Protected Sub Page_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreLoad Dim dt As DataTable dt = GetCustomMadeDataTable() GridView1.DataSource = dt GridView1.DataBind() End Sub Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) End Sub Protected 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 Dim LinkBut As LinkButton = DirectCast(e.Row.FindControl("LinkButton1"), LinkButton) Dim cal As Calendar = DirectCast(e.Row.FindControl("Cal1"), Calendar) 'If user has selected the particular record enable the calendar for that row alone End If End Sub End Class