是否可以设置默认值以在DynamicControl中显示?不

时间:2016-04-12 08:00:30

标签: c# asp.net webforms dynamic-controls

在asp.net webform中,我有一个带DynamicControl的FormView。如何为issuedDate设置默认值?

   <asp:FormView runat="server"....>
        <InsertItemTemplate>
                <asp:Label AssociatedControlID="issuedDate" runat="server">Issued date:</asp:Label>
                    <asp:DynamicControl runat="server" ID="issuedDate" DataField="IssuedDate" Mode="Edit" />
            ....
        </InsertItemTemplate>
        <EmptyDataTemplate>
            <p>
                No Data found.
            </p>
        </EmptyDataTemplate>
    </asp:FormView>

2 个答案:

答案 0 :(得分:0)

尝试按以下方式访问您的控件,

protected void ItemsFormView_DataBound(object sender, EventArgs e)
{
    If (FormView1.CurrentMode == FormViewMode.Insert){
        DataRowView dataRow = ((DataRowView)FormView1.DataItem);
        if (Convert.ToInt16(dataRow["ClStk"]) <= 0)
        {
            Label lbl = (Label)FormView1.FindControl("lblStock");
            lbl.CssClass = "changefont";
        }
    }
}

答案 1 :(得分:0)

DynamicControl不起作用。所以我使用TextBox而不是DynamicControl。

<asp:TextBox runat="server" ID="IssuedDateTextBox" CssClass="form-control" Text='<%# Bind("IssuedDate") %>' />

添加了myForm_DataBound

protected void myForm_DataBound(object sender, EventArgs e)
{
    if (myForm.CurrentMode == FormViewMode.Insert)
    {
        TextBox tb = (TextBox)myForm.FindControl("IssuedDateTextBox");
        if (String.IsNullOrEmpty(tb.Text.Trim()))
        {
            //set default value - TODAY date
            tb.Text = String.Format("{0:yyyy}{0:MM}{0:dd}", DateTime.Now);
        }
    }
}