有一个页面,既是新增,也是修改保存的页面。
遇到一个问题:我想修改其中的某一个信息(TextBox),却发现无论修改哪个值,在cs文件中取出来的值都是原先那个值!!!我仔细对比了页面代码,找不到原因,求各位有经验的给点建议和参考!

解决方案 »

  1.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="company_add.aspx.cs" Inherits="ui_company_company_add" %><!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">
    td {
    font-size: 12px;

    }.HerCss {
    CURSOR: hand;
    }    .TextBoxCss
    {
        font-size:12px;
        color:Black;    
        width:250px;
    }
        .TextBoxCss {
    height:20px;
    width:140px;
    }    </style>
    </head>
    <body style="background-color: #ffffff">
        <form id="form1" runat="server">
        <div>
           <table cellpadding="0" cellspacing="0" width="100%">
                
           <tr><td bgcolor="#CDCDCD" style="height: 28px; width: 40%;" valign="middle">
                    &nbsp;当前位置:<span style="color: #cc0000"><strong><%=TitleStr %></strong></span></td>
                <td bgcolor="#CDCDCD" style="height: 28px; width: 100%;" valign="middle" align="right">
                      <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/Button/Submit.jpg" OnClick="BtnSubmit_Click" />
                      <asp:Image ID="Image1" runat="server" ImageUrl="../images/Button/JianGe.jpg" />
                      <img class="HerCss" onclick="javascript:window.history.go(-1)" src="../images/Button/BtnExit.jpg" />
                      &nbsp;
                </td>
           </tr>
           </table>
           
           <table>
             <tr>
              <td align="right" style="width: 150px; height: 27px">
                  公司名称:</td>
              <td style="height: 27px">
              <asp:TextBox ID="tb_name" runat="server" CssClass="TextBoxCss" Width="150px"></asp:TextBox>
              <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tb_name"
                Display="Dynamic" ErrorMessage="*公司站名称不能为空"></asp:RequiredFieldValidator></td>
              </tr>
              
              <tr>
              <td align="right" style="width: 150px; height: 27px">
                  公司编码:</td>
              <td style="height: 27px">
              <asp:TextBox ID="tb_code" runat="server" CssClass="TextBoxCss" Width="150px" ></asp:TextBox>
              <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="tb_code"
                Display="Dynamic" ErrorMessage="*公司编码不能为空"></asp:RequiredFieldValidator></td>
              </tr>          <tr>
              <td align="right" style="width: 150px; height: 18px">
                  所属城市:</td>
              <td style="height: 18px">
                 <asp:DropDownList ID="ddlCity" runat="server"  CssClass ="TextBoxCss"  Width="200px"></asp:DropDownList> </td>
              </tr>          <tr>
              <td align="right" style="width: 150px; height: 27px">
                  备注信息:</td>
              <td style="height: 27px">
              <asp:TextBox ID="tbRe" runat="server" CssClass="TextBoxCss" Width="360px" 
                      TextMode ="MultiLine" Height="85px"></asp:TextBox></td>
              </tr>
              
           </table>
        </div>
        </form>
    </body>
    </html>
      

  2.   

    在.cs里你怎么取值?.cs代码贴出来
      

  3.   

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using COM.QJBMM.DBUtility;
    using COM.QJBMM.Model;
    using COM.QJBMM.BLL;
    using COM.QJBMM.SerUtility;public partial class ui_company_company_add : System.Web.UI.Page
    {
        public string TitleStr = string.Empty;
        company obj = new company();
        string EntityID = string.Empty;    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CommonFun.CheckSession();
                this.ddlCity.Items.Add("请选择");
                CommonVar.BindDataDropDownList(CommonVar.CityList, this.ddlCity, "name", "code");
            }        int typeID = int.Parse(Request.QueryString["Type"].ToString());
            TitleStr = obj.CompanyTiTleStr(typeID);
            if (typeID == 3)
            {
                EntityID = Request.QueryString["ID"].ToString();
                FillDataById(EntityID);
            }
        }    private void FillDataById(string ID)
        {
            t_company t = obj.GetCompany(ID);
            this.tb_name.Text = t.NAME;
            this.tb_code.Text = t.CODE;
            string Code = CommonVar.GetCityNameByCode(t.CityCode);
            this.ddlCity.SelectedIndex = CommonFun.IndexOfDropDownList(this.ddlCity, Code);
            this.tbRe.Text = t.Re; 
        }    //保存
        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            t_company t = new t_company();
            t.CODE = this.tb_code.Text.Trim();
            t.NAME = this.tb_name.Text.Trim();
            t.CityCode = this.ddlCity.SelectedValue.Trim();
            t.Re = this.tbRe.Text.ToString();
            company obj = new company();
            int i = 0;
            if (EntityID == string.Empty)
            {
                t.ID = CommonFun.NewGuid();
                i = obj.Add(t);
            }
            else
            {
                t.ID = EntityID;
                i = obj.Update(t);
            }
            if (i == 1)
            {
                Response.Write("<script>alert('信息操作成功!');window.location= 'company.aspx?Type=1'</script>");
            }
            else
            {
                Response.Write("<script>alert('操作错误,请检查您输入的内容是否正确!');</script>");
            }
        }
    }
      

  4.   

     int typeID = int.Parse(Request.QueryString["Type"].ToString()); 
            TitleStr = obj.CompanyTiTleStr(typeID); 
            if (typeID == 3) 
            { 
                EntityID = Request.QueryString["ID"].ToString(); 
                FillDataById(EntityID); 
            } 
    这里有问题。你在点击button的时候,也要先执行page_load(),在执行button里面的。。建议把这个也放在  if(!page.ispostback())中去
      

  5.   

     FillDataById(EntityID); 
    这里又取回修改前的数据了
      

  6.   

    我发现这个逻辑不是很好处理,有没有比较好的写法,就是兼容新增和修改的。我这么写的一点都不好,给点参考吧!    public string TitleStr = string.Empty;
        company obj = new company();
        string EntityID = string.Empty;    protected void Page_Load(object sender, EventArgs e)
        {
            int typeID = int.Parse(Request.QueryString["Type"].ToString());
            TitleStr = obj.CompanyTiTleStr(typeID);        if (!Page.IsPostBack)
            {
                CommonFun.CheckSession();
                this.ddlCity.Items.Add("请选择");
                CommonVar.BindDataDropDownList(CommonVar.CityList, this.ddlCity, "name", "code");
                if (typeID == 3)
                {
                    EntityID = Request.QueryString["ID"].ToString();
                    FillDataById(EntityID);
                }
            }
            else
            {
                if(typeID =3)
                EntityID = Request.QueryString["ID"].ToString();
            }
        }