Changing the properties of a control within an emptydatatemplate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ranger979
    New Member
    • Nov 2008
    • 2

    Changing the properties of a control within an emptydatatemplate

    I am using the emptydatatempla te for a gridview control. I have a detailsview control within the emptydatatempla te. In the detailsview I have a templatefield that contains an imagebutton control and a textbox control.

    Code:
                <EmptyDataTemplate>
                    &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="ActivityTypeControl_ID"
                        DataSourceID="spListActivityTypeControlByType" DefaultMode="Insert" Height="50px"
                        Width="125px" BackColor="#090AAAA" OnPageIndexChanging="DetailsView1_PageIndexChanging1" BorderStyle="None" GridLines="None" OnItemCommand="DetailsView1_ItemCommand">
                        <Fields>
                           <asp:TemplateField HeaderText="Begin Date" SortExpression="BeginDate">
                                <InsertItemTemplate>
                                    &nbsp;<asp:TextBox ID="txtIBeginDate" runat="server" Text='<%# Bind("BeginDate") %>' Width="90px"></asp:TextBox>
                                    <asp:ImageButton ID="imgbutICalendar1" runat="server" ImageUrl="~/Images/APPTL.ICO" Height="16px" 
                                    Width="16px" OnClick="imgbutCalendar_Click" />
                                </InsertItemTemplate>
                            </asp:TemplateField>
                     </asp:DetailsView>
                </EmptyDataTemplate>
    When the user clicks on the imagebutton I look at the text in the textbox and display a calendar control.

    Code:
        Private ParentControl As Control
        Protected Sub imgbutCalendar_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs)
         Dim txtDate as TextBox
    
         ParentControl = DirectCast(sender, Control).Parent
         txtDate = ParentControl.FindControl("txtIBeginDate")
    
         Calendar.VisibleDate = CDate(txtDate.Text)
         Calendar.Visible = True  
        End Sub
    This displays the calendar with the month I would expect. However, I then want to change the text property of my textbox (txtIBeginDate) with the date that was selected from the calendar control.

    I am doing this using the calendar's selectionchange d event using the following code.

    Code:
        Protected Sub Calendar_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calendar.SelectionChanged
            Calendar.Visible = False
            Dim txtDate As TextBox
    
            txtDate = ParentControl.FindControl(Session("txtIBeginDate"))
            txtDate.Text = Calendar.SelectedDate.ToShortDateString
        End Sub
    If I step through my code in debug everything seems to get set correctly. However, when my page is redisplayed the value in my textbox is left unchanged.

    Can anyone explain what I am doing wrong?
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    Code:
    Dim txtDate As TextBox 
    txtDate = ParentControl.FindControl(Session("txtIBeginDate")) 
    txtDate.Text = Calendar.SelectedDate.ToShortDateString
    I see here you are dimming txtDate as TextBox... in your aspx code do you have something like this
    Code:
    <asp:textbox runat="server" id="txtDate"></asp:textbox>
    If so there is no need to dim txtDate as TextBox. i'm not 100% sure this is the problem you are experiencing.. Also another look into things... in your page load are you setting txtDate.text to anything? maybe you need an ispostback check.

    Comment

    Working...