webform表单更新的问题,觉得从前没见过的啊!求大神来解

qq_25315877 2015-04-08 11:31:19
以下是aspx页面情况

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserEditOrAdd.aspx.cs"
Inherits="CreditCardManage.User.UserEditOrAdd" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 600px;
height: 400px;
}
.style2
{
width: 100%;
}
.style3
{
height: 100px;
}
.style4
{
height: 34px;
}
.style5
{
height: 30px;
}
.style6
{
height: 82px;
}
</style>
</head>
<body>
<form id="form1" runat="server" enableviewstate="True">
<div>
<table border="1" cellpadding="0" cellspacing="0" frame="box">
<tr>
<td>
<table class="style2" rules="all" frame="void" cellpadding="1" cellspacing="0">
<tr>
<td colspan="4">
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="返回列表" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
</td>
</tr>
<tr class="style5">
<td>
用户姓名
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" BorderColor="Black" BorderStyle="Solid"
BorderWidth="1px" Height="30px"></asp:TextBox>
</td>
<td>
身份证号
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" BorderColor="Black" BorderStyle="Solid"
BorderWidth="1px" Height="30px"></asp:TextBox>
</td>
</tr>
<tr class="style5">
<td class="style6">
性别
</td>
<td class="style6" valign="top">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Selected="True">男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:RadioButtonList>
</td>
<td class="style6">
个人信用等级
</td>
<td class="style6" valign="top">
<asp:DropDownList ID="DropDownList1" runat="server" Height="35px" Width="144px">
<asp:ListItem Selected="True">低级</asp:ListItem>
<asp:ListItem>中级</asp:ListItem>
<asp:ListItem>高级</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style5">
卡内金额
</td>
<td class="style5">
<asp:TextBox ID="TextBox3" runat="server" BorderColor="Black" BorderStyle="Solid"
BorderWidth="1px" Height="30px"></asp:TextBox>
</td>
<td class="style5">
联系电话
</td>
<td class="style5">
<asp:TextBox ID="TextBox4" runat="server" BorderColor="Black" BorderStyle="Solid"
BorderWidth="1px" Height="30px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
家庭住址
</td>
<td class="style4" colspan="3">
<asp:TextBox ID="TextBox5" runat="server" Width="394px" BorderColor="Black" BorderStyle="Solid"
BorderWidth="1px" Height="30px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
其他说明
</td>
<td class="style3" colspan="3">
<asp:TextBox ID="TextBox6" runat="server" Height="90px" TextMode="MultiLine" Width="394px"
BorderColor="#000001" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>



以下是后台代码情况



namespace CreditCardManage.User
{
public partial class UserEditOrAdd : System.Web.UI.Page
{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{
try
{
//有id时是修改模块
string id = Request["id"].ToString();
Button1.Text = "保存修改";
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from CardUserInfo where CreditCardGuid=@CreditCardGuid", con);
cmd.Parameters.AddWithValue(@"CreditCardGuid", id);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
TextBox1.Text = sdr["UserName"].ToString();
TextBox2.Text = sdr["IdentityCardNumber"].ToString();
TextBox3.Text = sdr["Money"].ToString();
TextBox4.Text = sdr["PhoneNumber"].ToString();
if (sdr["Sex"].ToString().Trim() == "男")
{
RadioButtonList1.SelectedIndex = 0;
}
else
{
RadioButtonList1.SelectedIndex = 1;
}
if (sdr["CreditStandard"].ToString().Trim() == "低级")
{
DropDownList1.SelectedIndex = 0;
}
else if (sdr["CreditStandard"].ToString().Trim() == "中级")
{
DropDownList1.SelectedIndex = 1;
}
else
{
DropDownList1.SelectedIndex = 2;
}
sdr.Close();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
catch (Exception)
{
//如果没有找到id,页面是添加模块
Button1.Text = "新增";
}
}

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "";

//判断是添加用户还是编辑用户
if (Button1.Text == "新增")
{
/*****........*****/
}
else /**************就是这里**************/
{
string ss = TextBox4.Text.Trim()
con.Open();
SqlCommand cmd = new SqlCommand("update CardUserInfo set UserName=@UserName,IdentityCardNumber=@IdentityCardNumber,"
+ "Sex=@Sex,CreditStandard=@CreditStandard,Money=@Money,PhoneNumber=@PhoneNumber,"
+ "HomeAddress=@HomeAddress,Note=@Note where CreditCardGuid=@CreditCardGuid", con);
cmd.Parameters.AddWithValue("@UserName", TextBox1.Text.Trim());
cmd.Parameters.AddWithValue("@IdentityCardNumber", TextBox2.Text.Trim());
cmd.Parameters.AddWithValue("@Sex", RadioButtonList1.SelectedValue);
cmd.Parameters.AddWithValue("@CreditStandard", DropDownList1.SelectedValue);
cmd.Parameters.AddWithValue("@Money", Convert.ToDecimal(TextBox3.Text.Trim()));
cmd.Parameters.AddWithValue("@PhoneNumber", TextBox4.Text.Trim());
cmd.Parameters.AddWithValue("@HomeAddress", TextBox5.Text.Trim());
cmd.Parameters.AddWithValue("@Note", TextBox6.Text);
cmd.Parameters.AddWithValue("@CreditCardGuid", Request["id"].ToString());
if ((int)cmd.ExecuteNonQuery() > 0)
{
Label1.Text = "更新成功!";
}



代码可能看着有点乱,请见谅哈!
就是在update那(代码中已标出),前台文本框的值我已经修改过了,点上边保存修改按钮的时候,我设了一个字符串调试下,没想到表单中所有控件的值都没变,这是什么情况啊?是不是跟表单有关呢?
求大神帮个忙啊!
...全文
115 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
_雨后初晴_ 2015-04-09
  • 打赏
  • 举报
回复
引用 6 楼 ll445480823 的回复:
!Ispostpage
一激动写错了


            if (!IsPostBack)
            {
                Bind();
            }
_雨后初晴_ 2015-04-09
  • 打赏
  • 举报
回复
!Ispostpage
程序猿老曾 2015-04-09
  • 打赏
  • 举报
回复
把page_load内的代码加个 if(!IsPoseback)试试
myhope88 2015-04-09
  • 打赏
  • 举报
回复
对呀,必须加是否回发判断
宝_爸 2015-04-09
  • 打赏
  • 举报
回复
Page.IsPostBack Property Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback.

private void Page_Load()
{
    if (!IsPostBack)
    {
        // Validate initially to force asterisks
        // to appear before the first roundtrip.
        Validate();
    }
}
以上来自msdn: https://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback(v=vs.110).aspx
  • 打赏
  • 举报
回复
没有!IsPostBack判断,Page_Load中你永远都在从数据库恢复数据啊

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧