设置详细信息视图的数据值 '以编程方式字段

发布于 2024-12-05 08:50:22 字数 4820 浏览 0 评论 0原文

当我单击详细信息视图上的插入按钮时,我想设置一个值(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
        }
    }

代码定期执行,但是如果我检查数据库上的数据,项目不会采用后面代码上强制的值。我也尝试了 OnDataBinding 事件,但结果是相同的。 我如何强制该字段的值?

I want to set a value (Field IDCommerciale) when I click the insert button on a details view. I tried to change the value using OnItemInserting but nothing happens.
This is the code on the 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>

and this is the code behind:

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

The code is execude regularly but if I check the data on the Database, the Item does'ent take the value forced on the code behind. I tried also with The event OnDataBinding but the result is the same.
How can I force the value for this field?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐偏执 2024-12-12 08:50:22

一个可能的解决方案是在我的 gridview 的实体数据源上添加一个事件 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;
    }

}

A possible solution is to add an event OnItemInserting on the entitydatasource of my gridview. Then I used this code:

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文