Enable PostBack for a ASP.NET User Control

Posted by Steven on Stack Overflow See other posts from Stack Overflow or by Steven
Published on 2010-04-30T19:12:40Z Indexed on 2010/04/30 19:27 UTC
Read the original article Hit count: 377

Filed under:
|
|
|

When I click my "Query" the values for my user controls are reset.

How do I enable PostBack for my user control?

myDatePicker.ascx

<%@ Control Language="vb" CodeBehind="myDatePicker.ascx.vb"
    Inherits="Website.myDate" AutoEventWireup="false" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" 
             tagprefix="asp" %>

<asp:TextBox ID="DateTxt" runat="server" ReadOnly="True" />
<asp:Image ID="DateImg" runat="server" ImageUrl="~/Calendar_scheduleHS.png"
              EnableViewState="True" EnableTheming="True" />

<asp:CalendarExtender ID="DateTxt_CalendarExtender" runat="server" Enabled="True"
                      TargetControlID="DateTxt" PopupButtonID="DateImg"
                      DefaultView="Days" Format="ddd MMM dd, yyyy"
                      EnableViewState="True"/>

myDatePicker.ascx

Partial Public Class myDate
    Inherits System.Web.UI.UserControl

    Public Property SelectedDate() As Date?
        Get
            Dim o As Object = ViewState("SelectedDate")
            If o = Nothing Then
                Return Nothing
            End If
            Return Date.Parse(o)
        End Get
        Set(ByVal value As Date?)
            ViewState("SelectedDate") = value
        End Set
    End Property
End Class

Default.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb"
         Inherits="Website._Default" EnableEventValidation="false"
         EnableViewState="true" %>

<%@ Register TagPrefix="my" TagName="DatePicker" Src="~/myDatePicker.ascx" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
             TagPrefix="ajax" %>
<%@ Register Assembly="..." Namespace="System.Web.UI.WebControls"
             TagPrefix="asp" %>

<!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">
...
<body>
<form id="form1" runat="server">
<div class="mainbox">
      <div class="query">
          Start Date<br />
          <my:DatePicker ID="StartDate" runat="server" EnableViewState="True" />
          End Date
          <br />
          <my:DatePicker ID="EndDate" runat="server" EnableViewState="True" />
        ...
        <div class="query_buttons">
            <asp:Button ID="Button1" runat="server" Text="Query" />
        </div>
 </div>
 <asp:GridView ID="GridView1" ... >
 </form>
 </body>
 </html>

Default.aspx.vb

Imports System.Web.Services
Imports System.Web.Script.Services
Imports AjaxControlToolkit

Protected Sub Page_Load(ByVal sender As Object, _
                        ByVal e As EventArgs) Handles Me.Load

End Sub

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(ByVal sender As Object, _
                                ByVal e As EventArgs) Handles Button1.Click
        GridView1.DataBind()
    End Sub
End Class

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about vb.net