如何在asp.net中的Repeater控件itemCommand中获取UserControl的Text值?

时间:2013-05-27 14:45:38

标签: asp.net twitter-bootstrap user-controls repeater

我开发了usercontrol timePicker。我正在使用Bootstrap和Ajaxtoolkit 4但不使用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; }
    }

通过此属性,我可以从我的网页获取文本值。它工作正常,但现在我想在转发器中使用Usercontrol(Timepicker)。这是代码:

 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;
     }

它在转发器之外工作正常,但在转发器中它不起作用,tmStart.Text(文本给出错误)。知道什么是错的吗?

2 个答案:

答案 0 :(得分:0)

您应该将tmStart控件强制转换为特定控件,而不是通用UserControl。您将可以访问文本属性。

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;
 }

答案 1 :(得分:0)

我通过右键单击变量并单击快速信息

解决了这个问题

这对我有用

 ((Controls_TimePicker)(tmStartEdit)).Text = lblStartTime.Text;
 ((Controls_TimePicker)tmEndEdit).Text = lblEndTime.Text;
 ((Controls_DatePickerAdmin)(cldrStartEdit)).Text = lblStartDate.Text;  
 ((Controls_DatePickerAdmin)(cldrEndEdit)).Text = lblEndDate.Text;

// Controls_TimePicker显示红色,但运行时没有给出错误,不知道为什么。