お客様が当社のウェブサイトから直接注文できるウェブサイトの開発に取り組んでいます。数日前にGridViewの編集方法を変更するまで、コードは機能していました。以前、GridViewをAutoGenerate Columnsに設定していましたが、編集機能にさらに多くの機能が必要になったため、これを変更しました。テーブルの作成方法は次のとおりです(ユーザーがボタンをクリックしてGridViewで簡単な詳細を追加したときに作成されます)。
public void CreateTable()
{
try
{
DataTable table = new DataTable();
if (Session["table"] != null)
table = (DataTable)Session["table"];
else
{
table.Columns.Add("Part Number", typeof(string));
table.Columns.Add("Quantity", typeof(Int32));
table.Columns.Add("Ship-To", typeof(string));
table.Columns.Add("Requested Date", typeof(string));
table.Columns.Add("Shipping Method", typeof(string));
}
DataRow row = table.NewRow();
row["Part Number"] = part;
row["Quantity"] = qty;
row["Ship-To"] = shipto;
row["Requested Date"] = reqdate;
row["Shipping Method"] = shipmthd;
table.Rows.Add(row);
Session["table"] = table;
griditems.DataSource = table.DefaultView;
griditems.DataBind();
}
catch
{
//error message
}
}
これにより、Gridviewが表示され、ユーザーは選択したアイテムを編集/削除できます。次に、GridViewの作成時に表示される別のボタンがあり、実際に.csvファイルをサーバー(展開までの当面のコンピューター)に書き込みます。そのためのコードは次のとおりです。
protected void orderbtn_Click(object sender, EventArgs e)
{
try
{
//ordernum++;
//custordernum = ordernum.ToString("0000000");
if (userlbl.Visible == false && userlbl2.Visible == false)
{
GlobalList.OnlineOrderNum.Add(custordernum, ordernum);
FileStream fs = new FileStream(@"C:\Web_Order\Orders.Bin", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, GlobalList.OnlineOrderNum);
fs.Close();
fs.Dispose();
///Write CSV File For Order
StringBuilder strBuilder = new StringBuilder();
TextWriter tw = new StreamWriter(@"C:\Web_Order\Order_W" + custordernum.ToString() + ".csv");
foreach (GridViewRow row in griditems.Rows)
{
foreach (TableCell cell in row.Cells)
{
// get cell's text
string cellText = cell.Text;
// add quotes and comma around value and append
strBuilder.Append("\"" + cellText + "\",");
}
strBuilder.Append("\n");
}
// output CSV result
tw.Write(strBuilder.ToString());
tw.Close();
tw.Dispose();
GlobalList.weborder = "W" + custordernum.ToString();
Response.Redirect("~/OrderSubmitted.aspx");
}
else
{
validatelbl.Text = "CANNOT SUBMIT FORM WITH ERRORS. PLEASE CORRECT YOUR ERRORS BEFORE SUBMITTING.";
validatelbl.Visible = true;
userlbl.Text = "Please correct your table with the correct information before submitting your order";
userlbl.Visible = true;
userlbl2.Text = "Are your Part Numbers correct? Are your Quantities in the correct format?";
userlbl2.Visible = true;
}
}
catch
{
//error message
}
}
GridViewの編集/削除コードは次のとおりです。
protected void griditems_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
griditems.PageIndex = e.NewPageIndex;
BindData();
}
protected void griditems_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
griditems.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
protected void griditems_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Reset the edit index.
griditems.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string valtext = "An error has occured, please check and make sure your editing is in the correct format and try again.";
orderbtn.Visible = false;
try
{
TextBox editpart = (TextBox)griditems.Rows[e.RowIndex].FindControl("partedit");
TextBox editqty = (TextBox)griditems.Rows[e.RowIndex].FindControl("qtyedit");
TextBox editshipto = (TextBox)griditems.Rows[e.RowIndex].FindControl("shiptoedit");
System.Web.UI.WebControls.Calendar editcal = (System.Web.UI.WebControls.Calendar)griditems.Rows[e.RowIndex].FindControl("reqdatecaledit");
DropDownList editshipmthd = (DropDownList)griditems.Rows[e.RowIndex].FindControl("shipmthdedit");
string newpart = editpart.Text.ToString();
int newqty = Convert.ToInt32(editqty.Text);
string newshipto = editshipto.Text.ToString();
string newreqdate = editcal.SelectedDate.ToShortDateString();
string newshipmthd = editshipmthd.SelectedItem.ToString();
//Reset date if calendar date is not changed so it is not null!
if (newreqdate == "1/1/0001")
newreqdate = reqdate;
DataTable dt = (DataTable)Session["table"];
DataRow dr = dt.Rows[e.RowIndex];
dr["Part Number"] = newpart;
dr["Quantity"] = newqty;
dr["Ship-TO"] = newshipto;
dr["Requested Date"] = newreqdate;
dr["Shipping Method"] = newshipmthd;
dr.AcceptChanges();
Session["table"] = dt;
if (validatelbl.Text == valtext)
validatelbl.Visible = false;
griditems.EditIndex = -1;
BindData();
orderbtn.Visible = true;
}
catch
{
validatelbl.Text = valtext;
validatelbl.Visible = true;
}
}
protected void griditems_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
//DataTable dt = table;
DataTable dt = (DataTable)Session["table"];
if (dt.Rows.Count > 0)
{
dt.Rows.RemoveAt(e.RowIndex + griditems.PageIndex * 10);
griditems.DataSource = dt;
BindData();
}
}
catch
{
validatelbl.Text = "An error occured while processing your request deleting a record. Please try again.";
validatelbl.Visible = true;
}
}
グリッドビューのaspxコードは次のとおりです。
<asp:GridView ID="griditems" runat="server"
onrowdeleting="griditems_RowDeleting" onrowediting="griditems_RowEditing" onrowupdating="griditems_RowUpdating"
AllowPaging="True"
onpageindexchanging="griditems_PageIndexChanging" Onrowcancelingedit="griditems_RowCancelingEdit"
Caption="Order Details" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True"
AutoGenerateColumns="False" >
<EditRowStyle BackColor="#FF9900" BorderStyle="Double"/>
<HeaderStyle Font-Bold="True" Font-Italic="False" />
<RowStyle HorizontalAlign="Center"/>
<Columns>
<asp:TemplateField HeaderText="Part Number">
<ItemTemplate>
<asp:Label ID = "partlbl" runat="server" Text='<%#Eval("Part Number") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="partedit" runat="server" Text='<%# Eval("Part Number")%>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:Label ID = "qtylbl" runat="server" Text='<%#Eval("Quantity") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="qtyedit" runat="server" Text='<%# Eval("Quantity")%>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ship-To">
<ItemTemplate>
<asp:Label ID = "shiptolbl" runat="server" Text='<%#Eval("Ship-To") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="shiptoedit" runat="server" Text='<%# Eval("Ship-To")%>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Requested Date">
<ItemTemplate>
<asp:Label ID = "reqdatelbl" runat="server" Text='<%#Eval("Requested Date") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Calendar ID="reqdatecaledit" runat="server" BackColor="White" BorderColor="#3366CC" BorderWidth="1px" CellPadding="1"
DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="#003399" Height="200px" Width="220px"
ondayrender="reqdatecal_DayRender" ShowGridLines="True">
<DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
<DayStyle BackColor="White" />
<NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
<OtherMonthDayStyle ForeColor="#999999" />
<SelectedDayStyle BackColor="#FF9900" Font-Bold="True" ForeColor="#CCFF99" />
<SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
<TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px" Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF"
Height="25px" />
<TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
<WeekendDayStyle BackColor="#CCCCFF" /></asp:Calendar>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shipping Method">
<ItemTemplate><asp:Label ID="shipmthdlbl" runat="server" Text='<%#Eval("Shipping Method") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="shipmthdedit" runat="server">
<asp:ListItem>FedEx Ground (1-5 Business Days)</asp:ListItem>
<asp:ListItem>FedEx 3 Business Days</asp:ListItem>
<asp:ListItem>FedEx 2 Business Days</asp:ListItem>
<asp:ListItem>FedEx Overnight</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
なぜこれまでずっと機能していたのかわかりませんが、突然機能しなくなりました。必要に応じて新しい注文番号でファイルを作成します。.csvファイルだけが空です(データがまったくありません)。