设置详细信息视图的数据值 '以编程方式字段
当我单击详细信息视图上的插入按钮时,我想设置一个值(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个可能的解决方案是在我的 gridview 的实体数据源上添加一个事件 OnItemInserting 。然后我使用了这段代码:
A possible solution is to add an event OnItemInserting on the entitydatasource of my gridview. Then I used this code: