前台代码:
<table class="style2">
<caption style="font-family: 宋体, Arial, Helvetica, sans-serif; font-size: larger; font-weight: bold; color: #000000; background-color: #FFCCFF">
订单信息</caption>
<tr>
<td>
订单编号:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
客户编码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
配方编号:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
订购日期:</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
需求日期:</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
单 价:</td>
<td>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
数 量:</td>
<td>
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
折 扣:</td>
<td>
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
支付方式:</td>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Text="现金" />
<asp:CheckBox ID="CheckBox2" runat="server" Text="银行汇款" />
<asp:CheckBox ID="CheckBox3" runat="server" Text="支票" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="提交" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="取消" />
</td>
</tr>
</table>
button1的onclick事件代码:
protected void Button1_Click(object sender, EventArgs e)
{
string ss;
string m = "";
string sqlconnstr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(sqlconnstr);
SqlCommand comm = new SqlCommand();
comm.Connection = sqlconn;
comm.CommandText = "insert into orders(orderid,CID,FormulaID,OrderDate,RequireDate,UnitPrice,Quantity,Discount,Payway) values(@id,@CID,@FID,@Odate,@Rdate,@price,@quantity,@discount,@payway)";
comm.Parameters.AddWithValue("@id", TextBox1.Text);
comm.Parameters.AddWithValue("@CID", TextBox2.Text);
comm.Parameters.AddWithValue("@FID", TextBox3.Text);
comm.Parameters.AddWithValue("@Odate", TextBox4.Text);
comm.Parameters.AddWithValue("@Rdate", TextBox5.Text);
comm.Parameters.AddWithValue("@price", TextBox6.Text);
comm.Parameters.AddWithValue("@quantity", TextBox7.Text);
comm.Parameters.AddWithValue("@discount", TextBox8.Text);
for (int i = 1; i <= 3; i++)
{
CheckBox bc = Page.FindControl("CheckBox" + i) as CheckBox;
if (bc.Checked)
{
m += bc.Text + " "; }
}
comm.Parameters.AddWithValue("@payway", m.ToString());
try
{
sqlconn.Open();
comm.ExecuteNonQuery();
ss = "alert('" + "提交成功!" + "');";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "success", ss, true);
}
catch (Exception ex)
{
ss = "alert('" + "提交失败,请重新添加!" + ex.Message + "');";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "success", ss, true);
}
finally
{
comm = null;
sqlconn.Close();
sqlconn = null;
}
}上面红色部分显示错误:未将对象引用设置到对象的实例。蓝色部分的代码我不知道对不对~
总共有三个checkbox,我是希望将选中的值都记录下来,可以同时选中两个以上。
<table class="style2">
<caption style="font-family: 宋体, Arial, Helvetica, sans-serif; font-size: larger; font-weight: bold; color: #000000; background-color: #FFCCFF">
订单信息</caption>
<tr>
<td>
订单编号:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
客户编码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
配方编号:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
订购日期:</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
需求日期:</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
单 价:</td>
<td>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
数 量:</td>
<td>
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
折 扣:</td>
<td>
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
支付方式:</td>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Text="现金" />
<asp:CheckBox ID="CheckBox2" runat="server" Text="银行汇款" />
<asp:CheckBox ID="CheckBox3" runat="server" Text="支票" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="提交" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="取消" />
</td>
</tr>
</table>
button1的onclick事件代码:
protected void Button1_Click(object sender, EventArgs e)
{
string ss;
string m = "";
string sqlconnstr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(sqlconnstr);
SqlCommand comm = new SqlCommand();
comm.Connection = sqlconn;
comm.CommandText = "insert into orders(orderid,CID,FormulaID,OrderDate,RequireDate,UnitPrice,Quantity,Discount,Payway) values(@id,@CID,@FID,@Odate,@Rdate,@price,@quantity,@discount,@payway)";
comm.Parameters.AddWithValue("@id", TextBox1.Text);
comm.Parameters.AddWithValue("@CID", TextBox2.Text);
comm.Parameters.AddWithValue("@FID", TextBox3.Text);
comm.Parameters.AddWithValue("@Odate", TextBox4.Text);
comm.Parameters.AddWithValue("@Rdate", TextBox5.Text);
comm.Parameters.AddWithValue("@price", TextBox6.Text);
comm.Parameters.AddWithValue("@quantity", TextBox7.Text);
comm.Parameters.AddWithValue("@discount", TextBox8.Text);
for (int i = 1; i <= 3; i++)
{
CheckBox bc = Page.FindControl("CheckBox" + i) as CheckBox;
if (bc.Checked)
{
m += bc.Text + " "; }
}
comm.Parameters.AddWithValue("@payway", m.ToString());
try
{
sqlconn.Open();
comm.ExecuteNonQuery();
ss = "alert('" + "提交成功!" + "');";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "success", ss, true);
}
catch (Exception ex)
{
ss = "alert('" + "提交失败,请重新添加!" + ex.Message + "');";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "success", ss, true);
}
finally
{
comm = null;
sqlconn.Close();
sqlconn = null;
}
}上面红色部分显示错误:未将对象引用设置到对象的实例。蓝色部分的代码我不知道对不对~
总共有三个checkbox,我是希望将选中的值都记录下来,可以同时选中两个以上。
解决方案 »
- ----------多层MVC框架,连接项目出现错误!恳求高手帮忙,怎样解决呀!
- 多语言标题设置问题
- ASP.NET 与 SYBASE的问题 解决别赠分!
- 关于URL重写后获取不到URL参数的值的问题!
- 怎样让页面加入的div 不透明
- RadioButtonList1触发事件问题
- 急救!!!第三方控件里的~!
- 研究讨论下分页控件的问题!大家多多积极参送分活动!在网上看到sqlPager控件资料比较多,这控件有没有人用过....
- 小弟菜鸟,跪求关于Session的问题!
- database.bak这种文件在SQL SERVER中还原怎么不好使啊???谢谢!!!!!
- asp。net 网站项目用iis服务器、aspnet_wp.exe的问题
- 分层开发中更新如何实现 比较好
没有得到控件。
改成
CheckBox bc = PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("CheckBox" + i) as CheckBox;
还是同样的错误
<%@ Page Language="C#" MasterPageFile="~/母板/MasterPage.master" AutoEventWireup="true" CodeFile="order.aspx.cs" Inherits="员工_order" Title="无标题页" %><asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.style2
{
width: 50%;
border-style: solid;
border-width: 1px;
background-color: #FFCCFF;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<tr>
<td>
<br />
<table class="style2">
<caption style="font-family: 宋体, Arial, Helvetica, sans-serif; font-size: larger; font-weight: bold; color: #000000; background-color: #FFCCFF">
订单信息</caption>
<tr>
<td>
订单编号:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
客户编码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
配方编号:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
订购日期:</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
需求日期:</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
单 价:</td>
<td>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
数 量:</td>
<td>
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
折 扣:</td>
<td>
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
支付方式:</td>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Text="现金" />
<asp:CheckBox ID="CheckBox2" runat="server" Text="银行汇款" />
<asp:CheckBox ID="CheckBox3" runat="server" Text="支票" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="提交" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="取消" />
</td>
</tr>
</table>
<br />
</td>
</tr>
</asp:Content>
怎么选三个?循环?
堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
员工_order.Button1_Click(Object sender, EventArgs e) in e:\成衣织物配方成本管理系统\员工\order.aspx.cs:40
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
我看不懂这个
foreach (Control c in form1.Controls) //form1是表单ID
{
if (c is CheckBox)
{
CheckBox ck = c as CheckBox;
if (ck.Checked)
{
m += bc.Text + " ";
}
}
}
我没出现过form。。要怎么改呀?
<asp:CheckBoxList ID="CheckBoxList1" runat="server"> </asp:CheckBoxList>
foreach (CheckBox ch in CheckBoxList1)
{ if (ch.Checked)
{
......
}
}
谢谢各位~