Search code examples
asp.nettwitter-bootstrapuser-controlsrepeater

How to get Text value of UserControl in Repeater Control itemCommand in asp.net?


I have developed usercontrol timePicker. I'm using Bootstrap and Ajaxtoolkit 4 but not using updatepanel

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TimePicker.ascx.cs" Inherits="Controls_TimePicker" %>
    <script type="text/javascript">
        $(function () {
            $('#<%=divTime.ClientID %>').datetimepicker({
                /* language: 'ar-kw',*/
                language: 'en-US',
                maskInput: true,
                format: 'HH:mm PP',
                pickDate: false,
                pickSeconds: false,
                pick12HourFormat: true,
                pickTime: true,
                placement: 'left'
            });
        });
    </script>
    <div id="divTime" clientidmode="AutoID" runat="server" class="input-append date dropup">
        <span class="add-on "><i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
        </span>
        <asp:TextBox ID="txtDate" ClientIDMode="Predictable" Width="65%" type="text" runat="server"></asp:TextBox> 
    </div>

C#:

 public string Text
    {
        get { return txtDate.Text; }
        set { txtDate.Text = value; }
    }

By this property I can get Text Value from my webpage. It's working fine, but now I want to use Usercontrol (Timepicker) in repeater. Here is the code:

 protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
UserControl tmStart = (UserControl)e.Item.FindControl("tmStart");
Label lblStartTime = (Label)e.Item.FindControl("lblStartTime");
tmStart.Text = lblStart.Text;
     }

It's working fine outside repeater but in repeater it's not working, tmStart.Text (Text is giving error). Any idea what's wrong?


Solution

  • You should cast the tmStart control as the specific control, not the generic UserControl. The you will have access to the the Text Property.

    protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Controls_TimePicker tmStart = (Controls_TimePicker)e.Item.FindControl("tmStart");
        Label lblStartTime = (Label)e.Item.FindControl("lblStartTime");
        tmStart.Text = lblStart.Text;
     }