How can I retrieve the value of a control within an ASP.Net Dynamic Data Custom Field Template?

StackOverflow https://stackoverflow.com/questions/16949398

  •  31-05-2022
  •  | 
  •  

質問

Long story short, for an old project I used ASP.Net Dynamic Data and probably did a terrible job with it. One of the Field Templates has multiple controls in it, and now I need to get at the value of one control from the FormView's Submit event because we changed the way that value is stored.

I can find the Field Template itself using FindFieldTemplate... but I can't figure out how to get to the controls inside of the template.

How can I do that without re-engineering the whole thing to pull that one field out? It would probably be more correct to re-engineer it, but this is a quick fix for a website that's going to be scrapped in a couple months.

EDIT: Was asked to show code so here it is. The FormView is pretty standard, just uses an . The Field Template actually has it's own listview and I'm controlling it's mode in codebehind. But I need to get the value of txtTitle.

Ticket_TicketMemo.ascx:

<asp:ListView   ID="lvTicketMemos" DataSourceID="ldsTicketMemo" 
            InsertItemPosition="FirstItem" OnLoad="lvTicketMemo_Load" runat="server">
<LayoutTemplate>
    <div style="overflow:auto; height:125px; width:600px;">
        <table class="ListViewTable" runat="server">
            <tr id="itemPlaceHolder" runat="server" />
        </table>
    </div>
</LayoutTemplate>
<ItemTemplate>
    <tr valign="top" class='<%# Container.DataItemIndex % 2 == 0 ? "" : "Alternate" %>'>
        <td><asp:DynamicControl ID="dcType" DataField="Type" runat="server" /></td>
        <td><asp:DynamicControl ID="dcMemo" DataField="Memo" runat="server" /></td>
        <td><asp:DynamicControl ID="dcCreateTime" DataField="CreateTime" runat="server" /></td>
    </tr>
</ItemTemplate>    
<InsertItemTemplate>
    <tr valign="top">
        <td colspan="3">
            <asp:TextBox ID="txtTitle" Width="99%" Visible="false" OnLoad="txtTitle_Load" runat="server" /><br /><br />
        </td>
    </tr>
    <tr valign="top">
        <td colspan="3" width="600px">
            <asp:TextBox    ID="txtMemo" Text='<%# Bind("Memo") %>' Width="99%" OnLoad="txtMemo_Load" TextMode="MultiLine" 
                            Rows="5" runat="server" />
                            <asp:RequiredFieldValidator ID="rfvMemo" Text="Must enter notes" ControlToValidate="txtMemo" runat="server" />
        </td>            
    </tr>
</InsertItemTemplate>

役に立ちましたか?

解決

I have just simulated your problem in my Dynamic Data project. Based on my research (and search) in order to get control value (not from DynamicControl value) in Dynamic Data you should implement the following method (i am using this method in my project and i have found one on Steve blog, i don't remember full link):

/// <summary>
/// Get the control by searching recursively for it.
/// </summary>
/// <param name="Root">The control to start the search at.</param>
/// <param name="Id">The ID of the control to find</param>
/// <returns>The control the was found or NULL if not found</returns>
public static Control FindControlRecursive(this Control Root, string Id)
{
    if (Root.ClientID.IndexOf(Id) > 0)
        return Root;


    foreach (Control Ctl in Root.Controls)
    {
        Control FoundCtl = FindControlRecursive(Ctl, Id);


        if (FoundCtl != null)
            return FoundCtl;
    }
    return null;
}

Now, my EXAMPLE.

First, my custom Insert.aspx page with FormView and EntityDataSource:

<asp:FormView runat="server" ID="FormView1" DataSourceID="DetailsDataSource" DefaultMode="Insert"
    OnItemCommand="FormView1_ItemCommand" RenderOuterTable="false">
    <InsertItemTemplate>
        <table>
            <tr valign="top">
                <td colspan="3">
                    <asp:TextBox ID="txtTitle" Width="99%" Visible="true" runat="server" /><br />
                    <br />
                </td>
            </tr>
        </table>
    </InsertItemTemplate>
</asp:FormView>
<asp:EntityDataSource ID="DetailsDataSource" runat="server" EnableInsert="true" OnInserted="DetailsDataSource_Inserted" />

Then, Inserted event of EntityDataSource:

protected MetaTable table;
protected void DetailsDataSource_Inserted(object sender, EntityDataSourceChangedEventArgs e)
{
    if (e.Exception == null || e.ExceptionHandled)
    {
            string strTitle = String.Empty;
            Control CtlTitle = FormView1.FindControlRecursive("txtTitle");
            if (CtlTitle != null)
            {
                TextBox TextBoxTitle = (TextBox)CtlTitle;
                strTitle = TextBoxTitle.Text;
            }

            Response.Redirect(table.ListActionPath + "?" + "Department_Id=" + strTitle);
    }
}

Finally, i enter the text into txtTitle, for example, 13 and then i get

enter image description here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top