我确实在控件的事件中选中了ItemInserting事件进行后台编辑。但是输入完数据后点击插入就会出现激发了未处理的事件ItemInserting 实在不知道出什么问题了,还希望大家帮忙看一看,谢谢。
代码如下:
源码:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateInsertButton="True" AutoGenerateRows="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Height="50px" OnModeChanging="DetailsView1_ModeChanging" OnPageIndexChanging="DetailsView1_PageIndexChanging" Width="125px">
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<Fields>
<asp:BoundField DataField="ID" HeaderText="编号" />
<asp:BoundField DataField="yonghuming" HeaderText="用户名" />
<asp:BoundField DataField="mima" HeaderText="密码" />
<asp:BoundField DataField="xingbie" HeaderText="性别" />
<asp:BoundField DataField="nianling" HeaderText="年龄" />
<asp:BoundField DataField="aihao" HeaderText="爱好" />
</Fields>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
</asp:DetailsView>protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
string constr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=student666;Data Source=.\\SQLEXPRESS";
OleDbConnection conn = new OleDbConnection(constr);
string sqlstr="insert into student666(id,yonghuming,mima,xingbie,nianling,aihao)values('"+ ((TextBox)(DetailsView1.Rows[0].Cells[0].Controls[0])).Text.ToString().Trim() + "','" + ((TextBox)(DetailsView1.Rows[1].Cells[0].Controls[0])).Text.ToString().Trim() + "','" + ((TextBox)(DetailsView1.Rows[2].Cells[0].Controls[0])).Text.ToString().Trim() + "','" + ((TextBox)(DetailsView1.Rows[3].Cells[0].Controls[0])).Text.ToString().Trim() + "','" + ((TextBox)(DetailsView1.Rows[4].Cells[0].Controls[0])).Text.ToString().Trim() + "','" + ((TextBox)(DetailsView1.Rows[5].Cells[0].Controls[0])).Text.ToString().Trim() + "')";
OleDbCommand cmd = new OleDbCommand(sqlstr, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
DetailsView1.DataBind();
bind();
}
代码如下:
源码:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateInsertButton="True" AutoGenerateRows="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Height="50px" OnModeChanging="DetailsView1_ModeChanging" OnPageIndexChanging="DetailsView1_PageIndexChanging" Width="125px">
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<Fields>
<asp:BoundField DataField="ID" HeaderText="编号" />
<asp:BoundField DataField="yonghuming" HeaderText="用户名" />
<asp:BoundField DataField="mima" HeaderText="密码" />
<asp:BoundField DataField="xingbie" HeaderText="性别" />
<asp:BoundField DataField="nianling" HeaderText="年龄" />
<asp:BoundField DataField="aihao" HeaderText="爱好" />
</Fields>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
</asp:DetailsView>protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
string constr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=student666;Data Source=.\\SQLEXPRESS";
OleDbConnection conn = new OleDbConnection(constr);
string sqlstr="insert into student666(id,yonghuming,mima,xingbie,nianling,aihao)values('"+ ((TextBox)(DetailsView1.Rows[0].Cells[0].Controls[0])).Text.ToString().Trim() + "','" + ((TextBox)(DetailsView1.Rows[1].Cells[0].Controls[0])).Text.ToString().Trim() + "','" + ((TextBox)(DetailsView1.Rows[2].Cells[0].Controls[0])).Text.ToString().Trim() + "','" + ((TextBox)(DetailsView1.Rows[3].Cells[0].Controls[0])).Text.ToString().Trim() + "','" + ((TextBox)(DetailsView1.Rows[4].Cells[0].Controls[0])).Text.ToString().Trim() + "','" + ((TextBox)(DetailsView1.Rows[5].Cells[0].Controls[0])).Text.ToString().Trim() + "')";
OleDbCommand cmd = new OleDbCommand(sqlstr, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
DetailsView1.DataBind();
bind();
}
激发了未处理的事件ItemInserting
4.如何获得第二个图中控件里的值。(可能我的SQL串中获值语句写的不对,不知该如何写)