以编程方式设置detailsView'字段的数据值

时间:2011-09-20 08:35:01

标签: c# asp.net detailsview

当我单击详细信息视图上的插入按钮时,我想设置一个值(Field IDCommerciale)。我尝试使用OnItemInserting更改值但没有任何反应。 这是aspx上的代码:

 <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" DefaultMode="Insert"
                AutoGenerateRows="False" DataKeyNames="IDPianificazione"
                DataSourceID="EntityDataSourceDetailsView" OnItemInserting="insertingmodeevent"
                >
                <Fields>
                    <asp:TemplateField HeaderText="IDCommerciale"
                        SortExpression="IDCommerciale">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Commerciali.Nome") %>'></asp:Label>
                        </ItemTemplate>
                        <InsertItemTemplate>
                        <asp:DropDownList ID="ddlIDCommerciale" runat="server" SelectedValue='<%# Bind("IDCommerciale") %>'
                            DataSourceID="EntityDataSource1" DataTextField="Nome"
                            DataValueField='IDCommerciale'>                        
                        </asp:DropDownList>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="IDCLiente" SortExpression="IDCLiente">
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("IDCLiente") %>'></asp:Label>
                        </ItemTemplate>
                        <InsertItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("IDCLiente") %>'></asp:TextBox>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="IDCategoria" SortExpression="IDCategoria">
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("IDCategoria") %>'></asp:Label>
                        </ItemTemplate>
                        <InsertItemTemplate>
                            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("IDCategoria") %>'></asp:TextBox>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Oggetto" HeaderText="Oggetto" 
                        SortExpression="Oggetto" />
                    <asp:BoundField DataField="datainizio" HeaderText="datainizio" 
                        SortExpression="datainizio" />
                    <asp:BoundField DataField="DataPresuntaFine" HeaderText="DataPresuntaFine" 
                        SortExpression="DataPresuntaFine" />
                    <asp:BoundField DataField="DataCompletamento" HeaderText="DataCompletamento" 
                        SortExpression="DataCompletamento" />
                    <asp:CommandField 
                        ShowInsertButton="True" />
                </Fields>
            </asp:DetailsView>




            <asp:EntityDataSource ID="EntityDataSourceDetailsView" runat="server" 
                ConnectionString="name=SalesPortalEntities" Include="Categorie,Commerciali,Clienti"
                DefaultContainerName="SalesPortalEntities" EnableDelete="True" 
                EnableFlattening="False" EnableInsert="True" EnableUpdate="True" 
                EntitySetName="PianificazioneIncontri">
            </asp:EntityDataSource>

        <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
            ConnectionString="name=SalesPortalEntities" 
            DefaultContainerName="SalesPortalEntities" EnableFlattening="False" 
            EntitySetName="Commerciali">
        </asp:EntityDataSource>

这是背后的代码:

protected void insertingmodeevent(object sender, EventArgs e)
    {
        string gruppoAdmin = System.Web.Security.FormsAuthentication.Decrypt(Request.Cookies[1].Value).UserData;
        if (gruppoAdmin.Contains("SPGAdmins"))
        {
            try
            {
            DropDownList dd = new DropDownList();
            dd = DetailsView1.FindControl("ddlIDCommerciale") as DropDownList;
            dd.SelectedIndex = 1;
            dd.SelectedValue = "1";
            }
            catch (Exception)
            {

                new Exception();
            }


        }
        else
        {
          // other code
        }
    }

代码是定期执行的,但是如果我检查数据库上的数据,那么Item就会在后面的代码上强制使用值。我也尝试使用事件OnDataBinding但结果是一样的。 如何强制该字段的值?

1 个答案:

答案 0 :(得分:0)

一种可能的解决方案是在gridview的entitydatasource上添加一个事件OnItemInserting。然后我使用了这段代码:

protected void UpdateDataSource(object sender, EntityDataSourceChangingEventArgs e)
{
    if (!gruppoAdmin.Contains("SPGAdmins"))
    {
        SalesPortalModel.PianificazioneIncontri pianificazione = e.Entity as SalesPortalModel.PianificazioneIncontri;
        int idCommerciale = (from a in entity.Commerciali where a.Nome == Context.User.Identity.Name select a.IDCommerciale).First();
        pianificazione.IDCommerciale = idCommerciale;
        detailsView1.Rows[1].Visible = false;
    }

}