下面的aspx代码是我的asp:FormView的一部分,它是从外部数据源填充的(一个单独的表,最终将反馈到该表中)。不允许用户直接对TextBox进行更改,但可以通过选择下拉列表来更新文本框,这将改变TextBox中的值。这段代码完美无缺。
<asp:TableRow>
<asp:TableCell Width="25px">
<asp:TextBox runat="server" OnTextChanged="AssetTypeTextBox_TextChanged" ID="AssetTypeTextBox" Width="25px" Readonly = "true" Text='<%#Bind("AssetType") %>' />
</asp:TableCell>
<asp:TableCell Width="400px">
<asp:Label runat="server" ID="AssetTypeTextLabel" Text="Blank"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:DropDownList runat="server" OnSelectedIndexChanged="AssetTypeDDL_SelectedIndexChanged" ID="AssetTypeDDL" AutoPostBack="true">
<asp:ListItem Selected="True"> </asp:ListItem>
<asp:ListItem Value="34">DB2 Cluster</asp:ListItem>
<asp:ListItem Value="3">Desktop</asp:ListItem>
<asp:ListItem Value="44">DNS Appliance</asp:ListItem>
<asp:ListItem Value="37">EXCH Resource Cluster</asp:ListItem>
<asp:ListItem Value="18">Firewall</asp:ListItem>
<asp:ListItem Value="12">HyperV Cluster</asp:ListItem>
<asp:ListItem Value="11">HyperV Guest Appliance</asp:ListItem>
<asp:ListItem Value="10">HyperV Guest OS</asp:ListItem>
<asp:ListItem Value="17">IDS/IPS Device</asp:ListItem>
<asp:ListItem Value="15">IBM PwrX Blade Server</asp:ListItem>
<asp:ListItem Value="14">IBM PwrX Server</asp:ListItem>
<asp:ListItem Value="52">IDS/IPS Device</asp:ListItem>
<asp:ListItem Value="4">Laptop</asp:ListItem>
<asp:ListItem Value="6">Mac Desktop</asp:ListItem>
<asp:ListItem Value="7">Mac Laptop</asp:ListItem>
<asp:ListItem Value="50">Mainframe</asp:ListItem>
<asp:ListItem Value="19">Mainframe Tape</asp:ListItem>
<asp:ListItem Value="35">MFD/Printers</asp:ListItem>
<asp:ListItem Value="48">Net Access Card Ctl</asp:ListItem>
<asp:ListItem Value="47">Net IP Cameras</asp:ListItem>
<asp:ListItem Value="48">Net Video Converter</asp:ListItem>
<asp:ListItem Value="46">Net Video Recorder</asp:ListItem>
<asp:ListItem Value="21">Network Appliance</asp:ListItem>
<asp:ListItem Value="52">Network Appliance (Virtual)</asp:ListItem>
<asp:ListItem Value="51">Network Supervisor</asp:ListItem>
<asp:ListItem Value="22">Physical Tape</asp:ListItem>
<asp:ListItem Value="42">Rack</asp:ListItem>
<asp:ListItem Value="43">Remote Mgmt</asp:ListItem>
<asp:ListItem Value="1">Router</asp:ListItem>
<asp:ListItem Value="20">SAN Device</asp:ListItem>
<asp:ListItem Value="27">Server Enclosure</asp:ListItem>
<asp:ListItem Value="33">SQL Cluster</asp:ListItem>
<asp:ListItem Value="2">Switch</asp:ListItem>
<asp:ListItem Value="8">ThinClient</asp:ListItem>
<asp:ListItem Value="5">UNIX Workstation</asp:ListItem>
<asp:ListItem Value="26">VIOS Cluster(NR)</asp:ListItem>
<asp:ListItem Value="16">VIOS Guest OS</asp:ListItem>
<asp:ListItem Value="45">VLE Cluster</asp:ListItem>
<asp:ListItem Value="25">VMWare Guest OS</asp:ListItem>
<asp:ListItem Value="40">VOIP Analog Fax</asp:ListItem>
<asp:ListItem Value="24">VOIP Gateway</asp:ListItem>
<asp:ListItem Value="23">VOIP Phone</asp:ListItem>
<asp:ListItem Value="41">VOIP Virtual Address</asp:ListItem>
<asp:ListItem Value="32">Windows FO Cluster</asp:ListItem>
<asp:ListItem Value="9">x86 Blade Server</asp:ListItem>
<asp:ListItem Value="13">x86 Standard Server</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
我正在使用以下代码来处理插入:
protected void InsertButton_Click1(object sender, EventArgs e)
{
ChangeFormFV.InsertItem(true);
Response.Redirect("InvList.aspx");
}
单击“插入”后,无论用户进行了哪些更改,FormView上的任何值都将写入插入数据库中。这似乎违背了我的逻辑。我认为在初始化InsertItem(true)时表单中存在的值将被写入表中而不是绘制屏幕时的第一个值。
有关纠正此问题的任何想法?
Sean,这是c#的更大剪辑:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CInTrac
{
public partial class Update : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox uname = (TextBox)ChangeFormFV.Row.FindControl("SubmitByTextBox");
if (uname != null)
uname.Text = Session["RegUser"].ToString();
TextBox udate = (TextBox)ChangeFormFV.Row.FindControl("SubmitDTTextBox");
if (udate != null)
udate.Text = DateTime.Now.ToString("MM/dd/yyyy");
// AssetID
Label ustuff = (Label)RefDataFV.Row.FindControl("AssetIDLabel");
if (ustuff != null)
Session["AID"] = ustuff.Text;
TextBox AIDvalue = (TextBox)ChangeFormFV.Row.FindControl("AssetIDTextBox");
if (AIDvalue != null)
AIDvalue.Text = Session["AID"].ToString();
// End of AssetID
// AssetType
Label atlstuff = (Label)RefDataFV.Row.FindControl("AssetTypeLabel");
TextBox ATypevalue = (TextBox)ChangeFormFV.Row.FindControl("AssetTypeTextBox");
if (ATypevalue != null)
ATypevalue.Text = atlstuff.Text;
Session["AssetType"] = atlstuff.Text;
GetAssetTypeFV.DataBind();
Label astylbl = (Label)GetAssetTypeFV.Row.FindControl("AssetTypeLabel");
Label ATTL = (Label)ChangeFormFV.Row.FindControl("AssetTypeTextLabel");
if (ATTL != null)
ATTL.Text = astylbl.Text;
// End of AssetType
// AssetName
Label anlstuff = (Label)RefDataFV.Row.FindControl("AssetNameLabel");
TextBox ANamevalue = (TextBox)ChangeFormFV.Row.FindControl("AssetNameTextBox");
if (ANamevalue != null)
ANamevalue.Text = anlstuff.Text;
// End of AssetName
// AssetShortDesc
Label asdstuff = (Label)RefDataFV.Row.FindControl("AssetShortDescLabel");
TextBox AShortDescvalue = (TextBox)ChangeFormFV.Row.FindControl("AssetShortDescTextBox");
if (AShortDescvalue != null)
AShortDescvalue.Text = asdstuff.Text;
// End of AssetShortDesc
// CLIPPED HERE to remove repeated copy of data from one data source to the other
// Also clipped from here for brevity are routines that hide/show pannels of indidvidaul fields
// based on ACL values of users
}
protected void AssetTypeDDL_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList pick = (DropDownList)ChangeFormFV.Row.FindControl("AssetTypeDDL");
TextBox ATypevalue = (TextBox)ChangeFormFV.Row.FindControl("AssetTypeTextBox");
if (ATypevalue != null)
ATypevalue.Text = pick.SelectedValue.ToString();
Label ATTL = (Label)ChangeFormFV.Row.FindControl("AssetTypeTextLabel");
if (ATTL != null)
ATTL.Text = pick.SelectedItem.Text;
}
protected void AssetStatusDdl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList pick = (DropDownList)ChangeFormFV.Row.FindControl("AssetStatusDdl");
TextBox AStatusvalue = (TextBox)ChangeFormFV.Row.FindControl("AssetStatusTextBox");
if (AStatusvalue != null)
AStatusvalue.Text = pick.SelectedValue.ToString();
}
protected void AssetPropTypeDdl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList pick = (DropDownList)ChangeFormFV.Row.FindControl("AssetPropTypeDdl");
TextBox APropTypevalue = (TextBox)ChangeFormFV.Row.FindControl("AssetPropTypeTextBox");
if (APropTypevalue != null)
APropTypevalue.Text = pick.SelectedValue.ToString();
}
protected void AssetCompany1Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList pick = (DropDownList)ChangeFormFV.Row.FindControl("AssetCompany1Ddl");
TextBox ACompanyvalue = (TextBox)ChangeFormFV.Row.FindControl("AssetCompanyTextBox");
if (ACompanyvalue != null)
ACompanyvalue.Text = pick.SelectedValue.ToString();
}
protected void AssetPriOSDdl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList pick = (DropDownList)ChangeFormFV.Row.FindControl("AssetPriOSDdl");
TextBox APriOSvalue = (TextBox)ChangeFormFV.Row.FindControl("AssetPriOSTextBox");
if (APriOSvalue != null)
APriOSvalue.Text = pick.SelectedValue.ToString();
}
protected void AssetAdminGrpDdl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList pick = (DropDownList)ChangeFormFV.Row.FindControl("AssetAdminGrpDdl");
TextBox AAdminGrpvalue = (TextBox)ChangeFormFV.Row.FindControl("AssetAdminGrpTextBox");
if (AAdminGrpvalue != null)
AAdminGrpvalue.Text = pick.SelectedValue.ToString();
}
protected void AssetOperStatusDDL_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList pick = (DropDownList)ChangeFormFV.Row.FindControl("AssetOperStatusDDL");
TextBox AOperStatusvalue = (TextBox)ChangeFormFV.Row.FindControl("AssetOperStatusTextBox");
if (AOperStatusvalue != null)
AOperStatusvalue.Text = pick.SelectedValue.ToString();
}
protected void InsertButton_Click1(object sender, EventArgs e)
{
ChangeFormFV.InsertItem(true);
Response.Redirect("InvList.aspx");
}
protected void AssetTypeTextBox_TextChanged(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:0)
很确定通过我们冗长的讨论,这与Page_Load期间的事件顺序有关。页面加载事件在按钮单击事件之前触发,因此在您尝试插入之前,下拉列表中的值等会被修改。只需在Page_Load:
期间查看if(!Page.IsPostBack)
这可以解决您遇到的问题。