文本框在更新时不会丢失以前的值

时间:2019-07-12 14:06:09

标签: c# asp.net

我正在通过request.querystring从gridview控件中将数据调用到文本框中。但是,当我编辑值文本框时,不会更新新值。

这是我正在从中调用数据的页面

                <td>
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" DataKeyNames="id" DataSourceID="SqlDataSource1" ForeColor="Black">
                        <Columns>
                            <asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id">
                                <EditItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" Text='<%# Eval("id") %>'></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="dealowner" HeaderText="dealowner" SortExpression="dealowner" />
                            <asp:BoundField DataField="dealname" HeaderText="dealname" SortExpression="dealname" />
                            <asp:BoundField DataField="accountname" HeaderText="accountname" SortExpression="accountname" />
                            <asp:BoundField DataField="type" HeaderText="type" SortExpression="type" />
                            <asp:BoundField DataField="nextstep" HeaderText="nextstep" SortExpression="nextstep" />
                            <asp:BoundField DataField="leadsource" HeaderText="leadsource" SortExpression="leadsource" />
                            <asp:BoundField DataField="amount" HeaderText="amount" SortExpression="amount" />
                            <asp:BoundField DataField="closingdate" HeaderText="closingdate" SortExpression="closingdate" />
                            <asp:BoundField DataField="stage" HeaderText="stage" SortExpression="stage" />
                            <asp:BoundField DataField="probability" HeaderText="probability" SortExpression="probability" />
                            <asp:BoundField DataField="expectedrevenue" HeaderText="expectedrevenue" SortExpression="expectedrevenue" />
                            <asp:BoundField DataField="campaignsource" HeaderText="campaignsource" SortExpression="campaignsource" />
                            <asp:BoundField DataField="description" HeaderText="description" SortExpression="description" />
                        </Columns>
                        <FooterStyle BackColor="#CCCCCC" />
                        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
                        <RowStyle BackColor="White" />
                        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F1F1F1" />
                        <SortedAscendingHeaderStyle BackColor="#808080" />
                        <SortedDescendingCellStyle BackColor="#CAC9C9" />
                        <SortedDescendingHeaderStyle BackColor="#383838" />
                    </asp:GridView>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:staffshawConnectionString %>" DeleteCommand="DELETE FROM [Deals] WHERE [id] = @id" InsertCommand="INSERT INTO [Deals] ([dealowner], [dealname], [accountname], [type], [nextstep], [leadsource], [amount], [closingdate], [stage], [probability], [expectedrevenue], [campaignsource], [description]) VALUES (@dealowner, @dealname, @accountname, @type, @nextstep, @leadsource, @amount, @closingdate, @stage, @probability, @expectedrevenue, @campaignsource, @description)" SelectCommand="SELECT * FROM [Deals]" UpdateCommand="UPDATE [Deals] SET [dealowner] = @dealowner, [dealname] = @dealname, [accountname] = @accountname, [type] = @type, [nextstep] = @nextstep, [leadsource] = @leadsource, [amount] = @amount, [closingdate] = @closingdate, [stage] = @stage, [probability] = @probability, [expectedrevenue] = @expectedrevenue, [campaignsource] = @campaignsource, [description] = @description WHERE [id] = @id">
                        <DeleteParameters>
                            <asp:Parameter Name="id" Type="Int32" />
                        </DeleteParameters>
                        <InsertParameters>
                            <asp:Parameter Name="dealowner" Type="String" />
                            <asp:Parameter Name="dealname" Type="String" />
                            <asp:Parameter Name="accountname" Type="String" />
                            <asp:Parameter Name="type" Type="String" />
                            <asp:Parameter Name="nextstep" Type="String" />
                            <asp:Parameter Name="leadsource" Type="String" />
                            <asp:Parameter Name="amount" Type="String" />
                            <asp:Parameter Name="closingdate" Type="String" />
                            <asp:Parameter Name="stage" Type="String" />
                            <asp:Parameter Name="probability" Type="String" />
                            <asp:Parameter Name="expectedrevenue" Type="String" />
                            <asp:Parameter Name="campaignsource" Type="String" />
                            <asp:Parameter Name="description" Type="String" />
                        </InsertParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="dealowner" Type="String" />
                            <asp:Parameter Name="dealname" Type="String" />
                            <asp:Parameter Name="accountname" Type="String" />
                            <asp:Parameter Name="type" Type="String" />
                            <asp:Parameter Name="nextstep" Type="String" />
                            <asp:Parameter Name="leadsource" Type="String" />
                            <asp:Parameter Name="amount" Type="String" />
                            <asp:Parameter Name="closingdate" Type="String" />
                            <asp:Parameter Name="stage" Type="String" />
                            <asp:Parameter Name="probability" Type="String" />
                            <asp:Parameter Name="expectedrevenue" Type="String" />
                            <asp:Parameter Name="campaignsource" Type="String" />
                            <asp:Parameter Name="description" Type="String" />
                            <asp:Parameter Name="id" Type="Int32" />
                        </UpdateParameters>
                    </asp:SqlDataSource>

这是我正在呼叫的页面:

<form id="form1" runat="server">
    <div>

        <table class="auto-style1">
            <tr>
                <td class="auto-style5"><strong>Deal Information</strong></td>
                <td class="auto-style5">
                    <asp:TextBox ID="ID" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style5"></td>
                <td class="auto-style5"></td>
            </tr>
            <tr>
                <td class="auto-style4">
                    <asp:Label ID="Label1" runat="server" Text="Deal Owner"></asp:Label>
                </td>
                <td class="auto-style4">
                    <asp:TextBox ID="dealowner" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style4">
                    <asp:Label ID="Label9" runat="server" Text="Amount"></asp:Label>
                </td>
                <td class="auto-style4">
                    <asp:TextBox ID="amount" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label2" runat="server" Text="Deal Name"></asp:Label>
                </td>
                <td class="auto-style2">
                    <asp:TextBox ID="dealname" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style2">
                    <asp:Label ID="Label10" runat="server" Text="Closing Date"></asp:Label>
                </td>
                <td class="auto-style2">
                    <asp:TextBox ID="closingdate" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style4">
                    <asp:Label ID="Label3" runat="server" Text="Account Name"></asp:Label>
                </td>
                <td class="auto-style4">
                    <asp:TextBox ID="accountname" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style4">
                    <asp:Label ID="Label11" runat="server" Text="Stage"></asp:Label>
                </td>
                <td class="auto-style4">
                    <asp:DropDownList ID="stage" runat="server">
                        <asp:ListItem>-None-</asp:ListItem>
                        <asp:ListItem>Qualification</asp:ListItem>
                        <asp:ListItem>Needs Analysis</asp:ListItem>
                        <asp:ListItem>Value Proposition</asp:ListItem>
                        <asp:ListItem>Identity Decision Makers</asp:ListItem>
                        <asp:ListItem>Proposal/Price Quote</asp:ListItem>
                        <asp:ListItem>Negotiation/Review</asp:ListItem>
                        <asp:ListItem>Closed Won</asp:ListItem>
                        <asp:ListItem>Closed Lost</asp:ListItem>
                        <asp:ListItem>Closed-Lost to Competition</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="auto-style4">
                    <asp:Label ID="Label4" runat="server" Text="Type"></asp:Label>
                </td>
                <td class="auto-style4">
                    <asp:DropDownList ID="type" runat="server">
                        <asp:ListItem>-None-</asp:ListItem>
                        <asp:ListItem>Existing Business</asp:ListItem>
                        <asp:ListItem>New Businees</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td class="auto-style4">
                    <asp:Label ID="Label12" runat="server" Text="Probability (%)"></asp:Label>
                </td>
                <td class="auto-style4">
                    <asp:TextBox ID="probability" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label5" runat="server" Text="Next Step"></asp:Label>
                </td>
                <td class="auto-style2">
                    <asp:TextBox ID="nextstep" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style2">
                    <asp:Label ID="Label13" runat="server" Text="Expected Revenue"></asp:Label>
                </td>
                <td class="auto-style2">
                    <asp:TextBox ID="expectedrevenue" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label6" runat="server" Text="Lead Source"></asp:Label>
                </td>
                <td class="auto-style2">
                    <asp:DropDownList ID="leadsource" runat="server">
                        <asp:ListItem>-None-</asp:ListItem>
                        <asp:ListItem>Advertisment</asp:ListItem>
                        <asp:ListItem>Cold Call</asp:ListItem>
                        <asp:ListItem>Employee Referral</asp:ListItem>
                        <asp:ListItem>External Referral</asp:ListItem>
                        <asp:ListItem>Online Store</asp:ListItem>
                        <asp:ListItem>Partner</asp:ListItem>
                        <asp:ListItem>Public Relations</asp:ListItem>
                        <asp:ListItem>Sales Email Alias</asp:ListItem>
                        <asp:ListItem>Seminar Partner</asp:ListItem>
                        <asp:ListItem>Internal Seminar</asp:ListItem>
                        <asp:ListItem>Trade Show</asp:ListItem>
                        <asp:ListItem>Web Download</asp:ListItem>
                        <asp:ListItem>Web Research</asp:ListItem>
                        <asp:ListItem>Chat</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td class="auto-style2">
                    <asp:Label ID="Label14" runat="server" Text="Campaign Source"></asp:Label>
                </td>
                <td class="auto-style2">
                    <asp:TextBox ID="campaignsource" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style4">
                    <asp:Label ID="Label7" runat="server" Text="Contact Name"></asp:Label>
                </td>
                <td class="auto-style4">
                    <asp:TextBox ID="contactname" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style4"></td>
                <td class="auto-style4"></td>
            </tr>
            <tr>
                <td class="auto-style3">Description Information</td>
                <td class="auto-style3"></td>
                <td class="auto-style3"></td>
                <td class="auto-style3"></td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label8" runat="server" Text="Description"></asp:Label>
                </td>
                <td class="auto-style2">
                    <asp:TextBox ID="Description" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style2">
                    <asp:Label ID="Label15" runat="server"></asp:Label>
                </td>
                <td class="auto-style2"></td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Edit" />

数据来自的页面:

protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Deals.aspx");
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditDeals.aspx?id=" + ((LinkButton)sender).Text);
        }
    }
}

这是将在其中编辑数据的页面:

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["staffshawConnectionString"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            string data = Request.QueryString["id"];
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select * from Deals where id = '" + data + "'";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    ID.Text = rdr["id"].ToString();
                    dealowner.Text = rdr["dealowner"].ToString();
                    accountname.Text = rdr["accountname"].ToString();
                    type.Text = rdr["type"].ToString();
                    nextstep.Text = rdr["nextstep"].ToString();
                    leadsource.Text = rdr["leadsource"].ToString();
                    amount.Text = rdr["amount"].ToString();
                    closingdate.Text = rdr["closingdate"].ToString();
                    stage.Text = rdr["stage"].ToString();
                    probability.Text = rdr["probability"].ToString();
                    expectedrevenue.Text = rdr["expectedrevenue"].ToString();
                    campaignsource.Text = rdr["campaignsource"].ToString();
                    Description.Text = rdr["description"].ToString();
                    dealname.Text = rdr["dealname"].ToString();

                }

            }
            conn.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {

                conn.Open();
                SqlCommand cmd = new SqlCommand("Update Deals set  dealowner='"+dealowner.Text+ "',dealname='" + dealname.Text + "',accountname='" + accountname.Text + "',type='" + type.Text + "',nextstep='" + nextstep.Text + "',leadsource='" + leadsource.Text + "',amount='" + amount.Text + "',closingdate='" + closingdate.Text + "',stage='" + stage.Text + "',probability='" + probability.Text + "',expectedrevenue='" + expectedrevenue.Text + "',campaignsource='" + campaignsource.Text + "',description='" + Description.Text + "' where id ="+ID.Text+";", conn);
               // SqlCommand cmd = new SqlCommand("Update [pessenger_details]set pnr='" + pnr.Text + "', t_no='" + t_no.Text + "', name='" + name.Text + "', type='" + type.Text + "' where id= '" + id.Text + "'", con);

                cmd.ExecuteNonQuery();

                conn.Close();

                Label1.Text = "Edit Successfully";
                Response.Redirect("AllDeals.aspx");





            }
            catch (Exception Ex)
            {
                Response.Write(Ex);
            }
        }
    }
}

我成功将数据检索到另一个Web表单,但是无法对其进行编辑。

1 个答案:

答案 0 :(得分:0)

您可能只需要在某处添加if(!Page.IsPostBack)。

单击按钮时,首先会重新加载页面,然后触发事件,最后会重新加载数据。

See this link about Page Life Cycle

仅尝试在第一个Page_load加载,而不是在每次回发时加载。

正如所说的那样,对于sql语句,除非您完全掌握了该字符串中要包含的数据(即使您认为自己有做...),否则字符串连接不是很好。

您应该考虑使用PreparedStatement