Ken Carter Ken Carter - 1 month ago 10
ASP.NET (C#) Question

Insert ignoring data in formview

The aspx code below is part of my asp:FormView which is populated from a external datasource (a separate table that this will eventually feed back into). The user is not allowed to make changes directly to the TextBox but can update the text box by making selections to the dropdownlist which will alter the value in the TextBox. This code works perfectly.

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




I'm using the following code behind to process insert:

protected void InsertButton_Click1(object sender, EventArgs e)
{
ChangeFormFV.InsertItem(true);
Response.Redirect("InvList.aspx");
}


Upon clicking 'Insert', whatever values were on the FormView regardless of what changes have been made by the user will be written to the database in the insert. This seems to defy logic to me. I would think that the values present in the form at the time that the InsertItem(true) was initialized would be written to the table not what was first there when the screen was drawn.

Any thoughts on correcting this issue?

Sean, here is a larger clipping of the c# for you:



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)
{

}
}

}‚Äč




Answer

Pretty sure that through our lengthy discussion this is related the order of events during Page_Load. The page load events fires BEFORE the button click event so the values in your dropdowns and such were getting modified before you tried to insert them. Simply check during Page_Load:

if(!Page.IsPostBack)

This should get you around the issue you are having.

Comments