下面编写详细信息代码视图,我正在尝试更新数据。 当我点击编辑按钮时,字段会更改为文本框,然后更改某些值以进行测试,当我点击更新时,以前的数据没有变化。 没有得到任何关于代码的错误,也没有逐行调试,但没有问题,它执行得很好但数据库中没有数据更新。
我还需要更多代码或其他一些东西。
<asp:DetailsView ID="dvstudentdetails" runat="server" EmptyDataRowStyle-HorizontalAlign="Left"
CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="false" AutoGenerateEditButton="True"
AutoGenerateRows="False"
onitemupdating="DetailsView1_ItemUpdating"
onitemupdated="DetailsView1_ItemUpdated" onmodechanging="DetailsView1_ModeChanging1">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<EmptyDataRowStyle HorizontalAlign="Left"></EmptyDataRowStyle>
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<Fields>
<asp:TemplateField HeaderText="StudentID" Visible="True" InsertVisible="True" >
<ItemTemplate>
<asp:Label ID="lblStudentID" Text='<%# Eval("stud_id") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblstud" runat="server" Text='<%# Bind("studname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteditstudname" runat="server" Text='<%# Bind("studname") %>' MaxLength="10" /></EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="InsertstudnameTextBox" runat="server" Text='<%# Bind("studname") %>' MaxLength="10" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" Student Contact Number">
<ItemTemplate>
<asp:Label ID="lblstudnumb" runat="server" Text='<%# Bind("stud_numb") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteditstudnumb" runat="server" Text='<%# Bind("stud_numb") %>' MaxLength="10" /></EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtinsertstudnumb" runat="server" Text='<%# Bind("stud_numb") %>' MaxLength="10" /></InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Parent Contact Number">
<ItemTemplate>
<asp:Label ID="lblparenumb" runat="server" Text='<%# Bind("pare_numb") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteditparenum" runat="server" Text='<%# Bind("pare_numb") %>' MaxLength="10" /></EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtinsertparenum" runat="server" Text='<%# Bind("pare_numb") %>' MaxLength="10" /></InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblAddress" runat="server" Text='<%#Bind("address") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteditaddress" runat="server" Text='<%# Bind("address") %>' MaxLength="10" /></EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtinsertaddress" runat="server" Text='<%# Bind("address") %>' MaxLength="10" /></InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%#Bind("city") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteditcity" runat="server" Text='<%# Bind("city") %>' MaxLength="10" /></EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtinsertcity" runat="server" Text='<%# Bind("city") %>' MaxLength="10" /></InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:Label ID="lblgender" runat="server" Text='<%#Bind("gender") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteditgender" runat="server" Text='<%# Bind("gender") %>' MaxLength="10" /></EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtinsertgender" runat="server" Text='<%# Bind("gender") %>' MaxLength="10" /></InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Id">
<ItemTemplate>
<asp:Label ID="lblemail" runat="server" Text='<%#Bind("email") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteditemail" runat="server" Text='<%# Bind("email") %>' MaxLength="10" /></EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtinsertemail" runat="server" Text='<%# Bind("email") %>' MaxLength="10" /></InsertItemTemplate>
</asp:TemplateField>
</Fields>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderTemplate>Personal Details</HeaderTemplate>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:DetailsView>
项目更新命令下面的代码
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
con = cn.connection();
//DataKey key = dvstudentdetails.DataKey;
TextBox txtstudname = (TextBox)dvstudentdetails.FindControl("txteditstudname");
TextBox txtstud_num = (TextBox)dvstudentdetails.FindControl("txteditstudnumb");
TextBox txtpare_num = (TextBox)dvstudentdetails.FindControl("txteditparenum");
TextBox txtaddress = (TextBox)dvstudentdetails.FindControl("txteditaddress");
TextBox txtcity = (TextBox)dvstudentdetails.FindControl("txteditcity");
TextBox txtgender = (TextBox)dvstudentdetails.FindControl("txteditgender");
TextBox txtemail = (TextBox)dvstudentdetails.FindControl("txteditemail");
try
{
cmd.Connection = con;
cmd =new SqlCommand( "UPDATE tblstudentdetails SET studname ='" + txtstudname.Text + "', stud_numb ='" + Convert.ToDouble(txtstud_num.Text) + "', pare_numb ='" + Convert.ToDouble(txtpare_num.Text) + "', address ='" + txtaddress.Text + "', city ='" + txtcity.Text + "', gender ='" + txtgender.Text + "', email ='" + txtemail.Text + "' WHERE stud_id='" + 1 + "'",con);
con.Open();
cmd.ExecuteNonQuery();
MessageLabel.Text = "Record updated successfully";
loaddetails();
con.Close();
}
catch (Exception ex)
{
MessageLabel.Text = ex.Message;
}
}
protected void DetailsView1_ModeChanging1(object sender, DetailsViewModeEventArgs e)
{
dvstudentdetails.ChangeMode(e.NewMode);
loaddetails();
}